blob: 37e08a2933f0d8ad26ffbf2ad87eb75c57df42c5 [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
14\begin{classdesc}{Cmd}{}
15A \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.
Guido van Rossum8668e8e1998-06-28 17:55:53 +000020\end{classdesc}
21
22\subsection{Cmd Objects}
23\label{Cmd-objects}
24
25A \class{Cmd} instance has the following methods:
26
Fred Drake579d3661998-07-02 19:35:12 +000027\begin{methoddesc}{cmdloop}{\optional{intro}}
Guido van Rossum8668e8e1998-06-28 17:55:53 +000028Repeatedly issue a prompt, accept input, parse an initial prefix off
29the received input, and dispatch to action methods, passing them the
30remainder of the line as argument.
31
32The optional argument is a banner or intro string to be issued before the
33first prompt (this overrides the \member{intro} class member).
34
35If the \module{readline} module is loaded, input will automatically
Fred Drake579d3661998-07-02 19:35:12 +000036inherit \program{bash}-like history-list editing (e.g. \kbd{Ctrl-P}
37scrolls back to the last command, \kbd{Ctrl-N} forward to the next
38one, \kbd{Ctrl-F} moves the cursor to the right non-destructively,
39\kbd{Ctrl-B} moves the cursor to the left non-destructively, etc.).
Guido van Rossum8668e8e1998-06-28 17:55:53 +000040
Fred Drake579d3661998-07-02 19:35:12 +000041An end-of-file on input is passed back as the string \code{'EOF'}.
Guido van Rossum8668e8e1998-06-28 17:55:53 +000042
Fred Drake579d3661998-07-02 19:35:12 +000043An interpreter instance will recognize a command name \samp{foo} if
44and only if it has a method \method{do_foo()}. As a special case,
Eric S. Raymond7ae3a5e2000-07-12 02:56:15 +000045a line beginning with the character \character{?} is dispatched to
Fred Drake579d3661998-07-02 19:35:12 +000046the method \method{do_help()}. As another special case, a line
Eric S. Raymond7ae3a5e2000-07-12 02:56:15 +000047beginning with the character \character{!} is dispatched to the
Fred Drake579d3661998-07-02 19:35:12 +000048method \method{do_shell} (if such a method is defined).
Guido van Rossum8668e8e1998-06-28 17:55:53 +000049
50All subclasses of \class{Cmd} inherit a predefined \method{do_help}.
51This method, called with an argument \code{bar}, invokes the
Fred Drake579d3661998-07-02 19:35:12 +000052corresponding method \method{help_bar()}. With no argument,
53\method{do_help()} lists all available help topics (that is, all
54commands with corresponding \method{help_*()} methods), and also lists
55any undocumented commands.
Guido van Rossum8668e8e1998-06-28 17:55:53 +000056\end{methoddesc}
57
58\begin{methoddesc}{onecmd}{str}
59Interpret the argument as though it had been typed in in
60response to the prompt.
61\end{methoddesc}
62
63\begin{methoddesc}{emptyline}{}
64Method called when an empty line is entered in response to the prompt.
65If this method is not overridden, it repeats the last nonempty command
66entered.
67\end{methoddesc}
68
69\begin{methoddesc}{default}{line}
70Method called on an input line when the command prefix is not
71recognized. If this method is not overridden, it prints an
72error message and returns.
73\end{methoddesc}
74
Fred Drake579d3661998-07-02 19:35:12 +000075\begin{methoddesc}{precmd}{}
76Hook method executed just before the input prompt is issued. This
77method is a stub in \class{Cmd}; it exists to be overridden by
78subclasses.
Guido van Rossum8668e8e1998-06-28 17:55:53 +000079\end{methoddesc}
80
Fred Drake579d3661998-07-02 19:35:12 +000081\begin{methoddesc}{postcmd}{}
Guido van Rossum8668e8e1998-06-28 17:55:53 +000082Hook method executed just after a command dispatch is finished. This
83method is a stub in \class{Cmd}; it exists to be overridden by
84subclasses.
85\end{methoddesc}
86
Fred Drake579d3661998-07-02 19:35:12 +000087\begin{methoddesc}{preloop}{}
88Hook method executed once when \method{cmdloop()} is called. This
89method is a stub in \class{Cmd}; it exists to be overridden by
90subclasses.
Guido van Rossum8668e8e1998-06-28 17:55:53 +000091\end{methoddesc}
92
Fred Drake579d3661998-07-02 19:35:12 +000093\begin{methoddesc}{postloop}{}
94Hook method executed once when \method{cmdloop()} is about to return.
95This method is a stub in \class{Cmd}; it exists to be overridden by
Guido van Rossum8668e8e1998-06-28 17:55:53 +000096subclasses.
97\end{methoddesc}
98
99Instances of \class{Cmd} subclasses have some public instance variables:
100
101\begin{memberdesc}{prompt}
102The prompt issued to solicit input.
103\end{memberdesc}
104
105\begin{memberdesc}{identchars}
106The string of characters accepted for the command prefix.
107\end{memberdesc}
108
109\begin{memberdesc}{lastcmd}
110The last nonempty command prefix seen.
111\end{memberdesc}
112
113\begin{memberdesc}{intro}
114A string to issue as an intro or banner. May be overridden by giving
115the \method{cmdloop()} method an argument.
116\end{memberdesc}
117
118\begin{memberdesc}{doc_header}
Fred Drake579d3661998-07-02 19:35:12 +0000119The header to issue if the help output has a section for documented
120commands.
Guido van Rossum8668e8e1998-06-28 17:55:53 +0000121\end{memberdesc}
122
123\begin{memberdesc}{misc_header}
Fred Drake579d3661998-07-02 19:35:12 +0000124The header to issue if the help output has a section for miscellaneous
125help topics (that is, there are \method{help_*()} methods without
126corresponding \method{do_*()} methods).
Guido van Rossum8668e8e1998-06-28 17:55:53 +0000127\end{memberdesc}
128
129\begin{memberdesc}{undoc_header}
130The header to issue if the help output has a section for undocumented
Fred Drake579d3661998-07-02 19:35:12 +0000131commands (that is, there are \method{do_*()} methods without
132corresponding \method{help_*()} methods).
Guido van Rossum8668e8e1998-06-28 17:55:53 +0000133\end{memberdesc}
134
135\begin{memberdesc}{ruler}
136The character used to draw separator lines under the help-message
Fred Drake579d3661998-07-02 19:35:12 +0000137headers. If empty, no ruler line is drawn. It defaults to
138\character{=}.
Guido van Rossum8668e8e1998-06-28 17:55:53 +0000139\end{memberdesc}
140
141