315
|
1 ======================================
|
|
2 Features requiring application changes
|
|
3 ======================================
|
|
4
|
|
5 Command shortcuts
|
|
6 =================
|
|
7
|
324
|
8 .. _parameters:
|
|
9
|
315
|
10 Environment parameters
|
|
11 ======================
|
|
12
|
316
|
13 Your application can define user-settable parameters
|
324
|
14 which your code can reference. Create them as class attributes
|
|
15 with their default values, and add them (with optional
|
|
16 documentation) to ``settable``.
|
|
17
|
|
18 ::
|
316
|
19
|
324
|
20 from cmd2 import Cmd
|
|
21 class App(Cmd):
|
|
22 degrees_c = 22
|
|
23 sunny = False
|
|
24 settable = Cmd.settable + '''degrees_c temperature in Celsius
|
|
25 sunny'''
|
|
26 def do_sunbathe(self, arg):
|
|
27 if self.degrees_c < 20:
|
|
28 result = "It's {temp} C - are you a penguin?".format(temp=self.degrees_c)
|
|
29 elif not self.sunny:
|
|
30 result = 'Too dim.'
|
|
31 else:
|
|
32 result = 'UV is bad for your skin.'
|
|
33 self.stdout.write(result + '\n')
|
|
34 app = App()
|
|
35 app.cmdloop()
|
|
36
|
|
37 ::
|
|
38
|
|
39 (Cmd) set --long
|
|
40 degrees_c: 22 # temperature in Celsius
|
|
41 sunny: False #
|
|
42 (Cmd) sunbathe
|
|
43 Too dim.
|
|
44 (Cmd) set sunny yes
|
|
45 sunny - was: False
|
|
46 now: True
|
|
47 (Cmd) sunbathe
|
|
48 UV is bad for your skin.
|
|
49 (Cmd) set degrees_c 13
|
|
50 degrees_c - was: 22
|
|
51 now: 13
|
|
52 (Cmd) sunbathe
|
|
53 It's 13 C - are you a penguin?
|
316
|
54
|
|
55
|
315
|
56 Commands with flags
|
|
57 ===================
|
324
|
58
|
|
59 .. _outputters:
|
|
60
|
|
61 poutput, pfeedback, perror
|
|
62 ==========================
|
|
63
|
|
64 Standard ``cmd`` applications produce their output with ``self.stdout.write('output')`` (or with ``print``,
|
|
65 but ``print`` decreases output flexibility). ``cmd2`` applications can use
|
|
66 ``self.poutput('output')``, ``self.pfeedback('message')``, and ``self.perror('errmsg')``
|
|
67 instead. These methods have these advantages:
|
|
68
|
|
69 - More concise
|
|
70 - ``.pfeedback()`` destination is controlled by :ref:`quiet` parameter.
|
|
71 |