Mercurial > python-cmd2
annotate docs/pycon2010/pycon2010.rst @ 362:18e8487be095
try again to upload docs
author | cat@eee |
---|---|
date | Thu, 18 Feb 2010 13:08:15 -0500 |
parents | ce4448ca4088 |
children | 2b2836b783be |
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 | |
362 | 18 :height: 250px |
337 | 19 |
20 But first... | |
21 ============ | |
22 | |
23 .. image:: sargon.jpg | |
362 | 24 :height: 250px |
344 | 25 |
351 | 26 .. image:: akkad.png |
362 | 27 :height: 250px |
351 | 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 | |
362 | 38 :height: 250px |
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 | |
362 | 70 (``ls``, ``grep``, ``ping``, etc.) |
71 | |
345
6fe1e75e3a67
transcript test wasn't running pre and post cmd hooks
catherine@Drou
parents:
344
diff
changeset
|
72 1. Accepts arguments at invocation |
6fe1e75e3a67
transcript test wasn't running pre and post cmd hooks
catherine@Drou
parents:
344
diff
changeset
|
73 2. executes |
6fe1e75e3a67
transcript test wasn't running pre and post cmd hooks
catherine@Drou
parents:
344
diff
changeset
|
74 3. terminates |
337 | 75 |
344 | 76 Use ``sys.argv``, ``optparse`` |
337 | 77 |
344 | 78 != "Text User Interfaces", "Consoles" |
79 ===================================== | |
337 | 80 |
344 | 81 * Use entire (session) screen |
82 * I/O is *not* line-by-line | |
337 | 83 |
84 .. image:: urwid.png | |
362 | 85 :height: 250px |
337 | 86 |
344 | 87 Use ``curses``, ``urwid`` |
337 | 88 |
351 | 89 Priorities |
90 ========== | |
337 | 91 |
351 | 92 .. image:: strategy.png |
362 | 93 :height: 250px |
344 | 94 |
351 | 95 A ``cmd`` app: pirate.py |
96 ======================== | |
337 | 97 |
98 :: | |
99 | |
100 from cmd import Cmd | |
101 | |
102 class Pirate(Cmd): | |
103 pass | |
104 | |
105 pirate = Pirate() | |
106 pirate.cmdloop() | |
107 | |
351 | 108 .. Nothing here... but history and help |
344 | 109 |
110 .. ctrl-r for bash-style history | |
111 | |
112 Fundamental prrrinciple | |
113 ======================= | |
114 | |
351 | 115 .. class:: huge |
344 | 116 |
351 | 117 :: |
118 | |
119 (Cmd) foo a b c | |
344 | 120 |
121 ``self.do_foo('a b c')`` | |
122 | |
123 ``do_``-methods: pirate2.py | |
124 =========================== | |
125 | |
126 :: | |
127 | |
128 class Pirate(Cmd): | |
351 | 129 gold = 3 |
344 | 130 def do_loot(self, arg): |
131 'Seize booty frrrom a passing ship.' | |
132 self.gold += 1 | |
133 print('Now we gots {0} doubloons'.format(self.gold)) | |
134 def do_drink(self, arg): | |
135 'Drown your sorrrows in rrrum.' | |
136 self.gold -= 1 | |
137 print('Now we gots {0} doubloons'.format(self.gold)) | |
138 | |
139 .. do_methods; more help | |
140 | |
141 Hooks | |
142 ===== | |
143 | |
144 .. image:: hook.jpeg | |
362 | 145 :height: 250px |
344 | 146 |
147 Hooks: pirate3.py | |
148 ================= | |
149 | |
150 :: | |
337 | 151 |
351 | 152 def do_loot(self, arg): |
153 'Seize booty from a passing ship.' | |
154 self.gold += 1 | |
155 def do_drink(self, arg): | |
156 'Drown your sorrrows in rrrum.' | |
157 self.gold -= 1 | |
158 def precmd(self, line): | |
159 self.initial_gold = self.gold | |
160 return line | |
161 def postcmd(self, stop, line): | |
162 if self.gold != self.initial_gold: | |
163 print('Now we gots {0} doubloons'.format(self.gold)) | |
344 | 164 |
165 Arguments: pirate4.py | |
166 ===================== | |
167 | |
168 :: | |
169 | |
170 def do_drink(self, arg): | |
171 '''Drown your sorrrows in rrrum. | |
172 | |
173 drink [n] - drink [n] barrel[s] o' rum.''' | |
174 try: | |
175 self.gold -= int(arg) | |
176 except: | |
177 if arg: | |
345
6fe1e75e3a67
transcript test wasn't running pre and post cmd hooks
catherine@Drou
parents:
344
diff
changeset
|
178 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
|
179 .format(arg)) |
344 | 180 self.gold -= 1 |
181 | |
182 quitting: pirate5.py | |
183 ==================== | |
184 | |
185 :: | |
186 | |
351 | 187 def postcmd(self, stop, line): |
188 if self.gold != self.initial_gold: | |
189 print('Now we gots {0} doubloons'.format(self.gold)) | |
344 | 190 if self.gold < 0: |
351 | 191 print("Off to debtorrr's prison. Game overrr.") |
344 | 192 return True |
193 return stop | |
194 def do_quit(self, arg): | |
195 print("Quiterrr!") | |
351 | 196 return True |
344 | 197 |
198 prompts and defaults: pirate6.py | |
199 ================================ | |
200 | |
201 :: | |
202 | |
203 prompt = 'arrr> ' | |
204 def default(self, line): | |
345
6fe1e75e3a67
transcript test wasn't running pre and post cmd hooks
catherine@Drou
parents:
344
diff
changeset
|
205 print('What mean ye by "{0}"?' |
6fe1e75e3a67
transcript test wasn't running pre and post cmd hooks
catherine@Drou
parents:
344
diff
changeset
|
206 .format(line)) |
351 | 207 |
345
6fe1e75e3a67
transcript test wasn't running pre and post cmd hooks
catherine@Drou
parents:
344
diff
changeset
|
208 cmd2 |
6fe1e75e3a67
transcript test wasn't running pre and post cmd hooks
catherine@Drou
parents:
344
diff
changeset
|
209 ==== |
6fe1e75e3a67
transcript test wasn't running pre and post cmd hooks
catherine@Drou
parents:
344
diff
changeset
|
210 |
351 | 211 .. image:: schematic.png |
362 | 212 :height: 250px |
345
6fe1e75e3a67
transcript test wasn't running pre and post cmd hooks
catherine@Drou
parents:
344
diff
changeset
|
213 |
347
432ccab7c6c8
going to try moving output redirection to outside precmd, postcmd hooks
catherine@Drou
parents:
346
diff
changeset
|
214 Absolutely free |
432ccab7c6c8
going to try moving output redirection to outside precmd, postcmd hooks
catherine@Drou
parents:
346
diff
changeset
|
215 =============== |
345
6fe1e75e3a67
transcript test wasn't running pre and post cmd hooks
catherine@Drou
parents:
344
diff
changeset
|
216 |
6fe1e75e3a67
transcript test wasn't running pre and post cmd hooks
catherine@Drou
parents:
344
diff
changeset
|
217 * Script files |
6fe1e75e3a67
transcript test wasn't running pre and post cmd hooks
catherine@Drou
parents:
344
diff
changeset
|
218 * Commands at invocation |
346 | 219 * Output redirection |
345
6fe1e75e3a67
transcript test wasn't running pre and post cmd hooks
catherine@Drou
parents:
344
diff
changeset
|
220 * Python |
346 | 221 * Transcript-based testing |
347
432ccab7c6c8
going to try moving output redirection to outside precmd, postcmd hooks
catherine@Drou
parents:
346
diff
changeset
|
222 |
432ccab7c6c8
going to try moving output redirection to outside precmd, postcmd hooks
catherine@Drou
parents:
346
diff
changeset
|
223 But wait, there's more |
432ccab7c6c8
going to try moving output redirection to outside precmd, postcmd hooks
catherine@Drou
parents:
346
diff
changeset
|
224 ====================== |
432ccab7c6c8
going to try moving output redirection to outside precmd, postcmd hooks
catherine@Drou
parents:
346
diff
changeset
|
225 |
432ccab7c6c8
going to try moving output redirection to outside precmd, postcmd hooks
catherine@Drou
parents:
346
diff
changeset
|
226 * Abbreviated commands |
432ccab7c6c8
going to try moving output redirection to outside precmd, postcmd hooks
catherine@Drou
parents:
346
diff
changeset
|
227 * Shell commands |
432ccab7c6c8
going to try moving output redirection to outside precmd, postcmd hooks
catherine@Drou
parents:
346
diff
changeset
|
228 * Quitting |
432ccab7c6c8
going to try moving output redirection to outside precmd, postcmd hooks
catherine@Drou
parents:
346
diff
changeset
|
229 * Timing |
432ccab7c6c8
going to try moving output redirection to outside precmd, postcmd hooks
catherine@Drou
parents:
346
diff
changeset
|
230 * Echo |
432ccab7c6c8
going to try moving output redirection to outside precmd, postcmd hooks
catherine@Drou
parents:
346
diff
changeset
|
231 * Debug |
345
6fe1e75e3a67
transcript test wasn't running pre and post cmd hooks
catherine@Drou
parents:
344
diff
changeset
|
232 |
347
432ccab7c6c8
going to try moving output redirection to outside precmd, postcmd hooks
catherine@Drou
parents:
346
diff
changeset
|
233 For a few keystrokes more... |
432ccab7c6c8
going to try moving output redirection to outside precmd, postcmd hooks
catherine@Drou
parents:
346
diff
changeset
|
234 ============================ |
432ccab7c6c8
going to try moving output redirection to outside precmd, postcmd hooks
catherine@Drou
parents:
346
diff
changeset
|
235 |
432ccab7c6c8
going to try moving output redirection to outside precmd, postcmd hooks
catherine@Drou
parents:
346
diff
changeset
|
236 * Default to shell |
432ccab7c6c8
going to try moving output redirection to outside precmd, postcmd hooks
catherine@Drou
parents:
346
diff
changeset
|
237 * Color output |
432ccab7c6c8
going to try moving output redirection to outside precmd, postcmd hooks
catherine@Drou
parents:
346
diff
changeset
|
238 * Shortcuts |
351 | 239 * Multiline commands |
240 * Environment variables | |
241 | |
242 Minor changes: pirate7.py | |
243 ========================= | |
244 | |
245 :: | |
347
432ccab7c6c8
going to try moving output redirection to outside precmd, postcmd hooks
catherine@Drou
parents:
346
diff
changeset
|
246 |
351 | 247 default_to_shell = True |
248 multilineCommands = ['sing'] | |
249 terminators = Cmd.terminators + ['...'] | |
356 | 250 songcolor = 'blue' |
251 settable = Cmd.settable + 'songcolor Color to ``sing`` in (red/blue/green/cyan/magenta, bold, underline)' | |
252 Cmd.shortcuts.update({'~': 'sing'}) | |
351 | 253 def do_sing(self, arg): |
356 | 254 print(self.colorize(arg, self.songcolor)) |
255 | |
256 Now how much would you pay? | |
257 =========================== | |
258 | |
259 * options / flags | |
260 * Quiet (suppress feedback) | |
261 * BASH-style ``select`` | |
262 * Parsing: terminators, suffixes | |
351 | 263 |
264 Options: pirate8.py | |
265 =================== | |
266 | |
267 :: | |
347
432ccab7c6c8
going to try moving output redirection to outside precmd, postcmd hooks
catherine@Drou
parents:
346
diff
changeset
|
268 |
351 | 269 @options([make_option('--ho', type='int', help="How often to chant 'ho'", default=2), |
270 make_option('-c', '--commas', action="store_true", help="Interspers commas")]) | |
271 def do_yo(self, arg, opts): | |
272 chant = ['yo'] + ['ho'] * opts.ho | |
273 if opts.commas: | |
274 separator = ', ' | |
275 else: | |
276 separator = ' ' | |
352 | 277 chant = separator.join(chant) |
278 print('{0} and a bottle of {1}'.format(chant, arg)) | |
362 | 279 |
280 Serious example: sqlpython | |
281 ========================== | |
282 | |
283 ``cmd``-based app by Luca Canali @ CERN | |
284 | |
285 Replacement for Oracle SQL/*Plus | |
286 | |
287 Now ``cmd2``-based; postgreSQL; MySQL | |
357 | 288 |
362 | 289 sqlpython features |
290 ================== | |
291 | |
292 Everything in ``cmd2`` | |
357 | 293 |
362 | 294 Multi connections |
295 | |
296 ls, grep | |
297 | |
298 Output to html, csv, inserts, bar graphs | |
357 | 299 |
300 |