blob: 4b7a23d5bac202a0c28ef92ab5e52a9e8b41a610 [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
Anthony Baxter983b0082003-02-06 01:45:11 +000014\begin{classdesc}{Cmd}{\optional{completekey},\optional{stdin},\optional{stdout}}
Guido van Rossum8668e8e1998-06-28 17:55:53 +000015A \class{Cmd} instance or subclass instance is a line-oriented
Fred Drake579d3661998-07-02 19:35:12 +000016interpreter framework. There is no good reason to instantiate
17\class{Cmd} itself; rather, it's useful as a superclass of an
18interpreter class you define yourself in order to inherit
19\class{Cmd}'s methods and encapsulate action methods.
Martin v. Löwis66b6e192001-07-28 14:44:03 +000020
Anthony Baxter983b0082003-02-06 01:45:11 +000021The optional argument \var{completekey} is the \refmodule{readline} name
22of a completion key; it defaults to \kbd{Tab}. If \var{completekey} is
23not \code{None} and \module{readline} is available, command completion
24is done automatically.
25
26The optional arguments \var{stdin} and \var{stdout} specify the
27input and output file objects that the Cmd instance or subclass
28instance will use for input and output. If not specified, they
29will default to \var{sys.stdin} and \var{sys.stdout}.
30
Neal Norwitz292f56c2003-02-06 05:02:39 +000031\versionchanged[The \var{stdin} and \var{stdout} parameters were added.]{2.3}
Guido van Rossum8668e8e1998-06-28 17:55:53 +000032\end{classdesc}
33
34\subsection{Cmd Objects}
35\label{Cmd-objects}
36
37A \class{Cmd} instance has the following methods:
38
Fred Drake579d3661998-07-02 19:35:12 +000039\begin{methoddesc}{cmdloop}{\optional{intro}}
Guido van Rossum8668e8e1998-06-28 17:55:53 +000040Repeatedly issue a prompt, accept input, parse an initial prefix off
41the received input, and dispatch to action methods, passing them the
42remainder of the line as argument.
43
44The optional argument is a banner or intro string to be issued before the
45first prompt (this overrides the \member{intro} class member).
46
47If the \module{readline} module is loaded, input will automatically
Fred Drake682d5f32001-07-12 02:09:51 +000048inherit \program{bash}-like history-list editing (e.g. \kbd{Control-P}
49scrolls back to the last command, \kbd{Control-N} forward to the next
50one, \kbd{Control-F} moves the cursor to the right non-destructively,
51\kbd{Control-B} moves the cursor to the left non-destructively, etc.).
Guido van Rossum8668e8e1998-06-28 17:55:53 +000052
Fred Drake579d3661998-07-02 19:35:12 +000053An end-of-file on input is passed back as the string \code{'EOF'}.
Guido van Rossum8668e8e1998-06-28 17:55:53 +000054
Fred Drake579d3661998-07-02 19:35:12 +000055An interpreter instance will recognize a command name \samp{foo} if
56and only if it has a method \method{do_foo()}. As a special case,
Eric S. Raymond7ae3a5e2000-07-12 02:56:15 +000057a line beginning with the character \character{?} is dispatched to
Fred Drake579d3661998-07-02 19:35:12 +000058the method \method{do_help()}. As another special case, a line
Eric S. Raymond7ae3a5e2000-07-12 02:56:15 +000059beginning with the character \character{!} is dispatched to the
Fred Drake43211ec2001-07-29 03:41:23 +000060method \method{do_shell()} (if such a method is defined).
Guido van Rossum8668e8e1998-06-28 17:55:53 +000061
Martin v. Löwis66b6e192001-07-28 14:44:03 +000062If completion is enabled, completing commands will be done
63automatically, and completing of commands args is done by calling
Fred Drake43211ec2001-07-29 03:41:23 +000064\method{complete_foo()} with arguments \var{text}, \var{line},
65\var{begidx}, and \var{endidx}. \var{text} is the string prefix we
66are attempting to match: all returned matches must begin with it.
67\var{line} is the current input line with leading whitespace removed,
68\var{begidx} and \var{endidx} are the beginning and ending indexes
69of the prefix text, which could be used to provide different
70completion depending upon which position the argument is in.
Martin v. Löwis66b6e192001-07-28 14:44:03 +000071
Fred Drake43211ec2001-07-29 03:41:23 +000072All subclasses of \class{Cmd} inherit a predefined \method{do_help()}.
73This method, called with an argument \code{'bar'}, invokes the
Fred Drake579d3661998-07-02 19:35:12 +000074corresponding method \method{help_bar()}. With no argument,
75\method{do_help()} lists all available help topics (that is, all
76commands with corresponding \method{help_*()} methods), and also lists
77any undocumented commands.
Guido van Rossum8668e8e1998-06-28 17:55:53 +000078\end{methoddesc}
79
80\begin{methoddesc}{onecmd}{str}
Fred Drake7c9a53d2001-12-27 05:10:18 +000081Interpret the argument as though it had been typed in response to the
82prompt. This may be overridden, but should not normally need to be;
83see the \method{precmd()} and \method{postcmd()} methods for useful
84execution hooks. The return value is a flag indicating whether
85interpretation of commands by the interpreter should stop.
Guido van Rossum8668e8e1998-06-28 17:55:53 +000086\end{methoddesc}
87
88\begin{methoddesc}{emptyline}{}
89Method called when an empty line is entered in response to the prompt.
90If this method is not overridden, it repeats the last nonempty command
91entered.
92\end{methoddesc}
93
94\begin{methoddesc}{default}{line}
95Method called on an input line when the command prefix is not
96recognized. If this method is not overridden, it prints an
97error message and returns.
98\end{methoddesc}
99
Martin v. Löwis66b6e192001-07-28 14:44:03 +0000100\begin{methoddesc}{completedefault}{text, line, begidx, endidx}
101Method called to complete an input line when no command-specific
Fred Drake43211ec2001-07-29 03:41:23 +0000102\method{complete_*()} method is available. By default, it returns an
Martin v. Löwis66b6e192001-07-28 14:44:03 +0000103empty list.
104\end{methoddesc}
105
Fred Drake7c9a53d2001-12-27 05:10:18 +0000106\begin{methoddesc}{precmd}{line}
107Hook method executed just before the command line \var{line} is
108interpreted, but after the input prompt is generated and issued. This
Fred Drake579d3661998-07-02 19:35:12 +0000109method is a stub in \class{Cmd}; it exists to be overridden by
Fred Drake7c9a53d2001-12-27 05:10:18 +0000110subclasses. The return value is used as the command which will be
111executed by the \method{onecmd()} method; the \method{precmd()}
112implementation may re-write the command or simply return \var{line}
113unchanged.
Guido van Rossum8668e8e1998-06-28 17:55:53 +0000114\end{methoddesc}
115
Fred Drake7c9a53d2001-12-27 05:10:18 +0000116\begin{methoddesc}{postcmd}{stop, line}
Guido van Rossum8668e8e1998-06-28 17:55:53 +0000117Hook method executed just after a command dispatch is finished. This
118method is a stub in \class{Cmd}; it exists to be overridden by
Fred Drake7c9a53d2001-12-27 05:10:18 +0000119subclasses. \var{line} is the command line which was executed, and
120\var{stop} is a flag which indicates whether execution will be
121terminated after the call to \method{postcmd()}; this will be the
122return value of the \method{onecmd()} method. The return value of
123this method will be used as the new value for the internal flag which
124corresponds to \var{stop}; returning false will cause interpretation
125to continue.
Guido van Rossum8668e8e1998-06-28 17:55:53 +0000126\end{methoddesc}
127
Fred Drake579d3661998-07-02 19:35:12 +0000128\begin{methoddesc}{preloop}{}
129Hook method executed once when \method{cmdloop()} is called. This
130method is a stub in \class{Cmd}; it exists to be overridden by
131subclasses.
Guido van Rossum8668e8e1998-06-28 17:55:53 +0000132\end{methoddesc}
133
Fred Drake579d3661998-07-02 19:35:12 +0000134\begin{methoddesc}{postloop}{}
135Hook method executed once when \method{cmdloop()} is about to return.
136This method is a stub in \class{Cmd}; it exists to be overridden by
Guido van Rossum8668e8e1998-06-28 17:55:53 +0000137subclasses.
138\end{methoddesc}
139
140Instances of \class{Cmd} subclasses have some public instance variables:
141
142\begin{memberdesc}{prompt}
143The prompt issued to solicit input.
144\end{memberdesc}
145
146\begin{memberdesc}{identchars}
147The string of characters accepted for the command prefix.
148\end{memberdesc}
149
150\begin{memberdesc}{lastcmd}
151The last nonempty command prefix seen.
152\end{memberdesc}
153
154\begin{memberdesc}{intro}
155A string to issue as an intro or banner. May be overridden by giving
156the \method{cmdloop()} method an argument.
157\end{memberdesc}
158
159\begin{memberdesc}{doc_header}
Fred Drake579d3661998-07-02 19:35:12 +0000160The header to issue if the help output has a section for documented
161commands.
Guido van Rossum8668e8e1998-06-28 17:55:53 +0000162\end{memberdesc}
163
164\begin{memberdesc}{misc_header}
Fred Drake579d3661998-07-02 19:35:12 +0000165The header to issue if the help output has a section for miscellaneous
166help topics (that is, there are \method{help_*()} methods without
167corresponding \method{do_*()} methods).
Guido van Rossum8668e8e1998-06-28 17:55:53 +0000168\end{memberdesc}
169
170\begin{memberdesc}{undoc_header}
171The header to issue if the help output has a section for undocumented
Fred Drake579d3661998-07-02 19:35:12 +0000172commands (that is, there are \method{do_*()} methods without
173corresponding \method{help_*()} methods).
Guido van Rossum8668e8e1998-06-28 17:55:53 +0000174\end{memberdesc}
175
176\begin{memberdesc}{ruler}
177The character used to draw separator lines under the help-message
Fred Drake579d3661998-07-02 19:35:12 +0000178headers. If empty, no ruler line is drawn. It defaults to
179\character{=}.
Guido van Rossum8668e8e1998-06-28 17:55:53 +0000180\end{memberdesc}
181
Guido van Rossumc8da0f92001-03-24 19:17:35 +0000182\begin{memberdesc}{use_rawinput}
183A flag, defaulting to true. If true, \method{cmdloop()} uses
184\function{raw_input()} to display a prompt and read the next command;
Fred Drake43211ec2001-07-29 03:41:23 +0000185if false, \method{sys.stdout.write()} and
186\method{sys.stdin.readline()} are used. (This means that by
Eric S. Raymondff00fda2001-06-23 14:42:43 +0000187importing \module{readline}, on systems that support it, the
188interpreter will automatically support Emacs-like line editing
189and command-history keystrokes.)
Guido van Rossumc8da0f92001-03-24 19:17:35 +0000190\end{memberdesc}