annotate README.txt @ 6:71067a445663

added README
author catherine@localhost
date Wed, 14 May 2008 10:56:05 -0400
parents
children 47af95ad83c7
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
71067a445663 added README
catherine@localhost
parents:
diff changeset
63 from cmd2 import Cmd, flagReader
71067a445663 added README
catherine@localhost
parents:
diff changeset
64
71067a445663 added README
catherine@localhost
parents:
diff changeset
65 class CmdLineApp(Cmd):
71067a445663 added README
catherine@localhost
parents:
diff changeset
66 multilineCommands = ['orate']
71067a445663 added README
catherine@localhost
parents:
diff changeset
67 Cmd.shortcuts.update({'&': 'speak'})
71067a445663 added README
catherine@localhost
parents:
diff changeset
68 maxrepeats = 3
71067a445663 added README
catherine@localhost
parents:
diff changeset
69 Cmd.settable.append('maxrepeats')
71067a445663 added README
catherine@localhost
parents:
diff changeset
70 speakflags = flagReader.FlagSet([flagReader.Flag('piglatin'),
71067a445663 added README
catherine@localhost
parents:
diff changeset
71 flagReader.Flag('shout'),
71067a445663 added README
catherine@localhost
parents:
diff changeset
72 flagReader.Flag('repeat', nargs=1)
71067a445663 added README
catherine@localhost
parents:
diff changeset
73 ])
71067a445663 added README
catherine@localhost
parents:
diff changeset
74 def do_speak(self, arg):
71067a445663 added README
catherine@localhost
parents:
diff changeset
75 """Repeats what you tell me to.
71067a445663 added README
catherine@localhost
parents:
diff changeset
76
71067a445663 added README
catherine@localhost
parents:
diff changeset
77 args: --piglatin, -p: translate to Pig Latin
71067a445663 added README
catherine@localhost
parents:
diff changeset
78 --shout, -s: emulate internet newbie
71067a445663 added README
catherine@localhost
parents:
diff changeset
79 --repeat (nTimes), -r: be redundant"""
71067a445663 added README
catherine@localhost
parents:
diff changeset
80 (options, arg) = self.speakflags.parse(arg)
71067a445663 added README
catherine@localhost
parents:
diff changeset
81
71067a445663 added README
catherine@localhost
parents:
diff changeset
82 if options.has_key('piglatin'):
71067a445663 added README
catherine@localhost
parents:
diff changeset
83 arg = '%s%say' % (arg[1:], arg[0])
71067a445663 added README
catherine@localhost
parents:
diff changeset
84 if options.has_key('shout'):
71067a445663 added README
catherine@localhost
parents:
diff changeset
85 arg = arg.upper()
71067a445663 added README
catherine@localhost
parents:
diff changeset
86 repetitions = options.get('repeat')
71067a445663 added README
catherine@localhost
parents:
diff changeset
87 repetitions = int(repetitions[0]) if repetitions else 1
71067a445663 added README
catherine@localhost
parents:
diff changeset
88 for i in range(min(repetitions, self.maxrepeats)):
71067a445663 added README
catherine@localhost
parents:
diff changeset
89 self.stdout.write(arg)
71067a445663 added README
catherine@localhost
parents:
diff changeset
90 self.stdout.write('\n')
71067a445663 added README
catherine@localhost
parents:
diff changeset
91 # self.stdout.write is better than "print", because Cmd can be
71067a445663 added README
catherine@localhost
parents:
diff changeset
92 # initialized with a non-standard output destination
71067a445663 added README
catherine@localhost
parents:
diff changeset
93 do_say = do_speak # now "say" is a synonym for "speak"
71067a445663 added README
catherine@localhost
parents:
diff changeset
94 do_orate = do_speak # another synonym, but this one takes multi-line input
71067a445663 added README
catherine@localhost
parents:
diff changeset
95
71067a445663 added README
catherine@localhost
parents:
diff changeset
96 app = CmdLineApp()
71067a445663 added README
catherine@localhost
parents:
diff changeset
97 app.cmdloop()
71067a445663 added README
catherine@localhost
parents:
diff changeset
98
71067a445663 added README
catherine@localhost
parents:
diff changeset
99 Sample session using the above code ::
71067a445663 added README
catherine@localhost
parents:
diff changeset
100
71067a445663 added README
catherine@localhost
parents:
diff changeset
101 c:\cmd2>python cmd2_example.py
71067a445663 added README
catherine@localhost
parents:
diff changeset
102 (Cmd) speak softly
71067a445663 added README
catherine@localhost
parents:
diff changeset
103 softly
71067a445663 added README
catherine@localhost
parents:
diff changeset
104 (Cmd) speak --piglatin softly
71067a445663 added README
catherine@localhost
parents:
diff changeset
105 oftlysay
71067a445663 added README
catherine@localhost
parents:
diff changeset
106 (Cmd) speak -psr 2 softly
71067a445663 added README
catherine@localhost
parents:
diff changeset
107 OFTLYSAY
71067a445663 added README
catherine@localhost
parents:
diff changeset
108 OFTLYSAY
71067a445663 added README
catherine@localhost
parents:
diff changeset
109 (Cmd) speak --repeat 1000000 softly
71067a445663 added README
catherine@localhost
parents:
diff changeset
110 softly
71067a445663 added README
catherine@localhost
parents:
diff changeset
111 softly
71067a445663 added README
catherine@localhost
parents:
diff changeset
112 softly
71067a445663 added README
catherine@localhost
parents:
diff changeset
113 (Cmd) show maxrepeats
71067a445663 added README
catherine@localhost
parents:
diff changeset
114 maxrepeats: 3
71067a445663 added README
catherine@localhost
parents:
diff changeset
115 (Cmd) set maxrepeats 5
71067a445663 added README
catherine@localhost
parents:
diff changeset
116 maxrepeats - was: 3
71067a445663 added README
catherine@localhost
parents:
diff changeset
117 now: 5
71067a445663 added README
catherine@localhost
parents:
diff changeset
118 (Cmd) speak --repeat 1000000 softly
71067a445663 added README
catherine@localhost
parents:
diff changeset
119 softly
71067a445663 added README
catherine@localhost
parents:
diff changeset
120 softly
71067a445663 added README
catherine@localhost
parents:
diff changeset
121 softly
71067a445663 added README
catherine@localhost
parents:
diff changeset
122 softly
71067a445663 added README
catherine@localhost
parents:
diff changeset
123 softly
71067a445663 added README
catherine@localhost
parents:
diff changeset
124 (Cmd) orate blah blah
71067a445663 added README
catherine@localhost
parents:
diff changeset
125 > blah
71067a445663 added README
catherine@localhost
parents:
diff changeset
126 > and furthermore
71067a445663 added README
catherine@localhost
parents:
diff changeset
127 > blah
71067a445663 added README
catherine@localhost
parents:
diff changeset
128 >
71067a445663 added README
catherine@localhost
parents:
diff changeset
129 blah blah blah and furthermore blah
71067a445663 added README
catherine@localhost
parents:
diff changeset
130 (Cmd) &greetings
71067a445663 added README
catherine@localhost
parents:
diff changeset
131 greetings
71067a445663 added README
catherine@localhost
parents:
diff changeset
132 (Cmd) history
71067a445663 added README
catherine@localhost
parents:
diff changeset
133 -------------------------[1]
71067a445663 added README
catherine@localhost
parents:
diff changeset
134 speak softly
71067a445663 added README
catherine@localhost
parents:
diff changeset
135 -------------------------[2]
71067a445663 added README
catherine@localhost
parents:
diff changeset
136 speak --piglatin softly
71067a445663 added README
catherine@localhost
parents:
diff changeset
137 -------------------------[3]
71067a445663 added README
catherine@localhost
parents:
diff changeset
138 speak -psr 2 softly
71067a445663 added README
catherine@localhost
parents:
diff changeset
139 -------------------------[4]
71067a445663 added README
catherine@localhost
parents:
diff changeset
140 speak --repeat 1000000 softly
71067a445663 added README
catherine@localhost
parents:
diff changeset
141 -------------------------[5]
71067a445663 added README
catherine@localhost
parents:
diff changeset
142 show maxrepeats
71067a445663 added README
catherine@localhost
parents:
diff changeset
143 -------------------------[6]
71067a445663 added README
catherine@localhost
parents:
diff changeset
144 set maxrepeats 5
71067a445663 added README
catherine@localhost
parents:
diff changeset
145 -------------------------[7]
71067a445663 added README
catherine@localhost
parents:
diff changeset
146 speak --repeat 1000000 softly
71067a445663 added README
catherine@localhost
parents:
diff changeset
147 -------------------------[8]
71067a445663 added README
catherine@localhost
parents:
diff changeset
148 orate blah blah
71067a445663 added README
catherine@localhost
parents:
diff changeset
149 blah
71067a445663 added README
catherine@localhost
parents:
diff changeset
150 and furthermore
71067a445663 added README
catherine@localhost
parents:
diff changeset
151 blah
71067a445663 added README
catherine@localhost
parents:
diff changeset
152
71067a445663 added README
catherine@localhost
parents:
diff changeset
153 -------------------------[9]
71067a445663 added README
catherine@localhost
parents:
diff changeset
154 &greetings
71067a445663 added README
catherine@localhost
parents:
diff changeset
155 (Cmd) run
71067a445663 added README
catherine@localhost
parents:
diff changeset
156 orate blah blah
71067a445663 added README
catherine@localhost
parents:
diff changeset
157 blah
71067a445663 added README
catherine@localhost
parents:
diff changeset
158 and furthermore
71067a445663 added README
catherine@localhost
parents:
diff changeset
159 blah
71067a445663 added README
catherine@localhost
parents:
diff changeset
160
71067a445663 added README
catherine@localhost
parents:
diff changeset
161 blah blah blah and furthermore blah
71067a445663 added README
catherine@localhost
parents:
diff changeset
162 (Cmd) run 3
71067a445663 added README
catherine@localhost
parents:
diff changeset
163 speak -psr 2 softly
71067a445663 added README
catherine@localhost
parents:
diff changeset
164 OFTLYSAY
71067a445663 added README
catherine@localhost
parents:
diff changeset
165 OFTLYSAY
71067a445663 added README
catherine@localhost
parents:
diff changeset
166 (Cmd) history maxrepeats
71067a445663 added README
catherine@localhost
parents:
diff changeset
167 -------------------------[5]
71067a445663 added README
catherine@localhost
parents:
diff changeset
168 set maxrepeats
71067a445663 added README
catherine@localhost
parents:
diff changeset
169 -------------------------[6]
71067a445663 added README
catherine@localhost
parents:
diff changeset
170 set maxrepeats 5
71067a445663 added README
catherine@localhost
parents:
diff changeset
171 (Cmd)