annotate README.txt @ 10:47af95ad83c7

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