6
|
1 `cmd2` is a tool for writing command-line interactive applications. It is based on the Python Standard Library's `cmd` module, and can be used anyplace `cmd` is used simply by importing `cmd2` instead.
|
|
2
|
|
3 `cmd2` provides the following features, in addition to those already existing in `cmd`:
|
|
4
|
|
5 - Searchable command history
|
|
6 - Load commands from file, save to file, edit commands in file
|
|
7 - Multi-line commands
|
|
8 - Case-insensitive commands
|
|
9 - Special-character shortcut commands (beyond cmd's "@" and "!")
|
|
10 - Settable environment parameters
|
|
11 - Parsing commands with flags
|
|
12
|
|
13 Instructions for implementing each feature follow.
|
|
14
|
|
15 - Searchable command history
|
|
16
|
|
17 All commands will automatically be tracked in the session's history, unless the command is listed in Cmd's excludeFromHistory attribute.
|
|
18 The history is accessed through the `history`, `list`, and `run` commands
|
|
19 (and their abbreviations: `hi`, `li`, `l`, `r`).
|
|
20 If you wish to exclude some of your custom commands from the history, append their names
|
|
21 to the list at Cmd.ExcludeFromHistory.
|
|
22
|
|
23 - Load commands from file, save to file, edit commands in file
|
|
24
|
|
25 Type `help load`, `help save`, `help edit` for details.
|
|
26
|
|
27 - Multi-line commands
|
|
28
|
|
29 Any command accepts multi-line input when its name is listed in `Cmd.multilineCommands`.
|
|
30 The program will keep expecting input until a line ends with any of the characters
|
|
31 in `Cmd.terminators` . The default terminators are `;` and `/n` (empty newline).
|
|
32
|
|
33 - Case-insensitive commands
|
|
34
|
|
35 All commands are case-insensitive, unless `Cmd.caseInsensitive` is set to `False`.
|
|
36
|
|
37 - Special-character shortcut commands (beyond cmd's "@" and "!")
|
|
38
|
|
39 To create a single-character shortcut for a command, update `Cmd.shortcuts`.
|
|
40
|
|
41 - Settable environment parameters
|
|
42
|
|
43 To allow a user to change an environment parameter during program execution,
|
|
44 append the parameter's name to `Cmd.settable`.
|
|
45
|
13
|
46 - Parsing commands with `optparse` options (flags)
|
6
|
47
|
13
|
48 ::
|
|
49
|
|
50 @options([make_option('-m', '--myoption', action="store_true", help="atinLay")])
|
|
51 def myfunc(self, arg):
|
|
52 ...
|
|
53
|
|
54 See Python standard library's `optparse` documentation: http://docs.python.org/lib/optparse-defining-options.html
|
|
55
|
6
|
56 - Catherine Devlin, catherinedevlin.blogspot.com
|
|
57
|
|
58 cmd2 can be installed with `easy_install cmd2`
|
|
59
|
|
60 Cheese Shop page: http://pypi.python.org/pypi/cmd2/0.1
|
|
61
|
|
62 Example cmd2 application (cmd2_example.py) ::
|
|
63
|
13
|
64 from cmd2 import Cmd, make_option, options
|
|
65
|
6
|
66 class CmdLineApp(Cmd):
|
|
67 multilineCommands = ['orate']
|
|
68 Cmd.shortcuts.update({'&': 'speak'})
|
|
69 maxrepeats = 3
|
13
|
70 Cmd.settable.append('maxrepeats')
|
|
71
|
|
72 @options([make_option('-p', '--piglatin', action="store_true", help="atinLay"),
|
|
73 make_option('-s', '--shout', action="store_true", help="N00B EMULATION MODE"),
|
|
74 make_option('-r', '--repeat', type="int", help="output [n] times")
|
|
75 ])
|
|
76 def do_speak(self, arg, opts=None):
|
|
77 """Repeats what you tell me to."""
|
|
78 arg = ' '.join(arg)
|
|
79 if opts.piglatin:
|
6
|
80 arg = '%s%say' % (arg[1:], arg[0])
|
13
|
81 if opts.shout:
|
6
|
82 arg = arg.upper()
|
13
|
83 repetitions = opts.repeat or 1
|
6
|
84 for i in range(min(repetitions, self.maxrepeats)):
|
|
85 self.stdout.write(arg)
|
|
86 self.stdout.write('\n')
|
|
87 # self.stdout.write is better than "print", because Cmd can be
|
|
88 # initialized with a non-standard output destination
|
13
|
89
|
6
|
90 do_say = do_speak # now "say" is a synonym for "speak"
|
|
91 do_orate = do_speak # another synonym, but this one takes multi-line input
|
13
|
92
|
6
|
93 app = CmdLineApp()
|
|
94 app.cmdloop()
|
|
95
|
|
96 Sample session using the above code ::
|
|
97
|
|
98 c:\cmd2>python cmd2_example.py
|
|
99 (Cmd) speak softly
|
|
100 softly
|
|
101 (Cmd) speak --piglatin softly
|
|
102 oftlysay
|
|
103 (Cmd) speak -psr 2 softly
|
|
104 OFTLYSAY
|
|
105 OFTLYSAY
|
|
106 (Cmd) speak --repeat 1000000 softly
|
|
107 softly
|
|
108 softly
|
|
109 softly
|
|
110 (Cmd) show maxrepeats
|
|
111 maxrepeats: 3
|
|
112 (Cmd) set maxrepeats 5
|
|
113 maxrepeats - was: 3
|
|
114 now: 5
|
|
115 (Cmd) speak --repeat 1000000 softly
|
|
116 softly
|
|
117 softly
|
|
118 softly
|
|
119 softly
|
|
120 softly
|
|
121 (Cmd) orate blah blah
|
|
122 > blah
|
|
123 > and furthermore
|
|
124 > blah
|
|
125 >
|
|
126 blah blah blah and furthermore blah
|
|
127 (Cmd) &greetings
|
|
128 greetings
|
|
129 (Cmd) history
|
|
130 -------------------------[1]
|
|
131 speak softly
|
|
132 -------------------------[2]
|
|
133 speak --piglatin softly
|
|
134 -------------------------[3]
|
|
135 speak -psr 2 softly
|
|
136 -------------------------[4]
|
|
137 speak --repeat 1000000 softly
|
|
138 -------------------------[5]
|
|
139 show maxrepeats
|
|
140 -------------------------[6]
|
|
141 set maxrepeats 5
|
|
142 -------------------------[7]
|
|
143 speak --repeat 1000000 softly
|
|
144 -------------------------[8]
|
|
145 orate blah blah
|
|
146 blah
|
|
147 and furthermore
|
|
148 blah
|
|
149
|
|
150 -------------------------[9]
|
|
151 &greetings
|
|
152 (Cmd) run
|
|
153 orate blah blah
|
|
154 blah
|
|
155 and furthermore
|
|
156 blah
|
|
157
|
|
158 blah blah blah and furthermore blah
|
|
159 (Cmd) run 3
|
|
160 speak -psr 2 softly
|
|
161 OFTLYSAY
|
|
162 OFTLYSAY
|
|
163 (Cmd) history maxrepeats
|
|
164 -------------------------[5]
|
|
165 set maxrepeats
|
|
166 -------------------------[6]
|
|
167 set maxrepeats 5
|
|
168 (Cmd)
|