This commit was manufactured by cvs2svn to create tag 'r212'.
[python/dscho.git] / Doc / lib / libcmd.tex
blobf24add37f4c7aca96c775a8eaca6036f6276c391
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 response to the
60 prompt. This may be overridden, but should not normally need to be;
61 see the \method{precmd()} and \method{postcmd()} methods for useful
62 execution hooks. The return value is a flag indicating whether
63 interpretation of commands by the interpreter should stop.
64 \end{methoddesc}
66 \begin{methoddesc}{emptyline}{}
67 Method called when an empty line is entered in response to the prompt.
68 If this method is not overridden, it repeats the last nonempty command
69 entered.
70 \end{methoddesc}
72 \begin{methoddesc}{default}{line}
73 Method called on an input line when the command prefix is not
74 recognized. If this method is not overridden, it prints an
75 error message and returns.
76 \end{methoddesc}
78 \begin{methoddesc}{precmd}{}
79 Hook method executed just before the command line \var{line} is
80 interpreted, but after the input prompt is generated and issued. This
81 method is a stub in \class{Cmd}; it exists to be overridden by
82 subclasses. The return value is used as the command which will be
83 executed by the \method{onecmd()} method; the \method{precmd()}
84 implementation may re-write the command or simply return \var{line}
85 unchanged.
86 \end{methoddesc}
88 \begin{methoddesc}{postcmd}{stop, line}
89 Hook method executed just after a command dispatch is finished. This
90 method is a stub in \class{Cmd}; it exists to be overridden by
91 subclasses. \var{line} is the command line which was executed, and
92 \var{stop} is a flag which indicates whether execution will be
93 terminated after the call to \method{postcmd()}; this will be the
94 return value of the \method{onecmd()} method. The return value of
95 this method will be used as the new value for the internal flag which
96 corresponds to \var{stop}; returning false will cause interpretation
97 to continue.
98 \end{methoddesc}
100 \begin{methoddesc}{preloop}{}
101 Hook method executed once when \method{cmdloop()} is called. This
102 method is a stub in \class{Cmd}; it exists to be overridden by
103 subclasses.
104 \end{methoddesc}
106 \begin{methoddesc}{postloop}{}
107 Hook method executed once when \method{cmdloop()} is about to return.
108 This method is a stub in \class{Cmd}; it exists to be overridden by
109 subclasses.
110 \end{methoddesc}
112 Instances of \class{Cmd} subclasses have some public instance variables:
114 \begin{memberdesc}{prompt}
115 The prompt issued to solicit input.
116 \end{memberdesc}
118 \begin{memberdesc}{identchars}
119 The string of characters accepted for the command prefix.
120 \end{memberdesc}
122 \begin{memberdesc}{lastcmd}
123 The last nonempty command prefix seen.
124 \end{memberdesc}
126 \begin{memberdesc}{intro}
127 A string to issue as an intro or banner. May be overridden by giving
128 the \method{cmdloop()} method an argument.
129 \end{memberdesc}
131 \begin{memberdesc}{doc_header}
132 The header to issue if the help output has a section for documented
133 commands.
134 \end{memberdesc}
136 \begin{memberdesc}{misc_header}
137 The header to issue if the help output has a section for miscellaneous
138 help topics (that is, there are \method{help_*()} methods without
139 corresponding \method{do_*()} methods).
140 \end{memberdesc}
142 \begin{memberdesc}{undoc_header}
143 The header to issue if the help output has a section for undocumented
144 commands (that is, there are \method{do_*()} methods without
145 corresponding \method{help_*()} methods).
146 \end{memberdesc}
148 \begin{memberdesc}{ruler}
149 The character used to draw separator lines under the help-message
150 headers. If empty, no ruler line is drawn. It defaults to
151 \character{=}.
152 \end{memberdesc}
154 \begin{memberdesc}{use_rawinput}
155 A flag, defaulting to true. If true, \method{cmdloop()} uses
156 \function{raw_input()} to display a prompt and read the next command;
157 if false, \function{sys.stdout.write()} and
158 \function{sys.stdin.readline()} are used.
159 \end{memberdesc}