Mercurial > python-cmd2
annotate README.txt @ 112:e3b8eaadea56
going to collapse down out of overdone package structure
author | catherine@Elli.myhome.westell.com |
---|---|
date | Sat, 25 Oct 2008 19:28:51 -0400 |
parents | 234fb764becd |
children | 0820c42ea23e |
rev | line source |
---|---|
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 | |
54 | 9 - Special-character shortcut commands (beyond cmd's `@` and `!`) |
6 | 10 - Settable environment parameters |
11 - Parsing commands with flags | |
54 | 12 - Redirection to file with `>`, `>>`; input from file with `<` |
13 - Bare '>', '>>' with no filename send output to paste buffer | |
14 - Pipe output to shell commands with `|` | |
6 | 15 |
16 Instructions for implementing each feature follow. | |
17 | |
18 - Searchable command history | |
19 | |
20 All commands will automatically be tracked in the session's history, unless the command is listed in Cmd's excludeFromHistory attribute. | |
21 The history is accessed through the `history`, `list`, and `run` commands | |
22 (and their abbreviations: `hi`, `li`, `l`, `r`). | |
23 If you wish to exclude some of your custom commands from the history, append their names | |
24 to the list at Cmd.ExcludeFromHistory. | |
25 | |
26 - Load commands from file, save to file, edit commands in file | |
27 | |
28 Type `help load`, `help save`, `help edit` for details. | |
29 | |
30 - Multi-line commands | |
31 | |
32 Any command accepts multi-line input when its name is listed in `Cmd.multilineCommands`. | |
33 The program will keep expecting input until a line ends with any of the characters | |
34 in `Cmd.terminators` . The default terminators are `;` and `/n` (empty newline). | |
35 | |
36 - Case-insensitive commands | |
37 | |
38 All commands are case-insensitive, unless `Cmd.caseInsensitive` is set to `False`. | |
39 | |
40 - Special-character shortcut commands (beyond cmd's "@" and "!") | |
41 | |
42 To create a single-character shortcut for a command, update `Cmd.shortcuts`. | |
43 | |
44 - Settable environment parameters | |
45 | |
46 To allow a user to change an environment parameter during program execution, | |
47 append the parameter's name to `Cmd.settable`. | |
48 | |
13 | 49 - Parsing commands with `optparse` options (flags) |
6 | 50 |
13 | 51 :: |
52 | |
17 | 53 @options([make_option('-m', '--myoption', action="store_true", help="all about my option")]) |
19
1899088dd95d
more readme correction, now in function def
catherine@localhost
parents:
18
diff
changeset
|
54 def do_myfunc(self, arg, opts): |
18
b7489d3f838e
more readme correction, now in function def
catherine@localhost
parents:
17
diff
changeset
|
55 if opts.myoption: |
b7489d3f838e
more readme correction, now in function def
catherine@localhost
parents:
17
diff
changeset
|
56 ... |
13 | 57 |
58 See Python standard library's `optparse` documentation: http://docs.python.org/lib/optparse-defining-options.html | |
59 | |
18
b7489d3f838e
more readme correction, now in function def
catherine@localhost
parents:
17
diff
changeset
|
60 - Catherine Devlin, http://catherinedevlin.blogspot.com |
6 | 61 |
62 cmd2 can be installed with `easy_install cmd2` | |
63 | |
16 | 64 Cheese Shop page: http://pypi.python.org/pypi/cmd2 |
6 | 65 |
112
e3b8eaadea56
going to collapse down out of overdone package structure
catherine@Elli.myhome.westell.com
parents:
110
diff
changeset
|
66 Example cmd2 application (example/example.py) :: |
6 | 67 |
112
e3b8eaadea56
going to collapse down out of overdone package structure
catherine@Elli.myhome.westell.com
parents:
110
diff
changeset
|
68 '''A sample application for cmd2.''' |
e3b8eaadea56
going to collapse down out of overdone package structure
catherine@Elli.myhome.westell.com
parents:
110
diff
changeset
|
69 |
e3b8eaadea56
going to collapse down out of overdone package structure
catherine@Elli.myhome.westell.com
parents:
110
diff
changeset
|
70 from cmd2 import Cmd, make_option, options, Cmd2TestCase |
e3b8eaadea56
going to collapse down out of overdone package structure
catherine@Elli.myhome.westell.com
parents:
110
diff
changeset
|
71 import unittest, optparse, sys |
e3b8eaadea56
going to collapse down out of overdone package structure
catherine@Elli.myhome.westell.com
parents:
110
diff
changeset
|
72 |
6 | 73 class CmdLineApp(Cmd): |
74 multilineCommands = ['orate'] | |
75 Cmd.shortcuts.update({'&': 'speak'}) | |
76 maxrepeats = 3 | |
112
e3b8eaadea56
going to collapse down out of overdone package structure
catherine@Elli.myhome.westell.com
parents:
110
diff
changeset
|
77 Cmd.settable.append('maxrepeats') |
13 | 78 |
79 @options([make_option('-p', '--piglatin', action="store_true", help="atinLay"), | |
80 make_option('-s', '--shout', action="store_true", help="N00B EMULATION MODE"), | |
81 make_option('-r', '--repeat', type="int", help="output [n] times") | |
82 ]) | |
83 def do_speak(self, arg, opts=None): | |
84 """Repeats what you tell me to.""" | |
112
e3b8eaadea56
going to collapse down out of overdone package structure
catherine@Elli.myhome.westell.com
parents:
110
diff
changeset
|
85 arg = ''.join(arg) |
13 | 86 if opts.piglatin: |
6 | 87 arg = '%s%say' % (arg[1:], arg[0]) |
13 | 88 if opts.shout: |
112
e3b8eaadea56
going to collapse down out of overdone package structure
catherine@Elli.myhome.westell.com
parents:
110
diff
changeset
|
89 arg = arg.upper() |
13 | 90 repetitions = opts.repeat or 1 |
6 | 91 for i in range(min(repetitions, self.maxrepeats)): |
92 self.stdout.write(arg) | |
93 self.stdout.write('\n') | |
112
e3b8eaadea56
going to collapse down out of overdone package structure
catherine@Elli.myhome.westell.com
parents:
110
diff
changeset
|
94 # self.stdout.write is better than "print", because Cmd can be |
6 | 95 # initialized with a non-standard output destination |
112
e3b8eaadea56
going to collapse down out of overdone package structure
catherine@Elli.myhome.westell.com
parents:
110
diff
changeset
|
96 |
6 | 97 do_say = do_speak # now "say" is a synonym for "speak" |
98 do_orate = do_speak # another synonym, but this one takes multi-line input | |
13 | 99 |
112
e3b8eaadea56
going to collapse down out of overdone package structure
catherine@Elli.myhome.westell.com
parents:
110
diff
changeset
|
100 class TestMyAppCase(Cmd2TestCase): |
e3b8eaadea56
going to collapse down out of overdone package structure
catherine@Elli.myhome.westell.com
parents:
110
diff
changeset
|
101 CmdApp = CmdLineApp |
e3b8eaadea56
going to collapse down out of overdone package structure
catherine@Elli.myhome.westell.com
parents:
110
diff
changeset
|
102 transcriptFileName = 'exampleSession.txt' |
e3b8eaadea56
going to collapse down out of overdone package structure
catherine@Elli.myhome.westell.com
parents:
110
diff
changeset
|
103 |
e3b8eaadea56
going to collapse down out of overdone package structure
catherine@Elli.myhome.westell.com
parents:
110
diff
changeset
|
104 parser = optparse.OptionParser() |
e3b8eaadea56
going to collapse down out of overdone package structure
catherine@Elli.myhome.westell.com
parents:
110
diff
changeset
|
105 parser.add_option('-t', '--test', dest='unittests', action='store_true', default=False, help='Run unit test suite') |
e3b8eaadea56
going to collapse down out of overdone package structure
catherine@Elli.myhome.westell.com
parents:
110
diff
changeset
|
106 (callopts, callargs) = parser.parse_args() |
e3b8eaadea56
going to collapse down out of overdone package structure
catherine@Elli.myhome.westell.com
parents:
110
diff
changeset
|
107 if callopts.unittests: |
e3b8eaadea56
going to collapse down out of overdone package structure
catherine@Elli.myhome.westell.com
parents:
110
diff
changeset
|
108 sys.argv = [sys.argv[0]] # the --test argument upsets unittest.main() |
e3b8eaadea56
going to collapse down out of overdone package structure
catherine@Elli.myhome.westell.com
parents:
110
diff
changeset
|
109 unittest.main() |
e3b8eaadea56
going to collapse down out of overdone package structure
catherine@Elli.myhome.westell.com
parents:
110
diff
changeset
|
110 else: |
e3b8eaadea56
going to collapse down out of overdone package structure
catherine@Elli.myhome.westell.com
parents:
110
diff
changeset
|
111 app = CmdLineApp() |
e3b8eaadea56
going to collapse down out of overdone package structure
catherine@Elli.myhome.westell.com
parents:
110
diff
changeset
|
112 app.cmdloop() |
6 | 113 |
114 Sample session using the above code :: | |
115 | |
116 c:\cmd2>python cmd2_example.py | |
117 (Cmd) speak softly | |
118 softly | |
119 (Cmd) speak --piglatin softly | |
120 oftlysay | |
121 (Cmd) speak -psr 2 softly | |
122 OFTLYSAY | |
123 OFTLYSAY | |
124 (Cmd) speak --repeat 1000000 softly | |
125 softly | |
126 softly | |
127 softly | |
128 (Cmd) show maxrepeats | |
129 maxrepeats: 3 | |
130 (Cmd) set maxrepeats 5 | |
131 maxrepeats - was: 3 | |
132 now: 5 | |
133 (Cmd) speak --repeat 1000000 softly | |
134 softly | |
135 softly | |
136 softly | |
137 softly | |
138 softly | |
139 (Cmd) orate blah blah | |
140 > blah | |
141 > and furthermore | |
142 > blah | |
143 > | |
144 blah blah blah and furthermore blah | |
145 (Cmd) &greetings | |
146 greetings | |
147 (Cmd) history | |
148 -------------------------[1] | |
149 speak softly | |
150 -------------------------[2] | |
151 speak --piglatin softly | |
152 -------------------------[3] | |
153 speak -psr 2 softly | |
154 -------------------------[4] | |
155 speak --repeat 1000000 softly | |
156 -------------------------[5] | |
157 show maxrepeats | |
158 -------------------------[6] | |
159 set maxrepeats 5 | |
160 -------------------------[7] | |
161 speak --repeat 1000000 softly | |
162 -------------------------[8] | |
163 orate blah blah | |
164 blah | |
165 and furthermore | |
166 blah | |
167 | |
168 -------------------------[9] | |
169 &greetings | |
170 (Cmd) run | |
171 orate blah blah | |
172 blah | |
173 and furthermore | |
174 blah | |
175 | |
176 blah blah blah and furthermore blah | |
177 (Cmd) run 3 | |
178 speak -psr 2 softly | |
179 OFTLYSAY | |
180 OFTLYSAY | |
181 (Cmd) history maxrepeats | |
182 -------------------------[5] | |
183 set maxrepeats | |
184 -------------------------[6] | |
185 set maxrepeats 5 | |
54 | 186 (Cmd) speak a dead parrot > pet.txt |
187 (Cmd) speak < pet.txt | |
188 a dead parrot | |
189 (Cmd) speak only resting | wc | |
190 1 2 13 | |
191 (Cmd) |