blob: 59d573fba9a413d366654a04614cbf3acabede1b [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
Martin v. Löwis66b6e192001-07-28 14:44:03 +000014\begin{classdesc}{Cmd}{\optional{completekey}}
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
21The optional argument is the \refmodule{readline} name of a completion
Fred Drake43211ec2001-07-29 03:41:23 +000022key; it defaults to \kbd{Tab}. If \var{completekey} is not \code{None}
23and \module{readline} is available, command completion is done
24automatically.
Guido van Rossum8668e8e1998-06-28 17:55:53 +000025\end{classdesc}
26
27\subsection{Cmd Objects}
28\label{Cmd-objects}
29
30A \class{Cmd} instance has the following methods:
31
Fred Drake579d3661998-07-02 19:35:12 +000032\begin{methoddesc}{cmdloop}{\optional{intro}}
Guido van Rossum8668e8e1998-06-28 17:55:53 +000033Repeatedly issue a prompt, accept input, parse an initial prefix off
34the received input, and dispatch to action methods, passing them the
35remainder of the line as argument.
36
37The optional argument is a banner or intro string to be issued before the
38first prompt (this overrides the \member{intro} class member).
39
40If the \module{readline} module is loaded, input will automatically
Fred Drake682d5f32001-07-12 02:09:51 +000041inherit \program{bash}-like history-list editing (e.g. \kbd{Control-P}
42scrolls back to the last command, \kbd{Control-N} forward to the next
43one, \kbd{Control-F} moves the cursor to the right non-destructively,
44\kbd{Control-B} moves the cursor to the left non-destructively, etc.).
Guido van Rossum8668e8e1998-06-28 17:55:53 +000045
Fred Drake579d3661998-07-02 19:35:12 +000046An end-of-file on input is passed back as the string \code{'EOF'}.
Guido van Rossum8668e8e1998-06-28 17:55:53 +000047
Fred Drake579d3661998-07-02 19:35:12 +000048An interpreter instance will recognize a command name \samp{foo} if
49and only if it has a method \method{do_foo()}. As a special case,
Eric S. Raymond7ae3a5e2000-07-12 02:56:15 +000050a line beginning with the character \character{?} is dispatched to
Fred Drake579d3661998-07-02 19:35:12 +000051the method \method{do_help()}. As another special case, a line
Eric S. Raymond7ae3a5e2000-07-12 02:56:15 +000052beginning with the character \character{!} is dispatched to the
Fred Drake43211ec2001-07-29 03:41:23 +000053method \method{do_shell()} (if such a method is defined).
Guido van Rossum8668e8e1998-06-28 17:55:53 +000054
Martin v. Löwis66b6e192001-07-28 14:44:03 +000055If completion is enabled, completing commands will be done
56automatically, and completing of commands args is done by calling
Fred Drake43211ec2001-07-29 03:41:23 +000057\method{complete_foo()} with arguments \var{text}, \var{line},
58\var{begidx}, and \var{endidx}. \var{text} is the string prefix we
59are attempting to match: all returned matches must begin with it.
60\var{line} is the current input line with leading whitespace removed,
61\var{begidx} and \var{endidx} are the beginning and ending indexes
62of the prefix text, which could be used to provide different
63completion depending upon which position the argument is in.
Martin v. Löwis66b6e192001-07-28 14:44:03 +000064
Fred Drake43211ec2001-07-29 03:41:23 +000065All subclasses of \class{Cmd} inherit a predefined \method{do_help()}.
66This method, called with an argument \code{'bar'}, invokes the
Fred Drake579d3661998-07-02 19:35:12 +000067corresponding method \method{help_bar()}. With no argument,
68\method{do_help()} lists all available help topics (that is, all
69commands with corresponding \method{help_*()} methods), and also lists
70any undocumented commands.
Guido van Rossum8668e8e1998-06-28 17:55:53 +000071\end{methoddesc}
72
73\begin{methoddesc}{onecmd}{str}
Fred Drake7c9a53d2001-12-27 05:10:18 +000074Interpret the argument as though it had been typed in response to the
75prompt. This may be overridden, but should not normally need to be;
76see the \method{precmd()} and \method{postcmd()} methods for useful
77execution hooks. The return value is a flag indicating whether
78interpretation of commands by the interpreter should stop.
Guido van Rossum8668e8e1998-06-28 17:55:53 +000079\end{methoddesc}
80
81\begin{methoddesc}{emptyline}{}
82Method called when an empty line is entered in response to the prompt.
83If this method is not overridden, it repeats the last nonempty command
84entered.
85\end{methoddesc}
86
87\begin{methoddesc}{default}{line}
88Method called on an input line when the command prefix is not
89recognized. If this method is not overridden, it prints an
90error message and returns.
91\end{methoddesc}
92
Martin v. Löwis66b6e192001-07-28 14:44:03 +000093\begin{methoddesc}{completedefault}{text, line, begidx, endidx}
94Method called to complete an input line when no command-specific
Fred Drake43211ec2001-07-29 03:41:23 +000095\method{complete_*()} method is available. By default, it returns an
Martin v. Löwis66b6e192001-07-28 14:44:03 +000096empty list.
97\end{methoddesc}
98
Fred Drake7c9a53d2001-12-27 05:10:18 +000099\begin{methoddesc}{precmd}{line}
100Hook method executed just before the command line \var{line} is
101interpreted, but after the input prompt is generated and issued. This
Fred Drake579d3661998-07-02 19:35:12 +0000102method is a stub in \class{Cmd}; it exists to be overridden by
Fred Drake7c9a53d2001-12-27 05:10:18 +0000103subclasses. The return value is used as the command which will be
104executed by the \method{onecmd()} method; the \method{precmd()}
105implementation may re-write the command or simply return \var{line}
106unchanged.
Guido van Rossum8668e8e1998-06-28 17:55:53 +0000107\end{methoddesc}
108
Fred Drake7c9a53d2001-12-27 05:10:18 +0000109\begin{methoddesc}{postcmd}{stop, line}
Guido van Rossum8668e8e1998-06-28 17:55:53 +0000110Hook method executed just after a command dispatch is finished. This
111method is a stub in \class{Cmd}; it exists to be overridden by
Fred Drake7c9a53d2001-12-27 05:10:18 +0000112subclasses. \var{line} is the command line which was executed, and
113\var{stop} is a flag which indicates whether execution will be
114terminated after the call to \method{postcmd()}; this will be the
115return value of the \method{onecmd()} method. The return value of
116this method will be used as the new value for the internal flag which
117corresponds to \var{stop}; returning false will cause interpretation
118to continue.
Guido van Rossum8668e8e1998-06-28 17:55:53 +0000119\end{methoddesc}
120
Fred Drake579d3661998-07-02 19:35:12 +0000121\begin{methoddesc}{preloop}{}
122Hook method executed once when \method{cmdloop()} is called. This
123method is a stub in \class{Cmd}; it exists to be overridden by
124subclasses.
Guido van Rossum8668e8e1998-06-28 17:55:53 +0000125\end{methoddesc}
126
Fred Drake579d3661998-07-02 19:35:12 +0000127\begin{methoddesc}{postloop}{}
128Hook method executed once when \method{cmdloop()} is about to return.
129This method is a stub in \class{Cmd}; it exists to be overridden by
Guido van Rossum8668e8e1998-06-28 17:55:53 +0000130subclasses.
131\end{methoddesc}
132
133Instances of \class{Cmd} subclasses have some public instance variables:
134
135\begin{memberdesc}{prompt}
136The prompt issued to solicit input.
137\end{memberdesc}
138
139\begin{memberdesc}{identchars}
140The string of characters accepted for the command prefix.
141\end{memberdesc}
142
143\begin{memberdesc}{lastcmd}
144The last nonempty command prefix seen.
145\end{memberdesc}
146
147\begin{memberdesc}{intro}
148A string to issue as an intro or banner. May be overridden by giving
149the \method{cmdloop()} method an argument.
150\end{memberdesc}
151
152\begin{memberdesc}{doc_header}
Fred Drake579d3661998-07-02 19:35:12 +0000153The header to issue if the help output has a section for documented
154commands.
Guido van Rossum8668e8e1998-06-28 17:55:53 +0000155\end{memberdesc}
156
157\begin{memberdesc}{misc_header}
Fred Drake579d3661998-07-02 19:35:12 +0000158The header to issue if the help output has a section for miscellaneous
159help topics (that is, there are \method{help_*()} methods without
160corresponding \method{do_*()} methods).
Guido van Rossum8668e8e1998-06-28 17:55:53 +0000161\end{memberdesc}
162
163\begin{memberdesc}{undoc_header}
164The header to issue if the help output has a section for undocumented
Fred Drake579d3661998-07-02 19:35:12 +0000165commands (that is, there are \method{do_*()} methods without
166corresponding \method{help_*()} methods).
Guido van Rossum8668e8e1998-06-28 17:55:53 +0000167\end{memberdesc}
168
169\begin{memberdesc}{ruler}
170The character used to draw separator lines under the help-message
Fred Drake579d3661998-07-02 19:35:12 +0000171headers. If empty, no ruler line is drawn. It defaults to
172\character{=}.
Guido van Rossum8668e8e1998-06-28 17:55:53 +0000173\end{memberdesc}
174
Guido van Rossumc8da0f92001-03-24 19:17:35 +0000175\begin{memberdesc}{use_rawinput}
176A flag, defaulting to true. If true, \method{cmdloop()} uses
177\function{raw_input()} to display a prompt and read the next command;
Fred Drake43211ec2001-07-29 03:41:23 +0000178if false, \method{sys.stdout.write()} and
179\method{sys.stdin.readline()} are used. (This means that by
Eric S. Raymondff00fda2001-06-23 14:42:43 +0000180importing \module{readline}, on systems that support it, the
181interpreter will automatically support Emacs-like line editing
182and command-history keystrokes.)
Guido van Rossumc8da0f92001-03-24 19:17:35 +0000183\end{memberdesc}