Skip to content

Commit 6d331a5

Browse files
committed
Revise environment setting documentation
1 parent c2418a9 commit 6d331a5

File tree

2 files changed

+71
-41
lines changed

2 files changed

+71
-41
lines changed

docs/unfreefeatures.rst

Lines changed: 33 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -82,47 +82,39 @@ to *use* ``arg.parsed``.)
8282
Environment 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

examples/environment.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
#!/usr/bin/env python
2+
# coding=utf-8
3+
"""
4+
A sample application for cmd2 demonstrating customized environment parameters
5+
"""
6+
7+
from cmd2 import Cmd
8+
9+
10+
class EnvironmentApp(Cmd):
11+
""" Example cmd2 application. """
12+
13+
degrees_c = 22
14+
sunny = False
15+
16+
def __init__(self):
17+
self.settable.update({'degrees_c': 'Temperature in Celsius'})
18+
self.settable.update({'sunny': 'Is it sunny outside?'})
19+
Cmd.__init__(self)
20+
21+
def do_sunbathe(self, arg):
22+
if self.degrees_c < 20:
23+
result = "It's {} C - are you a penguin?".format(self.degrees_c)
24+
elif not self.sunny:
25+
result = 'Too dim.'
26+
else:
27+
result = 'UV is bad for your skin.'
28+
self.poutput(result)
29+
30+
def _onchange_degrees_c(self, old, new):
31+
# if it's over 40C, it's gotta be sunny, right?
32+
if new > 40:
33+
self.sunny = True
34+
35+
36+
if __name__ == '__main__':
37+
c = EnvironmentApp()
38+
c.cmdloop()

0 commit comments

Comments
 (0)