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
|
344
|
39 * Accepts free text input at prompt
|
|
40 * Outputs lines of text
|
|
41 * Persistent CLI environment
|
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
|
344
|
58 * Accept arguments at invocation
|
|
59 * execution
|
|
60 * terminate
|
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:
|
|
166 print('''What's "{0}"? I'll take rrrum.'''.format(arg))
|
|
167 self.gold -= 1
|
|
168
|
|
169 quitting: pirate5.py
|
|
170 ====================
|
|
171
|
|
172 ::
|
|
173
|
|
174 def postcmd(self, stop, line):
|
|
175 print('Now we gots {0} doubloons'.format(self.gold))
|
|
176 if self.gold < 0:
|
|
177 print("Off to debtorrr's prrrison. Game overrr.")
|
|
178 return True
|
|
179 return stop
|
|
180 def do_quit(self, arg):
|
|
181 print("Quiterrr!")
|
|
182 return True
|
|
183
|
|
184 prompts and defaults: pirate6.py
|
|
185 ================================
|
|
186
|
|
187 ::
|
|
188
|
|
189 prompt = 'arrr> '
|
|
190 def default(self, line):
|
|
191 print('What mean ye by "{0}"?'.format(line)) |