blob: 834d16a0f8c03e7f5d3c1a6a4b825ec97019fd39 [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.
Martin v. Löwis66b6e192001-07-28 14:44:03 +000025
Guido van Rossum8668e8e1998-06-28 17:55:53 +000026\end{classdesc}
27
28\subsection{Cmd Objects}
29\label{Cmd-objects}
30
31A \class{Cmd} instance has the following methods:
32
Fred Drake579d3661998-07-02 19:35:12 +000033\begin{methoddesc}{cmdloop}{\optional{intro}}
Guido van Rossum8668e8e1998-06-28 17:55:53 +000034Repeatedly issue a prompt, accept input, parse an initial prefix off
35the received input, and dispatch to action methods, passing them the
36remainder of the line as argument.
37
38The optional argument is a banner or intro string to be issued before the
39first prompt (this overrides the \member{intro} class member).
40
41If the \module{readline} module is loaded, input will automatically
Fred Drake682d5f32001-07-12 02:09:51 +000042inherit \program{bash}-like history-list editing (e.g. \kbd{Control-P}
43scrolls back to the last command, \kbd{Control-N} forward to the next
44one, \kbd{Control-F} moves the cursor to the right non-destructively,
45\kbd{Control-B} moves the cursor to the left non-destructively, etc.).
Guido van Rossum8668e8e1998-06-28 17:55:53 +000046
Fred Drake579d3661998-07-02 19:35:12 +000047An end-of-file on input is passed back as the string \code{'EOF'}.
Guido van Rossum8668e8e1998-06-28 17:55:53 +000048
Fred Drake579d3661998-07-02 19:35:12 +000049An interpreter instance will recognize a command name \samp{foo} if
50and only if it has a method \method{do_foo()}. As a special case,
Eric S. Raymond7ae3a5e2000-07-12 02:56:15 +000051a line beginning with the character \character{?} is dispatched to
Fred Drake579d3661998-07-02 19:35:12 +000052the method \method{do_help()}. As another special case, a line
Eric S. Raymond7ae3a5e2000-07-12 02:56:15 +000053beginning with the character \character{!} is dispatched to the
Fred Drake43211ec2001-07-29 03:41:23 +000054method \method{do_shell()} (if such a method is defined).
Guido van Rossum8668e8e1998-06-28 17:55:53 +000055
Martin v. Löwis66b6e192001-07-28 14:44:03 +000056If completion is enabled, completing commands will be done
57automatically, and completing of commands args is done by calling
Fred Drake43211ec2001-07-29 03:41:23 +000058\method{complete_foo()} with arguments \var{text}, \var{line},
59\var{begidx}, and \var{endidx}. \var{text} is the string prefix we
60are attempting to match: all returned matches must begin with it.
61\var{line} is the current input line with leading whitespace removed,
62\var{begidx} and \var{endidx} are the beginning and ending indexes
63of the prefix text, which could be used to provide different
64completion depending upon which position the argument is in.
Martin v. Löwis66b6e192001-07-28 14:44:03 +000065
Fred Drake43211ec2001-07-29 03:41:23 +000066All subclasses of \class{Cmd} inherit a predefined \method{do_help()}.
67This method, called with an argument \code{'bar'}, invokes the
Fred Drake579d3661998-07-02 19:35:12 +000068corresponding method \method{help_bar()}. With no argument,
69\method{do_help()} lists all available help topics (that is, all
70commands with corresponding \method{help_*()} methods), and also lists
71any undocumented commands.
Guido van Rossum8668e8e1998-06-28 17:55:53 +000072\end{methoddesc}
73
74\begin{methoddesc}{onecmd}{str}
75Interpret the argument as though it had been typed in in
76response to the prompt.
77\end{methoddesc}
78
79\begin{methoddesc}{emptyline}{}
80Method called when an empty line is entered in response to the prompt.
81If this method is not overridden, it repeats the last nonempty command
82entered.
83\end{methoddesc}
84
85\begin{methoddesc}{default}{line}
86Method called on an input line when the command prefix is not
87recognized. If this method is not overridden, it prints an
88error message and returns.
89\end{methoddesc}
90
Martin v. Löwis66b6e192001-07-28 14:44:03 +000091\begin{methoddesc}{completedefault}{text, line, begidx, endidx}
92Method called to complete an input line when no command-specific
Fred Drake43211ec2001-07-29 03:41:23 +000093\method{complete_*()} method is available. By default, it returns an
Martin v. Löwis66b6e192001-07-28 14:44:03 +000094empty list.
95\end{methoddesc}
96
Fred Drake579d3661998-07-02 19:35:12 +000097\begin{methoddesc}{precmd}{}
Eric S. Raymondff00fda2001-06-23 14:42:43 +000098Hook method executed just before the command line is interpreted, but
99after the input prompt is generated and issued. This
Fred Drake579d3661998-07-02 19:35:12 +0000100method is a stub in \class{Cmd}; it exists to be overridden by
101subclasses.
Guido van Rossum8668e8e1998-06-28 17:55:53 +0000102\end{methoddesc}
103
Fred Drake579d3661998-07-02 19:35:12 +0000104\begin{methoddesc}{postcmd}{}
Guido van Rossum8668e8e1998-06-28 17:55:53 +0000105Hook method executed just after a command dispatch is finished. This
106method is a stub in \class{Cmd}; it exists to be overridden by
107subclasses.
108\end{methoddesc}
109
Fred Drake579d3661998-07-02 19:35:12 +0000110\begin{methoddesc}{preloop}{}
111Hook method executed once when \method{cmdloop()} is called. This
112method is a stub in \class{Cmd}; it exists to be overridden by
113subclasses.
Guido van Rossum8668e8e1998-06-28 17:55:53 +0000114\end{methoddesc}
115
Fred Drake579d3661998-07-02 19:35:12 +0000116\begin{methoddesc}{postloop}{}
117Hook method executed once when \method{cmdloop()} is about to return.
118This method is a stub in \class{Cmd}; it exists to be overridden by
Guido van Rossum8668e8e1998-06-28 17:55:53 +0000119subclasses.
120\end{methoddesc}
121
122Instances of \class{Cmd} subclasses have some public instance variables:
123
124\begin{memberdesc}{prompt}
125The prompt issued to solicit input.
126\end{memberdesc}
127
128\begin{memberdesc}{identchars}
129The string of characters accepted for the command prefix.
130\end{memberdesc}
131
132\begin{memberdesc}{lastcmd}
133The last nonempty command prefix seen.
134\end{memberdesc}
135
136\begin{memberdesc}{intro}
137A string to issue as an intro or banner. May be overridden by giving
138the \method{cmdloop()} method an argument.
139\end{memberdesc}
140
141\begin{memberdesc}{doc_header}
Fred Drake579d3661998-07-02 19:35:12 +0000142The header to issue if the help output has a section for documented
143commands.
Guido van Rossum8668e8e1998-06-28 17:55:53 +0000144\end{memberdesc}
145
146\begin{memberdesc}{misc_header}
Fred Drake579d3661998-07-02 19:35:12 +0000147The header to issue if the help output has a section for miscellaneous
148help topics (that is, there are \method{help_*()} methods without
149corresponding \method{do_*()} methods).
Guido van Rossum8668e8e1998-06-28 17:55:53 +0000150\end{memberdesc}
151
152\begin{memberdesc}{undoc_header}
153The header to issue if the help output has a section for undocumented
Fred Drake579d3661998-07-02 19:35:12 +0000154commands (that is, there are \method{do_*()} methods without
155corresponding \method{help_*()} methods).
Guido van Rossum8668e8e1998-06-28 17:55:53 +0000156\end{memberdesc}
157
158\begin{memberdesc}{ruler}
159The character used to draw separator lines under the help-message
Fred Drake579d3661998-07-02 19:35:12 +0000160headers. If empty, no ruler line is drawn. It defaults to
161\character{=}.
Guido van Rossum8668e8e1998-06-28 17:55:53 +0000162\end{memberdesc}
163
Guido van Rossumc8da0f92001-03-24 19:17:35 +0000164\begin{memberdesc}{use_rawinput}
165A flag, defaulting to true. If true, \method{cmdloop()} uses
166\function{raw_input()} to display a prompt and read the next command;
Fred Drake43211ec2001-07-29 03:41:23 +0000167if false, \method{sys.stdout.write()} and
168\method{sys.stdin.readline()} are used. (This means that by
Eric S. Raymondff00fda2001-06-23 14:42:43 +0000169importing \module{readline}, on systems that support it, the
170interpreter will automatically support Emacs-like line editing
171and command-history keystrokes.)
Guido van Rossumc8da0f92001-03-24 19:17:35 +0000172\end{memberdesc}