blob: 53935d5fea3f6a93524f4e22f6668392c01c8326 [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
Fred Drake579d3661998-07-02 19:35:12 +00006The \class{Cmd} class provides a simple framework for writing
Guido van Rossum8668e8e1998-06-28 17:55:53 +00007line-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
Fred Drake579d3661998-07-02 19:35:12 +000013interpreter framework. There is no good reason to instantiate
14\class{Cmd} itself; rather, it's useful as a superclass of an
15interpreter class you define yourself in order to inherit
16\class{Cmd}'s methods and encapsulate action methods.
Guido van Rossum8668e8e1998-06-28 17:55:53 +000017\end{classdesc}
18
19\subsection{Cmd Objects}
20\label{Cmd-objects}
21
22A \class{Cmd} instance has the following methods:
23
Fred Drake579d3661998-07-02 19:35:12 +000024\begin{methoddesc}{cmdloop}{\optional{intro}}
Guido van Rossum8668e8e1998-06-28 17:55:53 +000025Repeatedly 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
Fred Drake579d3661998-07-02 19:35:12 +000033inherit \program{bash}-like history-list editing (e.g. \kbd{Ctrl-P}
34scrolls back to the last command, \kbd{Ctrl-N} forward to the next
35one, \kbd{Ctrl-F} moves the cursor to the right non-destructively,
36\kbd{Ctrl-B} moves the cursor to the left non-destructively, etc.).
Guido van Rossum8668e8e1998-06-28 17:55:53 +000037
Fred Drake579d3661998-07-02 19:35:12 +000038An end-of-file on input is passed back as the string \code{'EOF'}.
Guido van Rossum8668e8e1998-06-28 17:55:53 +000039
Fred Drake579d3661998-07-02 19:35:12 +000040An interpreter instance will recognize a command name \samp{foo} if
41and only if it has a method \method{do_foo()}. As a special case,
42a line containing only the character \character{?} is dispatched to
43the method \method{do_help()}. As another special case, a line
44containing only the character \character{!} is dispatched to the
45method \method{do_shell} (if such a method is defined).
Guido van Rossum8668e8e1998-06-28 17:55:53 +000046
47All subclasses of \class{Cmd} inherit a predefined \method{do_help}.
48This method, called with an argument \code{bar}, invokes the
Fred Drake579d3661998-07-02 19:35:12 +000049corresponding method \method{help_bar()}. With no argument,
50\method{do_help()} lists all available help topics (that is, all
51commands with corresponding \method{help_*()} methods), and also lists
52any undocumented commands.
Guido van Rossum8668e8e1998-06-28 17:55:53 +000053\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
Fred Drake579d3661998-07-02 19:35:12 +000072\begin{methoddesc}{precmd}{}
73Hook method executed just before the input prompt is issued. This
74method is a stub in \class{Cmd}; it exists to be overridden by
75subclasses.
Guido van Rossum8668e8e1998-06-28 17:55:53 +000076\end{methoddesc}
77
Fred Drake579d3661998-07-02 19:35:12 +000078\begin{methoddesc}{postcmd}{}
Guido van Rossum8668e8e1998-06-28 17:55:53 +000079Hook method executed just after a command dispatch is finished. This
80method is a stub in \class{Cmd}; it exists to be overridden by
81subclasses.
82\end{methoddesc}
83
Fred Drake579d3661998-07-02 19:35:12 +000084\begin{methoddesc}{preloop}{}
85Hook method executed once when \method{cmdloop()} is called. This
86method is a stub in \class{Cmd}; it exists to be overridden by
87subclasses.
Guido van Rossum8668e8e1998-06-28 17:55:53 +000088\end{methoddesc}
89
Fred Drake579d3661998-07-02 19:35:12 +000090\begin{methoddesc}{postloop}{}
91Hook method executed once when \method{cmdloop()} is about to return.
92This method is a stub in \class{Cmd}; it exists to be overridden by
Guido van Rossum8668e8e1998-06-28 17:55:53 +000093subclasses.
94\end{methoddesc}
95
96Instances of \class{Cmd} subclasses have some public instance variables:
97
98\begin{memberdesc}{prompt}
99The prompt issued to solicit input.
100\end{memberdesc}
101
102\begin{memberdesc}{identchars}
103The string of characters accepted for the command prefix.
104\end{memberdesc}
105
106\begin{memberdesc}{lastcmd}
107The last nonempty command prefix seen.
108\end{memberdesc}
109
110\begin{memberdesc}{intro}
111A string to issue as an intro or banner. May be overridden by giving
112the \method{cmdloop()} method an argument.
113\end{memberdesc}
114
115\begin{memberdesc}{doc_header}
Fred Drake579d3661998-07-02 19:35:12 +0000116The header to issue if the help output has a section for documented
117commands.
Guido van Rossum8668e8e1998-06-28 17:55:53 +0000118\end{memberdesc}
119
120\begin{memberdesc}{misc_header}
Fred Drake579d3661998-07-02 19:35:12 +0000121The header to issue if the help output has a section for miscellaneous
122help topics (that is, there are \method{help_*()} methods without
123corresponding \method{do_*()} methods).
Guido van Rossum8668e8e1998-06-28 17:55:53 +0000124\end{memberdesc}
125
126\begin{memberdesc}{undoc_header}
127The header to issue if the help output has a section for undocumented
Fred Drake579d3661998-07-02 19:35:12 +0000128commands (that is, there are \method{do_*()} methods without
129corresponding \method{help_*()} methods).
Guido van Rossum8668e8e1998-06-28 17:55:53 +0000130\end{memberdesc}
131
132\begin{memberdesc}{ruler}
133The character used to draw separator lines under the help-message
Fred Drake579d3661998-07-02 19:35:12 +0000134headers. If empty, no ruler line is drawn. It defaults to
135\character{=}.
Guido van Rossum8668e8e1998-06-28 17:55:53 +0000136\end{memberdesc}
137
138