blob: 7c4dd4a3dc6a7fb2bb8e7ab0a050993bb707dc29 [file] [log] [blame]
Guido van Rossum8668e8e1998-06-28 17:55:53 +00001% Documentation by ESR
2\section{Standard Module \module{cmd}}
3\stmodindex{cmd}
4\label{module-cmd}
5
6The \code{Cmd} class provides a simple framework for writing
7line-oriented command interpreters. These are often useful for
8test harnesses, administrative tools, and prototypes that will
9later be wrapped in a more sophisticated interface.
10
11\begin{classdesc}{Cmd}{}
12A \class{Cmd} instance or subclass instance is a line-oriented
13interpreter framework. There is no good reason to instantiate Cmd
14itself; rather, it's useful as a superclass of an interpreter class
15you define yourself in order to inherit Cmd's methods and encapsulate
16action functions.
17\end{classdesc}
18
19\subsection{Cmd Objects}
20\label{Cmd-objects}
21
22A \class{Cmd} instance has the following methods:
23
24\begin{methoddesc}{cmdloop}{intro}
25Repeatedly issue a prompt, accept input, parse an initial prefix off
26the received input, and dispatch to action methods, passing them the
27remainder of the line as argument.
28
29The optional argument is a banner or intro string to be issued before the
30first prompt (this overrides the \member{intro} class member).
31
32If the \module{readline} module is loaded, input will automatically
33inherit Emacs-like history-list editing (e.g. Ctrl-P scrolls back to
34the last command, Ctrl-N forward to the next one, Ctrl-F moves the
35cursor to the right non-destructively, Ctrl-B moves the cursor to the
36left non-destructively, etc.).
37
38An end-of-file on input is passed back as the string "EOF".
39
40An interpreter instance will recognize a command name \code{foo} if
41and only if it has a method named \method{do_foo}. As a special case,
42a line containing only the character `?' is dispatched to the method
43\method{do_help}. As another special case, a line containing only the
44character `!' is dispatched to the method \method{do_shell} (if such a method
45is defined).
46
47All subclasses of \class{Cmd} inherit a predefined \method{do_help}.
48This method, called with an argument \code{bar}, invokes the
49corresponding method \method{help_bar}. With no argument,
50\method{do_help} lists all available help topics (that is, all
51commands with corresponding \code{help_} methods), and also lists any
52undocumented commands.
53\end{methoddesc}
54
55\begin{methoddesc}{onecmd}{str}
56Interpret the argument as though it had been typed in in
57response to the prompt.
58\end{methoddesc}
59
60\begin{methoddesc}{emptyline}{}
61Method called when an empty line is entered in response to the prompt.
62If this method is not overridden, it repeats the last nonempty command
63entered.
64\end{methoddesc}
65
66\begin{methoddesc}{default}{line}
67Method called on an input line when the command prefix is not
68recognized. If this method is not overridden, it prints an
69error message and returns.
70\end{methoddesc}
71
72\begin{methoddesc}{precmd}
73Hook method executed just before the input prompt is issued. This method is
74a stub in \class{Cmd}; it exists to be overridden by subclasses.
75\end{methoddesc}
76
77\begin{methoddesc}{postcmd}
78Hook method executed just after a command dispatch is finished. This
79method is a stub in \class{Cmd}; it exists to be overridden by
80subclasses.
81\end{methoddesc}
82
83\begin{methoddesc}{preloop}
84Hook method executed once when \method{cmdloop()} is called. This method is
85a stub in \class{Cmd}; it exists to be overridden by subclasses.
86\end{methoddesc}
87
88\begin{methoddesc}{postloop}
89Hook method executed once when \method{cmdloop()} is about to return. This
90method is a stub in \class{Cmd}; it exists to be overridden by
91subclasses.
92\end{methoddesc}
93
94Instances of \class{Cmd} subclasses have some public instance variables:
95
96\begin{memberdesc}{prompt}
97The prompt issued to solicit input.
98\end{memberdesc}
99
100\begin{memberdesc}{identchars}
101The string of characters accepted for the command prefix.
102\end{memberdesc}
103
104\begin{memberdesc}{lastcmd}
105The last nonempty command prefix seen.
106\end{memberdesc}
107
108\begin{memberdesc}{intro}
109A string to issue as an intro or banner. May be overridden by giving
110the \method{cmdloop()} method an argument.
111\end{memberdesc}
112
113\begin{memberdesc}{doc_header}
114The header to issue if the help output has a section for documented commands.
115\end{memberdesc}
116
117\begin{memberdesc}{misc_header}
118The header to issue if the help output has a section for miscellaneous
119help topics (that is, there are \code{help_} methods withoud corresponding
120\code{do_} functions).
121\end{memberdesc}
122
123\begin{memberdesc}{undoc_header}
124The header to issue if the help output has a section for undocumented
125commands (that is, there are \code{do_} methods withoud corresponding
126\code{help_} functions).
127\end{memberdesc}
128
129\begin{memberdesc}{ruler}
130The character used to draw separator lines under the help-message
131headers. If empty, no ruler line is drawn. It defaults to "=".
132\end{memberdesc}
133
134