blob: 153d7a3bee41708556c0d9ddb029eb211e9373a2 [file] [log] [blame]
Fred Drake295da241998-08-10 19:42:37 +00001\section{\module{cmd} ---
2 Build line-oriented command interpreters.}
Fred Drakeb91e9341998-07-23 17:59:49 +00003\declaremodule{standard}{cmd}
Fred Drake295da241998-08-10 19:42:37 +00004\sectionauthor{Eric S. Raymond}{esr@snark.thyrsus.com}
Fred Drakeb91e9341998-07-23 17:59:49 +00005
Fred Drake295da241998-08-10 19:42:37 +00006\modulesynopsis{Build line-oriented command interpreters; this is used
7by module \module{pdb}.}
Fred Drakeb91e9341998-07-23 17:59:49 +00008
Guido van Rossum8668e8e1998-06-28 17:55:53 +00009
Fred Drake579d3661998-07-02 19:35:12 +000010The \class{Cmd} class provides a simple framework for writing
Guido van Rossum8668e8e1998-06-28 17:55:53 +000011line-oriented command interpreters. These are often useful for
12test harnesses, administrative tools, and prototypes that will
13later be wrapped in a more sophisticated interface.
14
15\begin{classdesc}{Cmd}{}
16A \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.
Guido van Rossum8668e8e1998-06-28 17:55:53 +000021\end{classdesc}
22
23\subsection{Cmd Objects}
24\label{Cmd-objects}
25
26A \class{Cmd} instance has the following methods:
27
Fred Drake579d3661998-07-02 19:35:12 +000028\begin{methoddesc}{cmdloop}{\optional{intro}}
Guido van Rossum8668e8e1998-06-28 17:55:53 +000029Repeatedly issue a prompt, accept input, parse an initial prefix off
30the received input, and dispatch to action methods, passing them the
31remainder of the line as argument.
32
33The optional argument is a banner or intro string to be issued before the
34first prompt (this overrides the \member{intro} class member).
35
36If the \module{readline} module is loaded, input will automatically
Fred Drake579d3661998-07-02 19:35:12 +000037inherit \program{bash}-like history-list editing (e.g. \kbd{Ctrl-P}
38scrolls back to the last command, \kbd{Ctrl-N} forward to the next
39one, \kbd{Ctrl-F} moves the cursor to the right non-destructively,
40\kbd{Ctrl-B} moves the cursor to the left non-destructively, etc.).
Guido van Rossum8668e8e1998-06-28 17:55:53 +000041
Fred Drake579d3661998-07-02 19:35:12 +000042An end-of-file on input is passed back as the string \code{'EOF'}.
Guido van Rossum8668e8e1998-06-28 17:55:53 +000043
Fred Drake579d3661998-07-02 19:35:12 +000044An interpreter instance will recognize a command name \samp{foo} if
45and only if it has a method \method{do_foo()}. As a special case,
46a line containing only the character \character{?} is dispatched to
47the method \method{do_help()}. As another special case, a line
48containing only the character \character{!} is dispatched to the
49method \method{do_shell} (if such a method is defined).
Guido van Rossum8668e8e1998-06-28 17:55:53 +000050
51All subclasses of \class{Cmd} inherit a predefined \method{do_help}.
52This method, called with an argument \code{bar}, invokes the
Fred Drake579d3661998-07-02 19:35:12 +000053corresponding method \method{help_bar()}. With no argument,
54\method{do_help()} lists all available help topics (that is, all
55commands with corresponding \method{help_*()} methods), and also lists
56any undocumented commands.
Guido van Rossum8668e8e1998-06-28 17:55:53 +000057\end{methoddesc}
58
59\begin{methoddesc}{onecmd}{str}
60Interpret the argument as though it had been typed in in
61response to the prompt.
62\end{methoddesc}
63
64\begin{methoddesc}{emptyline}{}
65Method called when an empty line is entered in response to the prompt.
66If this method is not overridden, it repeats the last nonempty command
67entered.
68\end{methoddesc}
69
70\begin{methoddesc}{default}{line}
71Method called on an input line when the command prefix is not
72recognized. If this method is not overridden, it prints an
73error message and returns.
74\end{methoddesc}
75
Fred Drake579d3661998-07-02 19:35:12 +000076\begin{methoddesc}{precmd}{}
77Hook method executed just before the input prompt is issued. This
78method is a stub in \class{Cmd}; it exists to be overridden by
79subclasses.
Guido van Rossum8668e8e1998-06-28 17:55:53 +000080\end{methoddesc}
81
Fred Drake579d3661998-07-02 19:35:12 +000082\begin{methoddesc}{postcmd}{}
Guido van Rossum8668e8e1998-06-28 17:55:53 +000083Hook method executed just after a command dispatch is finished. This
84method is a stub in \class{Cmd}; it exists to be overridden by
85subclasses.
86\end{methoddesc}
87
Fred Drake579d3661998-07-02 19:35:12 +000088\begin{methoddesc}{preloop}{}
89Hook method executed once when \method{cmdloop()} is called. This
90method is a stub in \class{Cmd}; it exists to be overridden by
91subclasses.
Guido van Rossum8668e8e1998-06-28 17:55:53 +000092\end{methoddesc}
93
Fred Drake579d3661998-07-02 19:35:12 +000094\begin{methoddesc}{postloop}{}
95Hook method executed once when \method{cmdloop()} is about to return.
96This method is a stub in \class{Cmd}; it exists to be overridden by
Guido van Rossum8668e8e1998-06-28 17:55:53 +000097subclasses.
98\end{methoddesc}
99
100Instances of \class{Cmd} subclasses have some public instance variables:
101
102\begin{memberdesc}{prompt}
103The prompt issued to solicit input.
104\end{memberdesc}
105
106\begin{memberdesc}{identchars}
107The string of characters accepted for the command prefix.
108\end{memberdesc}
109
110\begin{memberdesc}{lastcmd}
111The last nonempty command prefix seen.
112\end{memberdesc}
113
114\begin{memberdesc}{intro}
115A string to issue as an intro or banner. May be overridden by giving
116the \method{cmdloop()} method an argument.
117\end{memberdesc}
118
119\begin{memberdesc}{doc_header}
Fred Drake579d3661998-07-02 19:35:12 +0000120The header to issue if the help output has a section for documented
121commands.
Guido van Rossum8668e8e1998-06-28 17:55:53 +0000122\end{memberdesc}
123
124\begin{memberdesc}{misc_header}
Fred Drake579d3661998-07-02 19:35:12 +0000125The header to issue if the help output has a section for miscellaneous
126help topics (that is, there are \method{help_*()} methods without
127corresponding \method{do_*()} methods).
Guido van Rossum8668e8e1998-06-28 17:55:53 +0000128\end{memberdesc}
129
130\begin{memberdesc}{undoc_header}
131The header to issue if the help output has a section for undocumented
Fred Drake579d3661998-07-02 19:35:12 +0000132commands (that is, there are \method{do_*()} methods without
133corresponding \method{help_*()} methods).
Guido van Rossum8668e8e1998-06-28 17:55:53 +0000134\end{memberdesc}
135
136\begin{memberdesc}{ruler}
137The character used to draw separator lines under the help-message
Fred Drake579d3661998-07-02 19:35:12 +0000138headers. If empty, no ruler line is drawn. It defaults to
139\character{=}.
Guido van Rossum8668e8e1998-06-28 17:55:53 +0000140\end{memberdesc}
141
142