| 1 |
|
| 2 |
"""A sample application for cmd2 demonstrating customized environment parameters.""" |
| 3 |
|
| 4 |
import cmd2 |
| 5 |
|
| 6 |
|
| 7 |
class EnvironmentApp(cmd2.Cmd): |
| 8 |
"""Example cmd2 application.""" |
| 9 |
|
| 10 |
def __init__(self) -> None: |
| 11 |
super().__init__() |
| 12 |
self.degrees_c = 22 |
| 13 |
self.sunny = False |
| 14 |
self.add_settable( |
| 15 |
cmd2.Settable("degrees_c", int, "Temperature in Celsius", self, onchange_cb=self._onchange_degrees_c) |
| 16 |
) |
| 17 |
self.add_settable(cmd2.Settable("sunny", bool, "Is it sunny outside?", self)) |
| 18 |
|
| 19 |
def do_sunbathe(self, _arg) -> None: |
| 20 |
"""Attempt to sunbathe.""" |
| 21 |
if self.degrees_c < 20: |
| 22 |
result = f"It's {self.degrees_c} C - are you a penguin?" |
| 23 |
elif not self.sunny: |
| 24 |
result = "Too dim." |
| 25 |
else: |
| 26 |
result = "UV is bad for your skin." |
| 27 |
self.poutput(result) |
| 28 |
|
| 29 |
def _onchange_degrees_c(self, _param_name, _old, new) -> None: |
| 30 |
|
| 31 |
if new > 40: |
| 32 |
self.sunny = True |
| 33 |
|
| 34 |
|
| 35 |
if __name__ == "__main__": |
| 36 |
import sys |
| 37 |
|
| 38 |
c = EnvironmentApp() |
| 39 |
sys.exit(c.cmdloop()) |