blob: 661eb9e45ca372a3104992912b49087a629688b4 [file] [log] [blame]
Fred Drake295da241998-08-10 19:42:37 +00001\section{\module{cmd} ---
Fred Drakef8ca7d82000-10-10 17:03:45 +00002 Support for line-oriented command interpreters}
3
Fred Drakeb91e9341998-07-23 17:59:49 +00004\declaremodule{standard}{cmd}
Fred Drake295da241998-08-10 19:42:37 +00005\sectionauthor{Eric S. Raymond}{esr@snark.thyrsus.com}
Fred Drakef8ca7d82000-10-10 17:03:45 +00006\modulesynopsis{Build line-oriented command interpreters.}
Fred Drakeb91e9341998-07-23 17:59:49 +00007
Guido van Rossum8668e8e1998-06-28 17:55:53 +00008
Fred Drake579d3661998-07-02 19:35:12 +00009The \class{Cmd} class provides a simple framework for writing
Guido van Rossum8668e8e1998-06-28 17:55:53 +000010line-oriented command interpreters. These are often useful for
11test harnesses, administrative tools, and prototypes that will
12later be wrapped in a more sophisticated interface.
13
Fred Drake7f8d17a2003-12-31 05:01:23 +000014\begin{classdesc}{Cmd}{\optional{completekey\optional{,
15 stdin\optional{, stdout}}}}
Guido van Rossum8668e8e1998-06-28 17:55:53 +000016A \class{Cmd} instance or subclass instance is a line-oriented
Fred Drake579d3661998-07-02 19:35:12 +000017interpreter framework. There is no good reason to instantiate
18\class{Cmd} itself; rather, it's useful as a superclass of an
19interpreter class you define yourself in order to inherit
20\class{Cmd}'s methods and encapsulate action methods.
Martin v. Löwis66b6e192001-07-28 14:44:03 +000021
Anthony Baxter983b0082003-02-06 01:45:11 +000022The optional argument \var{completekey} is the \refmodule{readline} name
23of a completion key; it defaults to \kbd{Tab}. If \var{completekey} is
Fred Drake7f8d17a2003-12-31 05:01:23 +000024not \constant{None} and \refmodule{readline} is available, command completion
Anthony Baxter983b0082003-02-06 01:45:11 +000025is done automatically.
26
27The optional arguments \var{stdin} and \var{stdout} specify the
28input and output file objects that the Cmd instance or subclass
29instance will use for input and output. If not specified, they
30will default to \var{sys.stdin} and \var{sys.stdout}.
31
Georg Brandla635fbb2006-01-15 07:55:35 +000032\versionchanged[The \var{stdin} and \var{stdout} parameters were added]{2.3}
Guido van Rossum8668e8e1998-06-28 17:55:53 +000033\end{classdesc}
34
35\subsection{Cmd Objects}
36\label{Cmd-objects}
37
38A \class{Cmd} instance has the following methods:
39
Fred Drake579d3661998-07-02 19:35:12 +000040\begin{methoddesc}{cmdloop}{\optional{intro}}
Guido van Rossum8668e8e1998-06-28 17:55:53 +000041Repeatedly issue a prompt, accept input, parse an initial prefix off
42the received input, and dispatch to action methods, passing them the
43remainder of the line as argument.
44
45The optional argument is a banner or intro string to be issued before the
46first prompt (this overrides the \member{intro} class member).
47
Fred Drake7f8d17a2003-12-31 05:01:23 +000048If the \refmodule{readline} module is loaded, input will automatically
Fred Drake682d5f32001-07-12 02:09:51 +000049inherit \program{bash}-like history-list editing (e.g. \kbd{Control-P}
50scrolls back to the last command, \kbd{Control-N} forward to the next
51one, \kbd{Control-F} moves the cursor to the right non-destructively,
52\kbd{Control-B} moves the cursor to the left non-destructively, etc.).
Guido van Rossum8668e8e1998-06-28 17:55:53 +000053
Fred Drake579d3661998-07-02 19:35:12 +000054An end-of-file on input is passed back as the string \code{'EOF'}.
Guido van Rossum8668e8e1998-06-28 17:55:53 +000055
Fred Drake579d3661998-07-02 19:35:12 +000056An interpreter instance will recognize a command name \samp{foo} if
57and only if it has a method \method{do_foo()}. As a special case,
Eric S. Raymond7ae3a5e2000-07-12 02:56:15 +000058a line beginning with the character \character{?} is dispatched to
Fred Drake579d3661998-07-02 19:35:12 +000059the method \method{do_help()}. As another special case, a line
Eric S. Raymond7ae3a5e2000-07-12 02:56:15 +000060beginning with the character \character{!} is dispatched to the
Fred Drake43211ec2001-07-29 03:41:23 +000061method \method{do_shell()} (if such a method is defined).
Guido van Rossum8668e8e1998-06-28 17:55:53 +000062
Fred Drake69013d02004-07-30 18:58:54 +000063This method will return when the \method{postcmd()} method returns a
64true value. The \var{stop} argument to \method{postcmd()} is the
65return value from the command's corresponding \method{do_*()} method.
66
Martin v. Löwis66b6e192001-07-28 14:44:03 +000067If completion is enabled, completing commands will be done
68automatically, and completing of commands args is done by calling
Fred Drake43211ec2001-07-29 03:41:23 +000069\method{complete_foo()} with arguments \var{text}, \var{line},
70\var{begidx}, and \var{endidx}. \var{text} is the string prefix we
71are attempting to match: all returned matches must begin with it.
72\var{line} is the current input line with leading whitespace removed,
73\var{begidx} and \var{endidx} are the beginning and ending indexes
74of the prefix text, which could be used to provide different
75completion depending upon which position the argument is in.
Martin v. Löwis66b6e192001-07-28 14:44:03 +000076
Fred Drake43211ec2001-07-29 03:41:23 +000077All subclasses of \class{Cmd} inherit a predefined \method{do_help()}.
78This method, called with an argument \code{'bar'}, invokes the
Fred Drake579d3661998-07-02 19:35:12 +000079corresponding method \method{help_bar()}. With no argument,
80\method{do_help()} lists all available help topics (that is, all
81commands with corresponding \method{help_*()} methods), and also lists
82any undocumented commands.
Guido van Rossum8668e8e1998-06-28 17:55:53 +000083\end{methoddesc}
84
85\begin{methoddesc}{onecmd}{str}
Fred Drake7c9a53d2001-12-27 05:10:18 +000086Interpret the argument as though it had been typed in response to the
87prompt. This may be overridden, but should not normally need to be;
88see the \method{precmd()} and \method{postcmd()} methods for useful
89execution hooks. The return value is a flag indicating whether
Fred Drake69013d02004-07-30 18:58:54 +000090interpretation of commands by the interpreter should stop. If there
91is a \method{do_*()} method for the command \var{str}, the return
92value of that method is returned, otherwise the return value from the
93\method{default()} method is returned.
Guido van Rossum8668e8e1998-06-28 17:55:53 +000094\end{methoddesc}
95
96\begin{methoddesc}{emptyline}{}
97Method called when an empty line is entered in response to the prompt.
98If this method is not overridden, it repeats the last nonempty command
99entered.
100\end{methoddesc}
101
102\begin{methoddesc}{default}{line}
103Method called on an input line when the command prefix is not
104recognized. If this method is not overridden, it prints an
105error message and returns.
106\end{methoddesc}
107
Martin v. Löwis66b6e192001-07-28 14:44:03 +0000108\begin{methoddesc}{completedefault}{text, line, begidx, endidx}
109Method called to complete an input line when no command-specific
Fred Drake43211ec2001-07-29 03:41:23 +0000110\method{complete_*()} method is available. By default, it returns an
Martin v. Löwis66b6e192001-07-28 14:44:03 +0000111empty list.
112\end{methoddesc}
113
Fred Drake7c9a53d2001-12-27 05:10:18 +0000114\begin{methoddesc}{precmd}{line}
115Hook method executed just before the command line \var{line} is
116interpreted, but after the input prompt is generated and issued. This
Fred Drake579d3661998-07-02 19:35:12 +0000117method is a stub in \class{Cmd}; it exists to be overridden by
Fred Drake7c9a53d2001-12-27 05:10:18 +0000118subclasses. The return value is used as the command which will be
119executed by the \method{onecmd()} method; the \method{precmd()}
120implementation may re-write the command or simply return \var{line}
121unchanged.
Guido van Rossum8668e8e1998-06-28 17:55:53 +0000122\end{methoddesc}
123
Fred Drake7c9a53d2001-12-27 05:10:18 +0000124\begin{methoddesc}{postcmd}{stop, line}
Guido van Rossum8668e8e1998-06-28 17:55:53 +0000125Hook method executed just after a command dispatch is finished. This
126method is a stub in \class{Cmd}; it exists to be overridden by
Fred Drake7c9a53d2001-12-27 05:10:18 +0000127subclasses. \var{line} is the command line which was executed, and
128\var{stop} is a flag which indicates whether execution will be
129terminated after the call to \method{postcmd()}; this will be the
130return value of the \method{onecmd()} method. The return value of
131this method will be used as the new value for the internal flag which
132corresponds to \var{stop}; returning false will cause interpretation
133to continue.
Guido van Rossum8668e8e1998-06-28 17:55:53 +0000134\end{methoddesc}
135
Fred Drake579d3661998-07-02 19:35:12 +0000136\begin{methoddesc}{preloop}{}
137Hook method executed once when \method{cmdloop()} is called. This
138method is a stub in \class{Cmd}; it exists to be overridden by
139subclasses.
Guido van Rossum8668e8e1998-06-28 17:55:53 +0000140\end{methoddesc}
141
Fred Drake579d3661998-07-02 19:35:12 +0000142\begin{methoddesc}{postloop}{}
143Hook method executed once when \method{cmdloop()} is about to return.
144This method is a stub in \class{Cmd}; it exists to be overridden by
Guido van Rossum8668e8e1998-06-28 17:55:53 +0000145subclasses.
146\end{methoddesc}
147
148Instances of \class{Cmd} subclasses have some public instance variables:
149
150\begin{memberdesc}{prompt}
151The prompt issued to solicit input.
152\end{memberdesc}
153
154\begin{memberdesc}{identchars}
155The string of characters accepted for the command prefix.
156\end{memberdesc}
157
158\begin{memberdesc}{lastcmd}
159The last nonempty command prefix seen.
160\end{memberdesc}
161
162\begin{memberdesc}{intro}
163A string to issue as an intro or banner. May be overridden by giving
164the \method{cmdloop()} method an argument.
165\end{memberdesc}
166
167\begin{memberdesc}{doc_header}
Fred Drake579d3661998-07-02 19:35:12 +0000168The header to issue if the help output has a section for documented
169commands.
Guido van Rossum8668e8e1998-06-28 17:55:53 +0000170\end{memberdesc}
171
172\begin{memberdesc}{misc_header}
Fred Drake579d3661998-07-02 19:35:12 +0000173The header to issue if the help output has a section for miscellaneous
174help topics (that is, there are \method{help_*()} methods without
175corresponding \method{do_*()} methods).
Guido van Rossum8668e8e1998-06-28 17:55:53 +0000176\end{memberdesc}
177
178\begin{memberdesc}{undoc_header}
179The header to issue if the help output has a section for undocumented
Fred Drake579d3661998-07-02 19:35:12 +0000180commands (that is, there are \method{do_*()} methods without
181corresponding \method{help_*()} methods).
Guido van Rossum8668e8e1998-06-28 17:55:53 +0000182\end{memberdesc}
183
184\begin{memberdesc}{ruler}
185The character used to draw separator lines under the help-message
Fred Drake579d3661998-07-02 19:35:12 +0000186headers. If empty, no ruler line is drawn. It defaults to
187\character{=}.
Guido van Rossum8668e8e1998-06-28 17:55:53 +0000188\end{memberdesc}
189
Guido van Rossumc8da0f92001-03-24 19:17:35 +0000190\begin{memberdesc}{use_rawinput}
191A flag, defaulting to true. If true, \method{cmdloop()} uses
192\function{raw_input()} to display a prompt and read the next command;
Fred Drake43211ec2001-07-29 03:41:23 +0000193if false, \method{sys.stdout.write()} and
194\method{sys.stdin.readline()} are used. (This means that by
Fred Drake7f8d17a2003-12-31 05:01:23 +0000195importing \refmodule{readline}, on systems that support it, the
196interpreter will automatically support \program{Emacs}-like line editing
Eric S. Raymondff00fda2001-06-23 14:42:43 +0000197and command-history keystrokes.)
Guido van Rossumc8da0f92001-03-24 19:17:35 +0000198\end{memberdesc}