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