@@ -82,47 +82,39 @@ to *use* ``arg.parsed``.)
8282Environment parameters
8383======================
8484
85- Your application can define user-settable parameters
86- which your code can reference. Create them as class attributes
87- with their default values, and add them (with optional
88- documentation) to ``settable ``.
89-
90- ::
91-
92- from cmd2 import Cmd
93- class App(Cmd):
94- degrees_c = 22
95- sunny = False
96- settable = Cmd.settable + '''degrees_c temperature in Celsius
97- sunny'''
98- def do_sunbathe(self, arg):
99- if self.degrees_c < 20:
100- result = "It's {temp} C - are you a penguin?".format(temp=self.degrees_c)
101- elif not self.sunny:
102- result = 'Too dim.'
103- else:
104- result = 'UV is bad for your skin.'
105- self.stdout.write(result + '\n')
106- app = App()
107- app.cmdloop()
108-
109- ::
110-
111- (Cmd) set --long
112- degrees_c: 22 # temperature in Celsius
113- sunny: False #
114- (Cmd) sunbathe
115- Too dim.
116- (Cmd) set sunny yes
117- sunny - was: False
118- now: True
119- (Cmd) sunbathe
120- UV is bad for your skin.
121- (Cmd) set degrees_c 13
122- degrees_c - was: 22
123- now: 13
124- (Cmd) sunbathe
125- It's 13 C - are you a penguin?
85+ Your application can define user-settable parameters which your code can
86+ reference. First create a class attribute with the default value. Then
87+ update the ``settable `` dictionary with your setting name and a short
88+ description before you initialize the superclass. Here's an example, from
89+ ``examples/environment.py ``:
90+
91+ .. literalinclude :: ../examples/environment.py
92+
93+ If you want to be notified when a setting changes (as we do above), then
94+ define a method ``_onchange_{setting}() ``. This method will be called after
95+ the user changes a setting, and will receive both the old value and the new
96+ value.
97+
98+ .. code-block :: none
99+
100+ (Cmd) set --long | grep sunny
101+ sunny: False # Is it sunny outside?
102+ (Cmd) set --long | grep degrees
103+ degrees_c: 22 # Temperature in Celsius
104+ (Cmd) sunbathe
105+ Too dim.
106+ (Cmd) set degrees_c 41
107+ degrees_c - was: 22
108+ now: 41
109+ (Cmd) set sunny
110+ sunny: True
111+ (Cmd) sunbathe
112+ UV is bad for your skin.
113+ (Cmd) set degrees_c 13
114+ degrees_c - was: 41
115+ now: 13
116+ (Cmd) sunbathe
117+ It's 13 C - are you a penguin?
126118
127119
128120 Commands with flags
0 commit comments