Commit | Line | Data |
---|---|---|
920dae64 AT |
1 | <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> |
2 | <html> | |
3 | <head> | |
4 | <link rel="STYLESHEET" href="lib.css" type='text/css' /> | |
5 | <link rel="SHORTCUT ICON" href="../icons/pyfav.png" type="image/png" /> | |
6 | <link rel='start' href='../index.html' title='Python Documentation Index' /> | |
7 | <link rel="first" href="lib.html" title='Python Library Reference' /> | |
8 | <link rel='contents' href='contents.html' title="Contents" /> | |
9 | <link rel='index' href='genindex.html' title='Index' /> | |
10 | <link rel='last' href='about.html' title='About this document...' /> | |
11 | <link rel='help' href='about.html' title='About this document...' /> | |
12 | <link rel="prev" href="module-cmd.html" /> | |
13 | <link rel="parent" href="module-cmd.html" /> | |
14 | <link rel="next" href="module-shlex.html" /> | |
15 | <meta name='aesop' content='information' /> | |
16 | <title>5.20.1 Cmd Objects</title> | |
17 | </head> | |
18 | <body> | |
19 | <DIV CLASS="navigation"> | |
20 | <div id='top-navigation-panel' xml:id='top-navigation-panel'> | |
21 | <table align="center" width="100%" cellpadding="0" cellspacing="2"> | |
22 | <tr> | |
23 | <td class='online-navigation'><a rel="prev" title="5.20 cmd " | |
24 | href="module-cmd.html"><img src='../icons/previous.png' | |
25 | border='0' height='32' alt='Previous Page' width='32' /></A></td> | |
26 | <td class='online-navigation'><a rel="parent" title="5.20 cmd " | |
27 | href="module-cmd.html"><img src='../icons/up.png' | |
28 | border='0' height='32' alt='Up One Level' width='32' /></A></td> | |
29 | <td class='online-navigation'><a rel="next" title="5.21 shlex " | |
30 | href="module-shlex.html"><img src='../icons/next.png' | |
31 | border='0' height='32' alt='Next Page' width='32' /></A></td> | |
32 | <td align="center" width="100%">Python Library Reference</td> | |
33 | <td class='online-navigation'><a rel="contents" title="Table of Contents" | |
34 | href="contents.html"><img src='../icons/contents.png' | |
35 | border='0' height='32' alt='Contents' width='32' /></A></td> | |
36 | <td class='online-navigation'><a href="modindex.html" title="Module Index"><img src='../icons/modules.png' | |
37 | border='0' height='32' alt='Module Index' width='32' /></a></td> | |
38 | <td class='online-navigation'><a rel="index" title="Index" | |
39 | href="genindex.html"><img src='../icons/index.png' | |
40 | border='0' height='32' alt='Index' width='32' /></A></td> | |
41 | </tr></table> | |
42 | <div class='online-navigation'> | |
43 | <b class="navlabel">Previous:</b> | |
44 | <a class="sectref" rel="prev" href="module-cmd.html">5.20 cmd </A> | |
45 | <b class="navlabel">Up:</b> | |
46 | <a class="sectref" rel="parent" href="module-cmd.html">5.20 cmd </A> | |
47 | <b class="navlabel">Next:</b> | |
48 | <a class="sectref" rel="next" href="module-shlex.html">5.21 shlex </A> | |
49 | </div> | |
50 | <hr /></div> | |
51 | </DIV> | |
52 | <!--End of Navigation Panel--> | |
53 | ||
54 | <H2><A NAME="SECTION0072010000000000000000"></A> | |
55 | <A NAME="Cmd-objects"></A> | |
56 | <BR> | |
57 | 5.20.1 Cmd Objects | |
58 | </H2> | |
59 | ||
60 | <P> | |
61 | A <tt class="class">Cmd</tt> instance has the following methods: | |
62 | ||
63 | <P> | |
64 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> | |
65 | <td><nobr><b><tt id='l2h-1461' xml:id='l2h-1461' class="method">cmdloop</tt></b>(</nobr></td> | |
66 | <td><var></var><big>[</big><var>intro</var><big>]</big><var></var>)</td></tr></table></dt> | |
67 | <dd> | |
68 | Repeatedly issue a prompt, accept input, parse an initial prefix off | |
69 | the received input, and dispatch to action methods, passing them the | |
70 | remainder of the line as argument. | |
71 | ||
72 | <P> | |
73 | The optional argument is a banner or intro string to be issued before the | |
74 | first prompt (this overrides the <tt class="member">intro</tt> class member). | |
75 | ||
76 | <P> | |
77 | If the <tt class="module"><a href="module-readline.html">readline</a></tt> module is loaded, input will automatically | |
78 | inherit <b class="program">bash</b>-like history-list editing (e.g. <kbd>Control-P</kbd> | |
79 | scrolls back to the last command, <kbd>Control-N</kbd> forward to the next | |
80 | one, <kbd>Control-F</kbd> moves the cursor to the right non-destructively, | |
81 | <kbd>Control-B</kbd> moves the cursor to the left non-destructively, etc.). | |
82 | ||
83 | <P> | |
84 | An end-of-file on input is passed back as the string <code>'EOF'</code>. | |
85 | ||
86 | <P> | |
87 | An interpreter instance will recognize a command name "<tt class="samp">foo</tt>" if | |
88 | and only if it has a method <tt class="method">do_foo()</tt>. As a special case, | |
89 | a line beginning with the character "<tt class="character">?</tt>" is dispatched to | |
90 | the method <tt class="method">do_help()</tt>. As another special case, a line | |
91 | beginning with the character "<tt class="character">!</tt>" is dispatched to the | |
92 | method <tt class="method">do_shell()</tt> (if such a method is defined). | |
93 | ||
94 | <P> | |
95 | This method will return when the <tt class="method">postcmd()</tt> method returns a | |
96 | true value. The <var>stop</var> argument to <tt class="method">postcmd()</tt> is the | |
97 | return value from the command's corresponding <tt class="method">do_*()</tt> method. | |
98 | ||
99 | <P> | |
100 | If completion is enabled, completing commands will be done | |
101 | automatically, and completing of commands args is done by calling | |
102 | <tt class="method">complete_foo()</tt> with arguments <var>text</var>, <var>line</var>, | |
103 | <var>begidx</var>, and <var>endidx</var>. <var>text</var> is the string prefix we | |
104 | are attempting to match: all returned matches must begin with it. | |
105 | <var>line</var> is the current input line with leading whitespace removed, | |
106 | <var>begidx</var> and <var>endidx</var> are the beginning and ending indexes | |
107 | of the prefix text, which could be used to provide different | |
108 | completion depending upon which position the argument is in. | |
109 | ||
110 | <P> | |
111 | All subclasses of <tt class="class">Cmd</tt> inherit a predefined <tt class="method">do_help()</tt>. | |
112 | This method, called with an argument <code>'bar'</code>, invokes the | |
113 | corresponding method <tt class="method">help_bar()</tt>. With no argument, | |
114 | <tt class="method">do_help()</tt> lists all available help topics (that is, all | |
115 | commands with corresponding <tt class="method">help_*()</tt> methods), and also lists | |
116 | any undocumented commands. | |
117 | </dl> | |
118 | ||
119 | <P> | |
120 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> | |
121 | <td><nobr><b><tt id='l2h-1462' xml:id='l2h-1462' class="method">onecmd</tt></b>(</nobr></td> | |
122 | <td><var>str</var>)</td></tr></table></dt> | |
123 | <dd> | |
124 | Interpret the argument as though it had been typed in response to the | |
125 | prompt. This may be overridden, but should not normally need to be; | |
126 | see the <tt class="method">precmd()</tt> and <tt class="method">postcmd()</tt> methods for useful | |
127 | execution hooks. The return value is a flag indicating whether | |
128 | interpretation of commands by the interpreter should stop. If there | |
129 | is a <tt class="method">do_*()</tt> method for the command <var>str</var>, the return | |
130 | value of that method is returned, otherwise the return value from the | |
131 | <tt class="method">default()</tt> method is returned. | |
132 | </dl> | |
133 | ||
134 | <P> | |
135 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> | |
136 | <td><nobr><b><tt id='l2h-1463' xml:id='l2h-1463' class="method">emptyline</tt></b>(</nobr></td> | |
137 | <td><var></var>)</td></tr></table></dt> | |
138 | <dd> | |
139 | Method called when an empty line is entered in response to the prompt. | |
140 | If this method is not overridden, it repeats the last nonempty command | |
141 | entered. | |
142 | </dl> | |
143 | ||
144 | <P> | |
145 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> | |
146 | <td><nobr><b><tt id='l2h-1464' xml:id='l2h-1464' class="method">default</tt></b>(</nobr></td> | |
147 | <td><var>line</var>)</td></tr></table></dt> | |
148 | <dd> | |
149 | Method called on an input line when the command prefix is not | |
150 | recognized. If this method is not overridden, it prints an | |
151 | error message and returns. | |
152 | </dl> | |
153 | ||
154 | <P> | |
155 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> | |
156 | <td><nobr><b><tt id='l2h-1465' xml:id='l2h-1465' class="method">completedefault</tt></b>(</nobr></td> | |
157 | <td><var>text, line, begidx, endidx</var>)</td></tr></table></dt> | |
158 | <dd> | |
159 | Method called to complete an input line when no command-specific | |
160 | <tt class="method">complete_*()</tt> method is available. By default, it returns an | |
161 | empty list. | |
162 | </dl> | |
163 | ||
164 | <P> | |
165 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> | |
166 | <td><nobr><b><tt id='l2h-1466' xml:id='l2h-1466' class="method">precmd</tt></b>(</nobr></td> | |
167 | <td><var>line</var>)</td></tr></table></dt> | |
168 | <dd> | |
169 | Hook method executed just before the command line <var>line</var> is | |
170 | interpreted, but after the input prompt is generated and issued. This | |
171 | method is a stub in <tt class="class">Cmd</tt>; it exists to be overridden by | |
172 | subclasses. The return value is used as the command which will be | |
173 | executed by the <tt class="method">onecmd()</tt> method; the <tt class="method">precmd()</tt> | |
174 | implementation may re-write the command or simply return <var>line</var> | |
175 | unchanged. | |
176 | </dl> | |
177 | ||
178 | <P> | |
179 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> | |
180 | <td><nobr><b><tt id='l2h-1467' xml:id='l2h-1467' class="method">postcmd</tt></b>(</nobr></td> | |
181 | <td><var>stop, line</var>)</td></tr></table></dt> | |
182 | <dd> | |
183 | Hook method executed just after a command dispatch is finished. This | |
184 | method is a stub in <tt class="class">Cmd</tt>; it exists to be overridden by | |
185 | subclasses. <var>line</var> is the command line which was executed, and | |
186 | <var>stop</var> is a flag which indicates whether execution will be | |
187 | terminated after the call to <tt class="method">postcmd()</tt>; this will be the | |
188 | return value of the <tt class="method">onecmd()</tt> method. The return value of | |
189 | this method will be used as the new value for the internal flag which | |
190 | corresponds to <var>stop</var>; returning false will cause interpretation | |
191 | to continue. | |
192 | </dl> | |
193 | ||
194 | <P> | |
195 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> | |
196 | <td><nobr><b><tt id='l2h-1468' xml:id='l2h-1468' class="method">preloop</tt></b>(</nobr></td> | |
197 | <td><var></var>)</td></tr></table></dt> | |
198 | <dd> | |
199 | Hook method executed once when <tt class="method">cmdloop()</tt> is called. This | |
200 | method is a stub in <tt class="class">Cmd</tt>; it exists to be overridden by | |
201 | subclasses. | |
202 | </dl> | |
203 | ||
204 | <P> | |
205 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> | |
206 | <td><nobr><b><tt id='l2h-1469' xml:id='l2h-1469' class="method">postloop</tt></b>(</nobr></td> | |
207 | <td><var></var>)</td></tr></table></dt> | |
208 | <dd> | |
209 | Hook method executed once when <tt class="method">cmdloop()</tt> is about to return. | |
210 | This method is a stub in <tt class="class">Cmd</tt>; it exists to be overridden by | |
211 | subclasses. | |
212 | </dl> | |
213 | ||
214 | <P> | |
215 | Instances of <tt class="class">Cmd</tt> subclasses have some public instance variables: | |
216 | ||
217 | <P> | |
218 | <dl><dt><b><tt id='l2h-1470' xml:id='l2h-1470' class="member">prompt</tt></b></dt> | |
219 | <dd> | |
220 | The prompt issued to solicit input. | |
221 | </dl> | |
222 | ||
223 | <P> | |
224 | <dl><dt><b><tt id='l2h-1471' xml:id='l2h-1471' class="member">identchars</tt></b></dt> | |
225 | <dd> | |
226 | The string of characters accepted for the command prefix. | |
227 | </dl> | |
228 | ||
229 | <P> | |
230 | <dl><dt><b><tt id='l2h-1472' xml:id='l2h-1472' class="member">lastcmd</tt></b></dt> | |
231 | <dd> | |
232 | The last nonempty command prefix seen. | |
233 | </dl> | |
234 | ||
235 | <P> | |
236 | <dl><dt><b><tt id='l2h-1473' xml:id='l2h-1473' class="member">intro</tt></b></dt> | |
237 | <dd> | |
238 | A string to issue as an intro or banner. May be overridden by giving | |
239 | the <tt class="method">cmdloop()</tt> method an argument. | |
240 | </dl> | |
241 | ||
242 | <P> | |
243 | <dl><dt><b><tt id='l2h-1474' xml:id='l2h-1474' class="member">doc_header</tt></b></dt> | |
244 | <dd> | |
245 | The header to issue if the help output has a section for documented | |
246 | commands. | |
247 | </dl> | |
248 | ||
249 | <P> | |
250 | <dl><dt><b><tt id='l2h-1475' xml:id='l2h-1475' class="member">misc_header</tt></b></dt> | |
251 | <dd> | |
252 | The header to issue if the help output has a section for miscellaneous | |
253 | help topics (that is, there are <tt class="method">help_*()</tt> methods without | |
254 | corresponding <tt class="method">do_*()</tt> methods). | |
255 | </dl> | |
256 | ||
257 | <P> | |
258 | <dl><dt><b><tt id='l2h-1476' xml:id='l2h-1476' class="member">undoc_header</tt></b></dt> | |
259 | <dd> | |
260 | The header to issue if the help output has a section for undocumented | |
261 | commands (that is, there are <tt class="method">do_*()</tt> methods without | |
262 | corresponding <tt class="method">help_*()</tt> methods). | |
263 | </dl> | |
264 | ||
265 | <P> | |
266 | <dl><dt><b><tt id='l2h-1477' xml:id='l2h-1477' class="member">ruler</tt></b></dt> | |
267 | <dd> | |
268 | The character used to draw separator lines under the help-message | |
269 | headers. If empty, no ruler line is drawn. It defaults to | |
270 | "<tt class="character">=</tt>". | |
271 | </dl> | |
272 | ||
273 | <P> | |
274 | <dl><dt><b><tt id='l2h-1478' xml:id='l2h-1478' class="member">use_rawinput</tt></b></dt> | |
275 | <dd> | |
276 | A flag, defaulting to true. If true, <tt class="method">cmdloop()</tt> uses | |
277 | <tt class="function">raw_input()</tt> to display a prompt and read the next command; | |
278 | if false, <tt class="method">sys.stdout.write()</tt> and | |
279 | <tt class="method">sys.stdin.readline()</tt> are used. (This means that by | |
280 | importing <tt class="module"><a href="module-readline.html">readline</a></tt>, on systems that support it, the | |
281 | interpreter will automatically support <b class="program">Emacs</b>-like line editing | |
282 | and command-history keystrokes.) | |
283 | </dl> | |
284 | ||
285 | <DIV CLASS="navigation"> | |
286 | <div class='online-navigation'> | |
287 | <p></p><hr /> | |
288 | <table align="center" width="100%" cellpadding="0" cellspacing="2"> | |
289 | <tr> | |
290 | <td class='online-navigation'><a rel="prev" title="5.20 cmd " | |
291 | href="module-cmd.html"><img src='../icons/previous.png' | |
292 | border='0' height='32' alt='Previous Page' width='32' /></A></td> | |
293 | <td class='online-navigation'><a rel="parent" title="5.20 cmd " | |
294 | href="module-cmd.html"><img src='../icons/up.png' | |
295 | border='0' height='32' alt='Up One Level' width='32' /></A></td> | |
296 | <td class='online-navigation'><a rel="next" title="5.21 shlex " | |
297 | href="module-shlex.html"><img src='../icons/next.png' | |
298 | border='0' height='32' alt='Next Page' width='32' /></A></td> | |
299 | <td align="center" width="100%">Python Library Reference</td> | |
300 | <td class='online-navigation'><a rel="contents" title="Table of Contents" | |
301 | href="contents.html"><img src='../icons/contents.png' | |
302 | border='0' height='32' alt='Contents' width='32' /></A></td> | |
303 | <td class='online-navigation'><a href="modindex.html" title="Module Index"><img src='../icons/modules.png' | |
304 | border='0' height='32' alt='Module Index' width='32' /></a></td> | |
305 | <td class='online-navigation'><a rel="index" title="Index" | |
306 | href="genindex.html"><img src='../icons/index.png' | |
307 | border='0' height='32' alt='Index' width='32' /></A></td> | |
308 | </tr></table> | |
309 | <div class='online-navigation'> | |
310 | <b class="navlabel">Previous:</b> | |
311 | <a class="sectref" rel="prev" href="module-cmd.html">5.20 cmd </A> | |
312 | <b class="navlabel">Up:</b> | |
313 | <a class="sectref" rel="parent" href="module-cmd.html">5.20 cmd </A> | |
314 | <b class="navlabel">Next:</b> | |
315 | <a class="sectref" rel="next" href="module-shlex.html">5.21 shlex </A> | |
316 | </div> | |
317 | </div> | |
318 | <hr /> | |
319 | <span class="release-info">Release 2.4.2, documentation updated on 28 September 2005.</span> | |
320 | </DIV> | |
321 | <!--End of Navigation Panel--> | |
322 | <ADDRESS> | |
323 | See <i><a href="about.html">About this document...</a></i> for information on suggesting changes. | |
324 | </ADDRESS> | |
325 | </BODY> | |
326 | </HTML> |