Mercurial > python-cmd2
annotate docs/pycon2010/pycon2010.rst @ 374:89e38f922c25
font sizes up
author | cat@eee |
---|---|
date | Sat, 20 Feb 2010 00:11:34 -0500 |
parents | a381f8dd3a45 |
children | 6cc12ad6e616 |
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 | |
374 | 7 :slides: http://pypi.python.org/pypi/cmd2 |
337 | 8 |
9 Web 2.0 | |
10 ======= | |
11 | |
12 .. image:: web-2-0-logos.gif | |
372 | 13 :height: 350px |
337 | 14 |
15 But first... | |
16 ============ | |
17 | |
18 .. image:: sargon.jpg | |
362 | 19 :height: 250px |
344 | 20 |
351 | 21 .. image:: akkad.png |
362 | 22 :height: 250px |
351 | 23 |
344 | 24 Sargon the Great |
25 Founder of Akkadian Empire | |
26 | |
27 .. twenty-third century BC | |
337 | 28 |
29 In between | |
30 ========== | |
31 | |
32 .. image:: apple.jpg | |
362 | 33 :height: 250px |
337 | 34 |
344 | 35 Command-Line Interface |
36 Unlike the Akkadian Empire, | |
37 the CLI will never die. | |
337 | 38 |
351 | 39 Defining CLI |
40 ============ | |
372 | 41 |
42 Also known as | |
344 | 43 |
44 - "Line-oriented command interpreter" | |
45 - "Command-line interface" | |
46 - "Shell" | |
337 | 47 |
345
6fe1e75e3a67
transcript test wasn't running pre and post cmd hooks
catherine@Drou
parents:
344
diff
changeset
|
48 1. Accepts free text input at prompt |
6fe1e75e3a67
transcript test wasn't running pre and post cmd hooks
catherine@Drou
parents:
344
diff
changeset
|
49 2. Outputs lines of text |
6fe1e75e3a67
transcript test wasn't running pre and post cmd hooks
catherine@Drou
parents:
344
diff
changeset
|
50 3. (repeat) |
337 | 51 |
52 Examples | |
53 ======== | |
54 | |
374 | 55 .. class:: big |
56 | |
57 * Bash, Korn, zsh | |
58 * Python shell | |
59 * screen | |
60 * Zork | |
61 * SQL clients: psql, SQL*\Plus, mysql... | |
62 * ed | |
344 | 63 |
64 .. ``ed`` proves that CLI is sometimes the wrong answer. | |
337 | 65 |
66 != Command Line Utilities | |
67 ========================= | |
68 | |
374 | 69 .. class:: big |
70 | |
71 (``ls``, ``grep``, ``ping``, etc.) | |
362 | 72 |
374 | 73 1. Accept arguments at invocation |
74 2. execute | |
75 3. terminate | |
337 | 76 |
374 | 77 Use ``sys.argv``, ``optparse`` |
337 | 78 |
374 | 79 !="Text User Interface" |
80 ======================= | |
337 | 81 |
344 | 82 * Use entire (session) screen |
83 * I/O is *not* line-by-line | |
372 | 84 * See ``curses``, ``urwid`` |
337 | 85 |
86 .. image:: urwid.png | |
362 | 87 :height: 250px |
337 | 88 |
89 | |
372 | 90 Decide your priorities |
91 ====================== | |
337 | 92 |
351 | 93 .. image:: strategy.png |
372 | 94 :height: 350px |
344 | 95 |
351 | 96 A ``cmd`` app: pirate.py |
97 ======================== | |
337 | 98 |
99 :: | |
100 | |
101 from cmd import Cmd | |
102 | |
103 class Pirate(Cmd): | |
104 pass | |
105 | |
106 pirate = Pirate() | |
107 pirate.cmdloop() | |
108 | |
351 | 109 .. Nothing here... but history and help |
344 | 110 |
111 .. ctrl-r for bash-style history | |
112 | |
113 Fundamental prrrinciple | |
114 ======================= | |
115 | |
351 | 116 .. class:: huge |
117 | |
374 | 118 ``(Cmd) foo a b c`` |
344 | 119 |
374 | 120 becomes |
121 | |
122 ``self.do_foo('a b c')`` | |
344 | 123 |
124 ``do_``-methods: pirate2.py | |
125 =========================== | |
126 | |
127 :: | |
128 | |
129 class Pirate(Cmd): | |
351 | 130 gold = 3 |
344 | 131 def do_loot(self, arg): |
132 'Seize booty frrrom a passing ship.' | |
133 self.gold += 1 | |
374 | 134 print('Now we gots {0} doubloons' |
135 .format(self.gold)) | |
344 | 136 def do_drink(self, arg): |
137 'Drown your sorrrows in rrrum.' | |
138 self.gold -= 1 | |
374 | 139 print('Now we gots {0} doubloons' |
140 .format(self.gold)) | |
344 | 141 |
142 .. do_methods; more help | |
143 | |
144 Hooks | |
145 ===== | |
146 | |
363 | 147 .. image:: hook.jpg |
362 | 148 :height: 250px |
344 | 149 |
374 | 150 :: |
151 | |
152 self.preloop() | |
153 self.postloop() | |
154 self.precmd(line) | |
155 self.postcmd(stop, line) | |
363 | 156 |
344 | 157 Hooks: pirate3.py |
158 ================= | |
159 | |
160 :: | |
337 | 161 |
351 | 162 def do_loot(self, arg): |
163 'Seize booty from a passing ship.' | |
164 self.gold += 1 | |
165 def do_drink(self, arg): | |
166 'Drown your sorrrows in rrrum.' | |
167 self.gold -= 1 | |
168 def precmd(self, line): | |
169 self.initial_gold = self.gold | |
170 return line | |
171 def postcmd(self, stop, line): | |
172 if self.gold != self.initial_gold: | |
374 | 173 print('Now we gots {0} doubloons' |
174 .format(self.gold)) | |
344 | 175 |
176 Arguments: pirate4.py | |
177 ===================== | |
178 | |
179 :: | |
180 | |
181 def do_drink(self, arg): | |
182 '''Drown your sorrrows in rrrum. | |
183 | |
184 drink [n] - drink [n] barrel[s] o' rum.''' | |
185 try: | |
186 self.gold -= int(arg) | |
187 except: | |
188 if arg: | |
345
6fe1e75e3a67
transcript test wasn't running pre and post cmd hooks
catherine@Drou
parents:
344
diff
changeset
|
189 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
|
190 .format(arg)) |
344 | 191 self.gold -= 1 |
192 | |
193 quitting: pirate5.py | |
194 ==================== | |
195 | |
196 :: | |
197 | |
351 | 198 def postcmd(self, stop, line): |
199 if self.gold != self.initial_gold: | |
374 | 200 print('Now we gots {0} doubloons' |
201 .format(self.gold)) | |
344 | 202 if self.gold < 0: |
374 | 203 print("Off to debtorrr's prison.") |
204 stop = True | |
344 | 205 return stop |
206 def do_quit(self, arg): | |
207 print("Quiterrr!") | |
351 | 208 return True |
344 | 209 |
374 | 210 prompts, defaults: pirate6.py |
211 ============================= | |
344 | 212 |
213 :: | |
214 | |
215 prompt = 'arrr> ' | |
216 def default(self, line): | |
345
6fe1e75e3a67
transcript test wasn't running pre and post cmd hooks
catherine@Drou
parents:
344
diff
changeset
|
217 print('What mean ye by "{0}"?' |
6fe1e75e3a67
transcript test wasn't running pre and post cmd hooks
catherine@Drou
parents:
344
diff
changeset
|
218 .format(line)) |
372 | 219 |
220 Other CLI packages | |
221 ================== | |
374 | 222 |
223 .. class:: big | |
224 | |
225 * CmdLoop | |
226 * cly | |
227 * CMdO | |
228 * pycopia | |
229 * cmdlin | |
230 * cmd2 | |
372 | 231 |
232 Demo | |
233 ==== | |
234 | |
374 | 235 .. class:: huge |
236 | |
237 Convert ``cmd`` app to ``cmd2`` | |
372 | 238 |
345
6fe1e75e3a67
transcript test wasn't running pre and post cmd hooks
catherine@Drou
parents:
344
diff
changeset
|
239 cmd2 |
6fe1e75e3a67
transcript test wasn't running pre and post cmd hooks
catherine@Drou
parents:
344
diff
changeset
|
240 ==== |
6fe1e75e3a67
transcript test wasn't running pre and post cmd hooks
catherine@Drou
parents:
344
diff
changeset
|
241 |
351 | 242 .. image:: schematic.png |
372 | 243 :height: 350px |
345
6fe1e75e3a67
transcript test wasn't running pre and post cmd hooks
catherine@Drou
parents:
344
diff
changeset
|
244 |
373 | 245 As you wish, Guido |
246 ================== | |
247 | |
374 | 248 .. class:: huge |
249 | |
250 Python 3 compatible | |
373 | 251 |
252 (um, mostly) | |
253 | |
347
432ccab7c6c8
going to try moving output redirection to outside precmd, postcmd hooks
catherine@Drou
parents:
346
diff
changeset
|
254 Absolutely free |
432ccab7c6c8
going to try moving output redirection to outside precmd, postcmd hooks
catherine@Drou
parents:
346
diff
changeset
|
255 =============== |
345
6fe1e75e3a67
transcript test wasn't running pre and post cmd hooks
catherine@Drou
parents:
344
diff
changeset
|
256 |
374 | 257 Script files |
258 | |
259 Commands at invocation | |
260 | |
261 Output redirection | |
262 | |
263 Python | |
264 | |
265 Transcript testing | |
347
432ccab7c6c8
going to try moving output redirection to outside precmd, postcmd hooks
catherine@Drou
parents:
346
diff
changeset
|
266 |
432ccab7c6c8
going to try moving output redirection to outside precmd, postcmd hooks
catherine@Drou
parents:
346
diff
changeset
|
267 But wait, there's more |
432ccab7c6c8
going to try moving output redirection to outside precmd, postcmd hooks
catherine@Drou
parents:
346
diff
changeset
|
268 ====================== |
432ccab7c6c8
going to try moving output redirection to outside precmd, postcmd hooks
catherine@Drou
parents:
346
diff
changeset
|
269 |
432ccab7c6c8
going to try moving output redirection to outside precmd, postcmd hooks
catherine@Drou
parents:
346
diff
changeset
|
270 * Abbreviated commands |
432ccab7c6c8
going to try moving output redirection to outside precmd, postcmd hooks
catherine@Drou
parents:
346
diff
changeset
|
271 * Shell commands |
432ccab7c6c8
going to try moving output redirection to outside precmd, postcmd hooks
catherine@Drou
parents:
346
diff
changeset
|
272 * Quitting |
432ccab7c6c8
going to try moving output redirection to outside precmd, postcmd hooks
catherine@Drou
parents:
346
diff
changeset
|
273 * Timing |
432ccab7c6c8
going to try moving output redirection to outside precmd, postcmd hooks
catherine@Drou
parents:
346
diff
changeset
|
274 * Echo |
432ccab7c6c8
going to try moving output redirection to outside precmd, postcmd hooks
catherine@Drou
parents:
346
diff
changeset
|
275 * Debug |
345
6fe1e75e3a67
transcript test wasn't running pre and post cmd hooks
catherine@Drou
parents:
344
diff
changeset
|
276 |
351 | 277 Minor changes: pirate7.py |
278 ========================= | |
279 | |
280 :: | |
347
432ccab7c6c8
going to try moving output redirection to outside precmd, postcmd hooks
catherine@Drou
parents:
346
diff
changeset
|
281 |
351 | 282 default_to_shell = True |
283 multilineCommands = ['sing'] | |
284 terminators = Cmd.terminators + ['...'] | |
356 | 285 songcolor = 'blue' |
286 settable = Cmd.settable + 'songcolor Color to ``sing`` in (red/blue/green/cyan/magenta, bold, underline)' | |
287 Cmd.shortcuts.update({'~': 'sing'}) | |
351 | 288 def do_sing(self, arg): |
356 | 289 print(self.colorize(arg, self.songcolor)) |
290 | |
291 Now how much would you pay? | |
292 =========================== | |
293 | |
374 | 294 options / flags |
295 | |
296 Quiet (suppress feedback) | |
297 | |
298 BASH-style ``select`` | |
299 | |
300 Parsing: terminators, suffixes | |
351 | 301 |
302 Options: pirate8.py | |
303 =================== | |
304 | |
305 :: | |
347
432ccab7c6c8
going to try moving output redirection to outside precmd, postcmd hooks
catherine@Drou
parents:
346
diff
changeset
|
306 |
351 | 307 def do_yo(self, arg, opts): |
308 chant = ['yo'] + ['ho'] * opts.ho | |
372 | 309 separator = ', ' if opts.commas else ' ' |
352 | 310 chant = separator.join(chant) |
372 | 311 print('{0} and a bottle of {1}' |
312 .format(chant, arg)) | |
362 | 313 |
314 Serious example: sqlpython | |
315 ========================== | |
316 | |
374 | 317 .. class:: big |
318 | |
319 ``cmd``-based app by Luca Canali @ CERN | |
362 | 320 |
374 | 321 Replacement for Oracle SQL\*Plus |
362 | 322 |
374 | 323 Now ``cmd2``-based; postgreSQL; MySQL |
357 | 324 |
362 | 325 sqlpython features |
326 ================== | |
327 | |
374 | 328 .. class:: big |
329 | |
330 * from ``cmd2``: scripts, redirection, | |
331 py, etc. | |
332 * multiple connections | |
333 * UNIX: ls, cat, grep | |
334 * Special output | |
357 | 335 |
372 | 336 File reporter |
337 ============= | |
338 | |
374 | 339 .. class:: huge |
340 | |
341 Gather info: Python | |
362 | 342 |
374 | 343 Store: postgresql |
372 | 344 |
374 | 345 Report: html |
362 | 346 |
372 | 347 Thank you |
348 ========= | |
357 | 349 |
374 | 350 .. class:: big |
351 | |
352 http://pypi.python.org/pypi/cmd2 | |
372 | 353 |
374 | 354 http://catherinedevlin.blogspot.com |
372 | 355 |
374 | 356 http://catherinedevlin.pythoneers.com |
357 | 357 |
363 | 358 |