comparison docs/pycon2010/pycon2010.rst @ 351:8a5bc9f5c28e

cmd2 docs
author catherine@Drou
date Tue, 16 Feb 2010 23:07:04 -0500
parents 432ccab7c6c8
children 798c7f32a960
comparison
equal deleted inserted replaced
350:1c91655d05f8 351:8a5bc9f5c28e
1 ===== 1 ================================================
2 Title 2 Easy command-line interpreters with cmd and cmd2
3 ===== 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
4 13
5 Web 2.0 14 Web 2.0
6 ======= 15 =======
7 16
8 .. image:: web-2-0-logos.gif 17 .. image:: web-2-0-logos.gif
12 ============ 21 ============
13 22
14 .. image:: sargon.jpg 23 .. image:: sargon.jpg
15 :height: 300px 24 :height: 300px
16 25
26 .. image:: akkad.png
27 :height: 300px
28
17 Sargon the Great 29 Sargon the Great
18 Founder of Akkadian Empire 30 Founder of Akkadian Empire
19 31
20 .. twenty-third century BC 32 .. twenty-third century BC
21 33
27 39
28 Command-Line Interface 40 Command-Line Interface
29 Unlike the Akkadian Empire, 41 Unlike the Akkadian Empire,
30 the CLI will never die. 42 the CLI will never die.
31 43
32 Defining 44 Defining CLI
33 ======== 45 ============
34 46
35 - "Line-oriented command interpreter" 47 - "Line-oriented command interpreter"
36 - "Command-line interface" 48 - "Command-line interface"
37 - "Shell" 49 - "Shell"
38 50
76 .. image:: urwid.png 88 .. image:: urwid.png
77 :height: 300px 89 :height: 300px
78 90
79 Use ``curses``, ``urwid`` 91 Use ``curses``, ``urwid``
80 92
81 Tradeoff 93 Priorities
82 ======== 94 ==========
83 95
84 .. image:: ease.png 96 .. image:: strategy.png
85 :height: 300px 97 :height: 300px
86 98
87 pirate.py 99 A ``cmd`` app: pirate.py
88 ========= 100 ========================
89 101
90 :: 102 ::
91 103
92 from cmd import Cmd 104 from cmd import Cmd
93 105
95 pass 107 pass
96 108
97 pirate = Pirate() 109 pirate = Pirate()
98 pirate.cmdloop() 110 pirate.cmdloop()
99 111
100 Nothing here... but history and help 112 .. Nothing here... but history and help
101 113
102 .. ctrl-r for bash-style history 114 .. ctrl-r for bash-style history
103 115
104 Fundamental prrrinciple 116 Fundamental prrrinciple
105 ======================= 117 =======================
106 118
107 .. class: huge 119 .. class:: huge
108 120
109 ``foo a b c`` -> 121 ::
122
123 (Cmd) foo a b c
110 124
111 ``self.do_foo('a b c')`` 125 ``self.do_foo('a b c')``
112 126
113 ``do_``-methods: pirate2.py 127 ``do_``-methods: pirate2.py
114 =========================== 128 ===========================
115 129
116 :: 130 ::
117 131
118 class Pirate(Cmd): 132 class Pirate(Cmd):
119 gold = 10 133 gold = 3
120 def do_loot(self, arg): 134 def do_loot(self, arg):
121 'Seize booty frrrom a passing ship.' 135 'Seize booty frrrom a passing ship.'
122 self.gold += 1 136 self.gold += 1
123 print('Now we gots {0} doubloons'.format(self.gold)) 137 print('Now we gots {0} doubloons'.format(self.gold))
124 def do_drink(self, arg): 138 def do_drink(self, arg):
137 Hooks: pirate3.py 151 Hooks: pirate3.py
138 ================= 152 =================
139 153
140 :: 154 ::
141 155
142 class Pirate(Cmd): 156 def do_loot(self, arg):
143 gold = 3 157 'Seize booty from a passing ship.'
144 def do_loot(self, arg): 158 self.gold += 1
145 'Drown your sorrrows in rrrum.' 159 def do_drink(self, arg):
146 self.gold += 1 160 'Drown your sorrrows in rrrum.'
147 def do_drink(self, arg): 161 self.gold -= 1
148 'Drown your sorrrows in rrrum.' 162 def precmd(self, line):
149 self.gold -= 1 163 self.initial_gold = self.gold
150 def postcmd(self, stop, line): 164 return line
151 print('Now we gots {0} doubloons'.format(self.gold)) 165 def postcmd(self, stop, line):
166 if self.gold != self.initial_gold:
167 print('Now we gots {0} doubloons'.format(self.gold))
152 168
153 Arguments: pirate4.py 169 Arguments: pirate4.py
154 ===================== 170 =====================
155 171
156 :: 172 ::
170 quitting: pirate5.py 186 quitting: pirate5.py
171 ==================== 187 ====================
172 188
173 :: 189 ::
174 190
175 def postcmd(self, stop, line): 191 def postcmd(self, stop, line):
176 print('Now we gots {0} doubloons' 192 if self.gold != self.initial_gold:
177 .format(self.gold)) 193 print('Now we gots {0} doubloons'.format(self.gold))
178 if self.gold < 0: 194 if self.gold < 0:
179 print("Off to debtorrr's prrrison. Game overrr.") 195 print("Off to debtorrr's prison. Game overrr.")
180 return True 196 return True
181 return stop 197 return stop
182 def do_quit(self, arg): 198 def do_quit(self, arg):
183 print("Quiterrr!") 199 print("Quiterrr!")
184 return True 200 return True
185 201
186 prompts and defaults: pirate6.py 202 prompts and defaults: pirate6.py
187 ================================ 203 ================================
188 204
189 :: 205 ::
190 206
191 prompt = 'arrr> ' 207 prompt = 'arrr> '
192 def default(self, line): 208 def default(self, line):
193 print('What mean ye by "{0}"?' 209 print('What mean ye by "{0}"?'
194 .format(line)) 210 .format(line))
195 211
196 cmd2 212 cmd2
197 ==== 213 ====
198 214
199 Third-party module in PyPI 215 .. image:: schematic.png
216 :height: 300px
200 217
201 Absolutely free 218 Absolutely free
202 =============== 219 ===============
203 220
204 * Script files 221 * Script files
221 ============================ 238 ============================
222 239
223 * Default to shell 240 * Default to shell
224 * Color output 241 * Color output
225 * Shortcuts 242 * Shortcuts
243 * Multiline commands
244 * Environment variables
226 245
227 But wait, there's more 246 Now how much would you pay?
228 ====================== 247 ===========================
229 248
230 * Case-insensitive commands 249 * options / flags
231 * Abbreviated commands 250 * Quiet (suppress feedback)
232 * Quitting 251 * BASH-style ``select``
233 * Timing 252 * Parsing: terminators, suffixes
234 * Echo 253
235 * Debug 254 Minor changes: pirate7.py
236 * Color output 255 =========================
237 256
238 257 ::
239 More 258
240 ==== 259 default_to_shell = True
241 260 multilineCommands = ['sing']
242 * Case-insensitivity 261 terminators = Cmd.terminators + ['...']
243 * One-character shortcuts 262 def do_sing(self, arg):
244 * Default to shell 263 print(self.colorize(arg, 'blue'))
245 264
265 Options: pirate8.py
266 ===================
267
268 ::
269
270 @options([make_option('--ho', type='int', help="How often to chant 'ho'", default=2),
271 make_option('-c', '--commas', action="store_true", help="Interspers commas")])
272 def do_yo(self, arg, opts):
273 chant = ['yo'] + ['ho'] * opts.ho
274 if opts.commas:
275 separator = ', '
276 else:
277 separator = ' '
278 print('{0} and a bottle of {1}'.format(separator.join(chant), arg))
279
280