Mercurial > python-cmd2
annotate docs/pycon2010/pycon2010.rst @ 346:49dd1ce6cfd6
quitting after invocation commands
author | catherine@Drou |
---|---|
date | Tue, 16 Feb 2010 15:47:17 -0500 |
parents | 6fe1e75e3a67 |
children | 432ccab7c6c8 |
rev | line source |
---|---|
344 | 1 ===== |
2 Title | |
3 ===== | |
337 | 4 |
5 Web 2.0 | |
6 ======= | |
7 | |
8 .. image:: web-2-0-logos.gif | |
344 | 9 :height: 300px |
337 | 10 |
11 But first... | |
12 ============ | |
13 | |
14 .. image:: sargon.jpg | |
344 | 15 :height: 300px |
16 | |
17 Sargon the Great | |
18 Founder of Akkadian Empire | |
19 | |
20 .. twenty-third century BC | |
337 | 21 |
22 In between | |
23 ========== | |
24 | |
25 .. image:: apple.jpg | |
344 | 26 :height: 300px |
337 | 27 |
344 | 28 Command-Line Interface |
29 Unlike the Akkadian Empire, | |
30 the CLI will never die. | |
337 | 31 |
32 Defining | |
33 ======== | |
344 | 34 |
35 - "Line-oriented command interpreter" | |
36 - "Command-line interface" | |
37 - "Shell" | |
337 | 38 |
345
6fe1e75e3a67
transcript test wasn't running pre and post cmd hooks
catherine@Drou
parents:
344
diff
changeset
|
39 1. Accepts free text input at prompt |
6fe1e75e3a67
transcript test wasn't running pre and post cmd hooks
catherine@Drou
parents:
344
diff
changeset
|
40 2. Outputs lines of text |
6fe1e75e3a67
transcript test wasn't running pre and post cmd hooks
catherine@Drou
parents:
344
diff
changeset
|
41 3. (repeat) |
337 | 42 |
43 Examples | |
44 ======== | |
45 | |
344 | 46 * Bash, Korn, zsh |
47 * Python shell | |
48 * screen | |
49 * Zork | |
50 * SQL clients: psql, SQL*\Plus, mysql... | |
51 * ed | |
52 | |
53 .. ``ed`` proves that CLI is sometimes the wrong answer. | |
337 | 54 |
55 != Command Line Utilities | |
56 ========================= | |
57 | |
345
6fe1e75e3a67
transcript test wasn't running pre and post cmd hooks
catherine@Drou
parents:
344
diff
changeset
|
58 1. Accepts arguments at invocation |
6fe1e75e3a67
transcript test wasn't running pre and post cmd hooks
catherine@Drou
parents:
344
diff
changeset
|
59 2. executes |
6fe1e75e3a67
transcript test wasn't running pre and post cmd hooks
catherine@Drou
parents:
344
diff
changeset
|
60 3. terminates |
337 | 61 |
344 | 62 Examples |
63 -------- | |
64 * ls | |
65 * grep | |
66 * ping | |
337 | 67 |
344 | 68 Use ``sys.argv``, ``optparse`` |
337 | 69 |
344 | 70 != "Text User Interfaces", "Consoles" |
71 ===================================== | |
337 | 72 |
344 | 73 * Use entire (session) screen |
74 * I/O is *not* line-by-line | |
337 | 75 |
76 .. image:: urwid.png | |
77 :height: 300px | |
78 | |
344 | 79 Use ``curses``, ``urwid`` |
337 | 80 |
344 | 81 Tradeoff |
82 ======== | |
337 | 83 |
344 | 84 .. image:: ease.png |
85 :height: 300px | |
86 | |
337 | 87 pirate.py |
88 ========= | |
89 | |
90 :: | |
91 | |
92 from cmd import Cmd | |
93 | |
94 class Pirate(Cmd): | |
95 pass | |
96 | |
97 pirate = Pirate() | |
98 pirate.cmdloop() | |
99 | |
344 | 100 Nothing here... but history and help |
101 | |
102 .. ctrl-r for bash-style history | |
103 | |
104 Fundamental prrrinciple | |
105 ======================= | |
106 | |
107 .. class: huge | |
108 | |
109 ``foo a b c`` -> | |
110 | |
111 ``self.do_foo('a b c')`` | |
112 | |
113 ``do_``-methods: pirate2.py | |
114 =========================== | |
115 | |
116 :: | |
117 | |
118 class Pirate(Cmd): | |
119 gold = 10 | |
120 def do_loot(self, arg): | |
121 'Seize booty frrrom a passing ship.' | |
122 self.gold += 1 | |
123 print('Now we gots {0} doubloons'.format(self.gold)) | |
124 def do_drink(self, arg): | |
125 'Drown your sorrrows in rrrum.' | |
126 self.gold -= 1 | |
127 print('Now we gots {0} doubloons'.format(self.gold)) | |
128 | |
129 .. do_methods; more help | |
130 | |
131 Hooks | |
132 ===== | |
133 | |
134 .. image:: hook.jpeg | |
135 :height: 300px | |
136 | |
137 Hooks: pirate3.py | |
138 ================= | |
139 | |
140 :: | |
337 | 141 |
344 | 142 class Pirate(Cmd): |
143 gold = 3 | |
144 def do_loot(self, arg): | |
145 'Drown your sorrrows in rrrum.' | |
146 self.gold += 1 | |
147 def do_drink(self, arg): | |
148 'Drown your sorrrows in rrrum.' | |
149 self.gold -= 1 | |
150 def postcmd(self, stop, line): | |
151 print('Now we gots {0} doubloons'.format(self.gold)) | |
152 | |
153 Arguments: pirate4.py | |
154 ===================== | |
155 | |
156 :: | |
157 | |
158 def do_drink(self, arg): | |
159 '''Drown your sorrrows in rrrum. | |
160 | |
161 drink [n] - drink [n] barrel[s] o' rum.''' | |
162 try: | |
163 self.gold -= int(arg) | |
164 except: | |
165 if arg: | |
345
6fe1e75e3a67
transcript test wasn't running pre and post cmd hooks
catherine@Drou
parents:
344
diff
changeset
|
166 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
|
167 .format(arg)) |
344 | 168 self.gold -= 1 |
169 | |
170 quitting: pirate5.py | |
171 ==================== | |
172 | |
173 :: | |
174 | |
175 def postcmd(self, stop, line): | |
345
6fe1e75e3a67
transcript test wasn't running pre and post cmd hooks
catherine@Drou
parents:
344
diff
changeset
|
176 print('Now we gots {0} doubloons' |
6fe1e75e3a67
transcript test wasn't running pre and post cmd hooks
catherine@Drou
parents:
344
diff
changeset
|
177 .format(self.gold)) |
344 | 178 if self.gold < 0: |
179 print("Off to debtorrr's prrrison. Game overrr.") | |
180 return True | |
181 return stop | |
182 def do_quit(self, arg): | |
183 print("Quiterrr!") | |
184 return True | |
185 | |
186 prompts and defaults: pirate6.py | |
187 ================================ | |
188 | |
189 :: | |
190 | |
191 prompt = 'arrr> ' | |
192 def default(self, line): | |
345
6fe1e75e3a67
transcript test wasn't running pre and post cmd hooks
catherine@Drou
parents:
344
diff
changeset
|
193 print('What mean ye by "{0}"?' |
6fe1e75e3a67
transcript test wasn't running pre and post cmd hooks
catherine@Drou
parents:
344
diff
changeset
|
194 .format(line)) |
6fe1e75e3a67
transcript test wasn't running pre and post cmd hooks
catherine@Drou
parents:
344
diff
changeset
|
195 |
6fe1e75e3a67
transcript test wasn't running pre and post cmd hooks
catherine@Drou
parents:
344
diff
changeset
|
196 cmd2 |
6fe1e75e3a67
transcript test wasn't running pre and post cmd hooks
catherine@Drou
parents:
344
diff
changeset
|
197 ==== |
6fe1e75e3a67
transcript test wasn't running pre and post cmd hooks
catherine@Drou
parents:
344
diff
changeset
|
198 |
6fe1e75e3a67
transcript test wasn't running pre and post cmd hooks
catherine@Drou
parents:
344
diff
changeset
|
199 Third-party module in PyPI |
6fe1e75e3a67
transcript test wasn't running pre and post cmd hooks
catherine@Drou
parents:
344
diff
changeset
|
200 |
6fe1e75e3a67
transcript test wasn't running pre and post cmd hooks
catherine@Drou
parents:
344
diff
changeset
|
201 What you get |
6fe1e75e3a67
transcript test wasn't running pre and post cmd hooks
catherine@Drou
parents:
344
diff
changeset
|
202 ============ |
6fe1e75e3a67
transcript test wasn't running pre and post cmd hooks
catherine@Drou
parents:
344
diff
changeset
|
203 |
6fe1e75e3a67
transcript test wasn't running pre and post cmd hooks
catherine@Drou
parents:
344
diff
changeset
|
204 * Script files |
6fe1e75e3a67
transcript test wasn't running pre and post cmd hooks
catherine@Drou
parents:
344
diff
changeset
|
205 * Commands at invocation |
346 | 206 * Output redirection |
345
6fe1e75e3a67
transcript test wasn't running pre and post cmd hooks
catherine@Drou
parents:
344
diff
changeset
|
207 * Python |
346 | 208 * Transcript-based testing |
345
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 * Searchable command history |
6fe1e75e3a67
transcript test wasn't running pre and post cmd hooks
catherine@Drou
parents:
344
diff
changeset
|
211 * Quitting the application |
6fe1e75e3a67
transcript test wasn't running pre and post cmd hooks
catherine@Drou
parents:
344
diff
changeset
|
212 * Comments |
6fe1e75e3a67
transcript test wasn't running pre and post cmd hooks
catherine@Drou
parents:
344
diff
changeset
|
213 * Misc. pre-defined commands |
346 | 214 |
345
6fe1e75e3a67
transcript test wasn't running pre and post cmd hooks
catherine@Drou
parents:
344
diff
changeset
|
215 More |
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 |
6fe1e75e3a67
transcript test wasn't running pre and post cmd hooks
catherine@Drou
parents:
344
diff
changeset
|
218 * Case-insensitivity |
6fe1e75e3a67
transcript test wasn't running pre and post cmd hooks
catherine@Drou
parents:
344
diff
changeset
|
219 * Shortcuts |
6fe1e75e3a67
transcript test wasn't running pre and post cmd hooks
catherine@Drou
parents:
344
diff
changeset
|
220 * Default to shell |
6fe1e75e3a67
transcript test wasn't running pre and post cmd hooks
catherine@Drou
parents:
344
diff
changeset
|
221 * Timing |
6fe1e75e3a67
transcript test wasn't running pre and post cmd hooks
catherine@Drou
parents:
344
diff
changeset
|
222 * Echo |
6fe1e75e3a67
transcript test wasn't running pre and post cmd hooks
catherine@Drou
parents:
344
diff
changeset
|
223 * Debug |
6fe1e75e3a67
transcript test wasn't running pre and post cmd hooks
catherine@Drou
parents:
344
diff
changeset
|
224 * Other user-settable parameters |