Mercurial > python-cmd2
annotate docs/pycon2010/pycon2010.rst @ 356:d275d3beceff
fix usage help name-stripping bug
author | catherine@dellzilla |
---|---|
date | Wed, 17 Feb 2010 14:32:04 -0500 |
parents | 5e3f918c41d8 |
children | ce4448ca4088 |
rev | line source |
---|---|
351 | 1 ================================================ |
2 Easy command-line interpreters with cmd and cmd2 | |
3 ================================================ | |
4 | |
5 :author: Catherine Devlin | |
6 :date: 2010-02-20 | |
7 | |
8 Quit scribbling | |
9 =============== | |
10 | |
11 Slides are *already* posted at | |
12 catherinedevlin.pythoneers.com | |
337 | 13 |
14 Web 2.0 | |
15 ======= | |
16 | |
17 .. image:: web-2-0-logos.gif | |
344 | 18 :height: 300px |
337 | 19 |
20 But first... | |
21 ============ | |
22 | |
23 .. image:: sargon.jpg | |
344 | 24 :height: 300px |
25 | |
351 | 26 .. image:: akkad.png |
27 :height: 300px | |
28 | |
344 | 29 Sargon the Great |
30 Founder of Akkadian Empire | |
31 | |
32 .. twenty-third century BC | |
337 | 33 |
34 In between | |
35 ========== | |
36 | |
37 .. image:: apple.jpg | |
344 | 38 :height: 300px |
337 | 39 |
344 | 40 Command-Line Interface |
41 Unlike the Akkadian Empire, | |
42 the CLI will never die. | |
337 | 43 |
351 | 44 Defining CLI |
45 ============ | |
344 | 46 |
47 - "Line-oriented command interpreter" | |
48 - "Command-line interface" | |
49 - "Shell" | |
337 | 50 |
345
6fe1e75e3a67
transcript test wasn't running pre and post cmd hooks
catherine@Drou
parents:
344
diff
changeset
|
51 1. Accepts free text input at prompt |
6fe1e75e3a67
transcript test wasn't running pre and post cmd hooks
catherine@Drou
parents:
344
diff
changeset
|
52 2. Outputs lines of text |
6fe1e75e3a67
transcript test wasn't running pre and post cmd hooks
catherine@Drou
parents:
344
diff
changeset
|
53 3. (repeat) |
337 | 54 |
55 Examples | |
56 ======== | |
57 | |
344 | 58 * Bash, Korn, zsh |
59 * Python shell | |
60 * screen | |
61 * Zork | |
62 * SQL clients: psql, SQL*\Plus, mysql... | |
63 * ed | |
64 | |
65 .. ``ed`` proves that CLI is sometimes the wrong answer. | |
337 | 66 |
67 != Command Line Utilities | |
68 ========================= | |
69 | |
345
6fe1e75e3a67
transcript test wasn't running pre and post cmd hooks
catherine@Drou
parents:
344
diff
changeset
|
70 1. Accepts arguments at invocation |
6fe1e75e3a67
transcript test wasn't running pre and post cmd hooks
catherine@Drou
parents:
344
diff
changeset
|
71 2. executes |
6fe1e75e3a67
transcript test wasn't running pre and post cmd hooks
catherine@Drou
parents:
344
diff
changeset
|
72 3. terminates |
337 | 73 |
344 | 74 Examples |
75 -------- | |
76 * ls | |
77 * grep | |
78 * ping | |
337 | 79 |
344 | 80 Use ``sys.argv``, ``optparse`` |
337 | 81 |
344 | 82 != "Text User Interfaces", "Consoles" |
83 ===================================== | |
337 | 84 |
344 | 85 * Use entire (session) screen |
86 * I/O is *not* line-by-line | |
337 | 87 |
88 .. image:: urwid.png | |
89 :height: 300px | |
90 | |
344 | 91 Use ``curses``, ``urwid`` |
337 | 92 |
351 | 93 Priorities |
94 ========== | |
337 | 95 |
351 | 96 .. image:: strategy.png |
344 | 97 :height: 300px |
98 | |
351 | 99 A ``cmd`` app: pirate.py |
100 ======================== | |
337 | 101 |
102 :: | |
103 | |
104 from cmd import Cmd | |
105 | |
106 class Pirate(Cmd): | |
107 pass | |
108 | |
109 pirate = Pirate() | |
110 pirate.cmdloop() | |
111 | |
351 | 112 .. Nothing here... but history and help |
344 | 113 |
114 .. ctrl-r for bash-style history | |
115 | |
116 Fundamental prrrinciple | |
117 ======================= | |
118 | |
351 | 119 .. class:: huge |
344 | 120 |
351 | 121 :: |
122 | |
123 (Cmd) foo a b c | |
344 | 124 |
125 ``self.do_foo('a b c')`` | |
126 | |
127 ``do_``-methods: pirate2.py | |
128 =========================== | |
129 | |
130 :: | |
131 | |
132 class Pirate(Cmd): | |
351 | 133 gold = 3 |
344 | 134 def do_loot(self, arg): |
135 'Seize booty frrrom a passing ship.' | |
136 self.gold += 1 | |
137 print('Now we gots {0} doubloons'.format(self.gold)) | |
138 def do_drink(self, arg): | |
139 'Drown your sorrrows in rrrum.' | |
140 self.gold -= 1 | |
141 print('Now we gots {0} doubloons'.format(self.gold)) | |
142 | |
143 .. do_methods; more help | |
144 | |
145 Hooks | |
146 ===== | |
147 | |
148 .. image:: hook.jpeg | |
149 :height: 300px | |
150 | |
151 Hooks: pirate3.py | |
152 ================= | |
153 | |
154 :: | |
337 | 155 |
351 | 156 def do_loot(self, arg): |
157 'Seize booty from a passing ship.' | |
158 self.gold += 1 | |
159 def do_drink(self, arg): | |
160 'Drown your sorrrows in rrrum.' | |
161 self.gold -= 1 | |
162 def precmd(self, line): | |
163 self.initial_gold = self.gold | |
164 return line | |
165 def postcmd(self, stop, line): | |
166 if self.gold != self.initial_gold: | |
167 print('Now we gots {0} doubloons'.format(self.gold)) | |
344 | 168 |
169 Arguments: pirate4.py | |
170 ===================== | |
171 | |
172 :: | |
173 | |
174 def do_drink(self, arg): | |
175 '''Drown your sorrrows in rrrum. | |
176 | |
177 drink [n] - drink [n] barrel[s] o' rum.''' | |
178 try: | |
179 self.gold -= int(arg) | |
180 except: | |
181 if arg: | |
345
6fe1e75e3a67
transcript test wasn't running pre and post cmd hooks
catherine@Drou
parents:
344
diff
changeset
|
182 print('''What's "{0}"? I'll take rrrum.''' |
6fe1e75e3a67
transcript test wasn't running pre and post cmd hooks
catherine@Drou
parents:
344
diff
changeset
|
183 .format(arg)) |
344 | 184 self.gold -= 1 |
185 | |
186 quitting: pirate5.py | |
187 ==================== | |
188 | |
189 :: | |
190 | |
351 | 191 def postcmd(self, stop, line): |
192 if self.gold != self.initial_gold: | |
193 print('Now we gots {0} doubloons'.format(self.gold)) | |
344 | 194 if self.gold < 0: |
351 | 195 print("Off to debtorrr's prison. Game overrr.") |
344 | 196 return True |
197 return stop | |
198 def do_quit(self, arg): | |
199 print("Quiterrr!") | |
351 | 200 return True |
344 | 201 |
202 prompts and defaults: pirate6.py | |
203 ================================ | |
204 | |
205 :: | |
206 | |
207 prompt = 'arrr> ' | |
208 def default(self, line): | |
345
6fe1e75e3a67
transcript test wasn't running pre and post cmd hooks
catherine@Drou
parents:
344
diff
changeset
|
209 print('What mean ye by "{0}"?' |
6fe1e75e3a67
transcript test wasn't running pre and post cmd hooks
catherine@Drou
parents:
344
diff
changeset
|
210 .format(line)) |
351 | 211 |
345
6fe1e75e3a67
transcript test wasn't running pre and post cmd hooks
catherine@Drou
parents:
344
diff
changeset
|
212 cmd2 |
6fe1e75e3a67
transcript test wasn't running pre and post cmd hooks
catherine@Drou
parents:
344
diff
changeset
|
213 ==== |
6fe1e75e3a67
transcript test wasn't running pre and post cmd hooks
catherine@Drou
parents:
344
diff
changeset
|
214 |
351 | 215 .. image:: schematic.png |
216 :height: 300px | |
345
6fe1e75e3a67
transcript test wasn't running pre and post cmd hooks
catherine@Drou
parents:
344
diff
changeset
|
217 |
347
432ccab7c6c8
going to try moving output redirection to outside precmd, postcmd hooks
catherine@Drou
parents:
346
diff
changeset
|
218 Absolutely free |
432ccab7c6c8
going to try moving output redirection to outside precmd, postcmd hooks
catherine@Drou
parents:
346
diff
changeset
|
219 =============== |
345
6fe1e75e3a67
transcript test wasn't running pre and post cmd hooks
catherine@Drou
parents:
344
diff
changeset
|
220 |
6fe1e75e3a67
transcript test wasn't running pre and post cmd hooks
catherine@Drou
parents:
344
diff
changeset
|
221 * Script files |
6fe1e75e3a67
transcript test wasn't running pre and post cmd hooks
catherine@Drou
parents:
344
diff
changeset
|
222 * Commands at invocation |
346 | 223 * Output redirection |
345
6fe1e75e3a67
transcript test wasn't running pre and post cmd hooks
catherine@Drou
parents:
344
diff
changeset
|
224 * Python |
346 | 225 * Transcript-based testing |
347
432ccab7c6c8
going to try moving output redirection to outside precmd, postcmd hooks
catherine@Drou
parents:
346
diff
changeset
|
226 |
432ccab7c6c8
going to try moving output redirection to outside precmd, postcmd hooks
catherine@Drou
parents:
346
diff
changeset
|
227 But wait, there's more |
432ccab7c6c8
going to try moving output redirection to outside precmd, postcmd hooks
catherine@Drou
parents:
346
diff
changeset
|
228 ====================== |
432ccab7c6c8
going to try moving output redirection to outside precmd, postcmd hooks
catherine@Drou
parents:
346
diff
changeset
|
229 |
432ccab7c6c8
going to try moving output redirection to outside precmd, postcmd hooks
catherine@Drou
parents:
346
diff
changeset
|
230 * Abbreviated commands |
432ccab7c6c8
going to try moving output redirection to outside precmd, postcmd hooks
catherine@Drou
parents:
346
diff
changeset
|
231 * Shell commands |
432ccab7c6c8
going to try moving output redirection to outside precmd, postcmd hooks
catherine@Drou
parents:
346
diff
changeset
|
232 * Quitting |
432ccab7c6c8
going to try moving output redirection to outside precmd, postcmd hooks
catherine@Drou
parents:
346
diff
changeset
|
233 * Timing |
432ccab7c6c8
going to try moving output redirection to outside precmd, postcmd hooks
catherine@Drou
parents:
346
diff
changeset
|
234 * Echo |
432ccab7c6c8
going to try moving output redirection to outside precmd, postcmd hooks
catherine@Drou
parents:
346
diff
changeset
|
235 * Debug |
345
6fe1e75e3a67
transcript test wasn't running pre and post cmd hooks
catherine@Drou
parents:
344
diff
changeset
|
236 |
347
432ccab7c6c8
going to try moving output redirection to outside precmd, postcmd hooks
catherine@Drou
parents:
346
diff
changeset
|
237 For a few keystrokes more... |
432ccab7c6c8
going to try moving output redirection to outside precmd, postcmd hooks
catherine@Drou
parents:
346
diff
changeset
|
238 ============================ |
432ccab7c6c8
going to try moving output redirection to outside precmd, postcmd hooks
catherine@Drou
parents:
346
diff
changeset
|
239 |
432ccab7c6c8
going to try moving output redirection to outside precmd, postcmd hooks
catherine@Drou
parents:
346
diff
changeset
|
240 * Default to shell |
432ccab7c6c8
going to try moving output redirection to outside precmd, postcmd hooks
catherine@Drou
parents:
346
diff
changeset
|
241 * Color output |
432ccab7c6c8
going to try moving output redirection to outside precmd, postcmd hooks
catherine@Drou
parents:
346
diff
changeset
|
242 * Shortcuts |
351 | 243 * Multiline commands |
244 * Environment variables | |
245 | |
246 Minor changes: pirate7.py | |
247 ========================= | |
248 | |
249 :: | |
347
432ccab7c6c8
going to try moving output redirection to outside precmd, postcmd hooks
catherine@Drou
parents:
346
diff
changeset
|
250 |
351 | 251 default_to_shell = True |
252 multilineCommands = ['sing'] | |
253 terminators = Cmd.terminators + ['...'] | |
356 | 254 songcolor = 'blue' |
255 settable = Cmd.settable + 'songcolor Color to ``sing`` in (red/blue/green/cyan/magenta, bold, underline)' | |
256 Cmd.shortcuts.update({'~': 'sing'}) | |
351 | 257 def do_sing(self, arg): |
356 | 258 print(self.colorize(arg, self.songcolor)) |
259 | |
260 Now how much would you pay? | |
261 =========================== | |
262 | |
263 * options / flags | |
264 * Quiet (suppress feedback) | |
265 * BASH-style ``select`` | |
266 * Parsing: terminators, suffixes | |
351 | 267 |
268 Options: pirate8.py | |
269 =================== | |
270 | |
271 :: | |
347
432ccab7c6c8
going to try moving output redirection to outside precmd, postcmd hooks
catherine@Drou
parents:
346
diff
changeset
|
272 |
351 | 273 @options([make_option('--ho', type='int', help="How often to chant 'ho'", default=2), |
274 make_option('-c', '--commas', action="store_true", help="Interspers commas")]) | |
275 def do_yo(self, arg, opts): | |
276 chant = ['yo'] + ['ho'] * opts.ho | |
277 if opts.commas: | |
278 separator = ', ' | |
279 else: | |
280 separator = ' ' | |
352 | 281 chant = separator.join(chant) |
282 print('{0} and a bottle of {1}'.format(chant, arg)) | |
351 | 283 |