| 1 | \section{\module{cmd} ---
|
|---|
| 2 | Support for line-oriented command interpreters}
|
|---|
| 3 |
|
|---|
| 4 | \declaremodule{standard}{cmd}
|
|---|
| 5 | \sectionauthor{Eric S. Raymond}{[email protected]}
|
|---|
| 6 | \modulesynopsis{Build line-oriented command interpreters.}
|
|---|
| 7 |
|
|---|
| 8 |
|
|---|
| 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.
|
|---|
| 13 |
|
|---|
| 14 | \begin{classdesc}{Cmd}{\optional{completekey\optional{,
|
|---|
| 15 | stdin\optional{, stdout}}}}
|
|---|
| 16 | A \class{Cmd} instance or subclass instance is a line-oriented
|
|---|
| 17 | interpreter framework. There is no good reason to instantiate
|
|---|
| 18 | \class{Cmd} itself; rather, it's useful as a superclass of an
|
|---|
| 19 | interpreter class you define yourself in order to inherit
|
|---|
| 20 | \class{Cmd}'s methods and encapsulate action methods.
|
|---|
| 21 |
|
|---|
| 22 | The optional argument \var{completekey} is the \refmodule{readline} name
|
|---|
| 23 | of a completion key; it defaults to \kbd{Tab}. If \var{completekey} is
|
|---|
| 24 | not \constant{None} and \refmodule{readline} is available, command completion
|
|---|
| 25 | is done automatically.
|
|---|
| 26 |
|
|---|
| 27 | The optional arguments \var{stdin} and \var{stdout} specify the
|
|---|
| 28 | input and output file objects that the Cmd instance or subclass
|
|---|
| 29 | instance will use for input and output. If not specified, they
|
|---|
| 30 | will default to \var{sys.stdin} and \var{sys.stdout}.
|
|---|
| 31 |
|
|---|
| 32 | \versionchanged[The \var{stdin} and \var{stdout} parameters were added]{2.3}
|
|---|
| 33 | \end{classdesc}
|
|---|
| 34 |
|
|---|
| 35 | \subsection{Cmd Objects}
|
|---|
| 36 | \label{Cmd-objects}
|
|---|
| 37 |
|
|---|
| 38 | A \class{Cmd} instance has the following methods:
|
|---|
| 39 |
|
|---|
| 40 | \begin{methoddesc}{cmdloop}{\optional{intro}}
|
|---|
| 41 | Repeatedly issue a prompt, accept input, parse an initial prefix off
|
|---|
| 42 | the received input, and dispatch to action methods, passing them the
|
|---|
| 43 | remainder of the line as argument.
|
|---|
| 44 |
|
|---|
| 45 | The optional argument is a banner or intro string to be issued before the
|
|---|
| 46 | first prompt (this overrides the \member{intro} class member).
|
|---|
| 47 |
|
|---|
| 48 | If the \refmodule{readline} module is loaded, input will automatically
|
|---|
| 49 | inherit \program{bash}-like history-list editing (e.g. \kbd{Control-P}
|
|---|
| 50 | scrolls back to the last command, \kbd{Control-N} forward to the next
|
|---|
| 51 | one, \kbd{Control-F} moves the cursor to the right non-destructively,
|
|---|
| 52 | \kbd{Control-B} moves the cursor to the left non-destructively, etc.).
|
|---|
| 53 |
|
|---|
| 54 | An end-of-file on input is passed back as the string \code{'EOF'}.
|
|---|
| 55 |
|
|---|
| 56 | An interpreter instance will recognize a command name \samp{foo} if
|
|---|
| 57 | and only if it has a method \method{do_foo()}. As a special case,
|
|---|
| 58 | a line beginning with the character \character{?} is dispatched to
|
|---|
| 59 | the method \method{do_help()}. As another special case, a line
|
|---|
| 60 | beginning with the character \character{!} is dispatched to the
|
|---|
| 61 | method \method{do_shell()} (if such a method is defined).
|
|---|
| 62 |
|
|---|
| 63 | This method will return when the \method{postcmd()} method returns a
|
|---|
| 64 | true value. The \var{stop} argument to \method{postcmd()} is the
|
|---|
| 65 | return value from the command's corresponding \method{do_*()} method.
|
|---|
| 66 |
|
|---|
| 67 | If completion is enabled, completing commands will be done
|
|---|
| 68 | automatically, and completing of commands args is done by calling
|
|---|
| 69 | \method{complete_foo()} with arguments \var{text}, \var{line},
|
|---|
| 70 | \var{begidx}, and \var{endidx}. \var{text} is the string prefix we
|
|---|
| 71 | are attempting to match: all returned matches must begin with it.
|
|---|
| 72 | \var{line} is the current input line with leading whitespace removed,
|
|---|
| 73 | \var{begidx} and \var{endidx} are the beginning and ending indexes
|
|---|
| 74 | of the prefix text, which could be used to provide different
|
|---|
| 75 | completion depending upon which position the argument is in.
|
|---|
| 76 |
|
|---|
| 77 | All subclasses of \class{Cmd} inherit a predefined \method{do_help()}.
|
|---|
| 78 | This method, called with an argument \code{'bar'}, invokes the
|
|---|
| 79 | corresponding method \method{help_bar()}. With no argument,
|
|---|
| 80 | \method{do_help()} lists all available help topics (that is, all
|
|---|
| 81 | commands with corresponding \method{help_*()} methods), and also lists
|
|---|
| 82 | any undocumented commands.
|
|---|
| 83 | \end{methoddesc}
|
|---|
| 84 |
|
|---|
| 85 | \begin{methoddesc}{onecmd}{str}
|
|---|
| 86 | Interpret the argument as though it had been typed in response to the
|
|---|
| 87 | prompt. This may be overridden, but should not normally need to be;
|
|---|
| 88 | see the \method{precmd()} and \method{postcmd()} methods for useful
|
|---|
| 89 | execution hooks. The return value is a flag indicating whether
|
|---|
| 90 | interpretation of commands by the interpreter should stop. If there
|
|---|
| 91 | is a \method{do_*()} method for the command \var{str}, the return
|
|---|
| 92 | value of that method is returned, otherwise the return value from the
|
|---|
| 93 | \method{default()} method is returned.
|
|---|
| 94 | \end{methoddesc}
|
|---|
| 95 |
|
|---|
| 96 | \begin{methoddesc}{emptyline}{}
|
|---|
| 97 | Method called when an empty line is entered in response to the prompt.
|
|---|
| 98 | If this method is not overridden, it repeats the last nonempty command
|
|---|
| 99 | entered.
|
|---|
| 100 | \end{methoddesc}
|
|---|
| 101 |
|
|---|
| 102 | \begin{methoddesc}{default}{line}
|
|---|
| 103 | Method called on an input line when the command prefix is not
|
|---|
| 104 | recognized. If this method is not overridden, it prints an
|
|---|
| 105 | error message and returns.
|
|---|
| 106 | \end{methoddesc}
|
|---|
| 107 |
|
|---|
| 108 | \begin{methoddesc}{completedefault}{text, line, begidx, endidx}
|
|---|
| 109 | Method called to complete an input line when no command-specific
|
|---|
| 110 | \method{complete_*()} method is available. By default, it returns an
|
|---|
| 111 | empty list.
|
|---|
| 112 | \end{methoddesc}
|
|---|
| 113 |
|
|---|
| 114 | \begin{methoddesc}{precmd}{line}
|
|---|
| 115 | Hook method executed just before the command line \var{line} is
|
|---|
| 116 | interpreted, but after the input prompt is generated and issued. This
|
|---|
| 117 | method is a stub in \class{Cmd}; it exists to be overridden by
|
|---|
| 118 | subclasses. The return value is used as the command which will be
|
|---|
| 119 | executed by the \method{onecmd()} method; the \method{precmd()}
|
|---|
| 120 | implementation may re-write the command or simply return \var{line}
|
|---|
| 121 | unchanged.
|
|---|
| 122 | \end{methoddesc}
|
|---|
| 123 |
|
|---|
| 124 | \begin{methoddesc}{postcmd}{stop, line}
|
|---|
| 125 | Hook method executed just after a command dispatch is finished. This
|
|---|
| 126 | method is a stub in \class{Cmd}; it exists to be overridden by
|
|---|
| 127 | subclasses. \var{line} is the command line which was executed, and
|
|---|
| 128 | \var{stop} is a flag which indicates whether execution will be
|
|---|
| 129 | terminated after the call to \method{postcmd()}; this will be the
|
|---|
| 130 | return value of the \method{onecmd()} method. The return value of
|
|---|
| 131 | this method will be used as the new value for the internal flag which
|
|---|
| 132 | corresponds to \var{stop}; returning false will cause interpretation
|
|---|
| 133 | to continue.
|
|---|
| 134 | \end{methoddesc}
|
|---|
| 135 |
|
|---|
| 136 | \begin{methoddesc}{preloop}{}
|
|---|
| 137 | Hook method executed once when \method{cmdloop()} is called. This
|
|---|
| 138 | method is a stub in \class{Cmd}; it exists to be overridden by
|
|---|
| 139 | subclasses.
|
|---|
| 140 | \end{methoddesc}
|
|---|
| 141 |
|
|---|
| 142 | \begin{methoddesc}{postloop}{}
|
|---|
| 143 | Hook method executed once when \method{cmdloop()} is about to return.
|
|---|
| 144 | This method is a stub in \class{Cmd}; it exists to be overridden by
|
|---|
| 145 | subclasses.
|
|---|
| 146 | \end{methoddesc}
|
|---|
| 147 |
|
|---|
| 148 | Instances of \class{Cmd} subclasses have some public instance variables:
|
|---|
| 149 |
|
|---|
| 150 | \begin{memberdesc}{prompt}
|
|---|
| 151 | The prompt issued to solicit input.
|
|---|
| 152 | \end{memberdesc}
|
|---|
| 153 |
|
|---|
| 154 | \begin{memberdesc}{identchars}
|
|---|
| 155 | The string of characters accepted for the command prefix.
|
|---|
| 156 | \end{memberdesc}
|
|---|
| 157 |
|
|---|
| 158 | \begin{memberdesc}{lastcmd}
|
|---|
| 159 | The last nonempty command prefix seen.
|
|---|
| 160 | \end{memberdesc}
|
|---|
| 161 |
|
|---|
| 162 | \begin{memberdesc}{intro}
|
|---|
| 163 | A string to issue as an intro or banner. May be overridden by giving
|
|---|
| 164 | the \method{cmdloop()} method an argument.
|
|---|
| 165 | \end{memberdesc}
|
|---|
| 166 |
|
|---|
| 167 | \begin{memberdesc}{doc_header}
|
|---|
| 168 | The header to issue if the help output has a section for documented
|
|---|
| 169 | commands.
|
|---|
| 170 | \end{memberdesc}
|
|---|
| 171 |
|
|---|
| 172 | \begin{memberdesc}{misc_header}
|
|---|
| 173 | The header to issue if the help output has a section for miscellaneous
|
|---|
| 174 | help topics (that is, there are \method{help_*()} methods without
|
|---|
| 175 | corresponding \method{do_*()} methods).
|
|---|
| 176 | \end{memberdesc}
|
|---|
| 177 |
|
|---|
| 178 | \begin{memberdesc}{undoc_header}
|
|---|
| 179 | The header to issue if the help output has a section for undocumented
|
|---|
| 180 | commands (that is, there are \method{do_*()} methods without
|
|---|
| 181 | corresponding \method{help_*()} methods).
|
|---|
| 182 | \end{memberdesc}
|
|---|
| 183 |
|
|---|
| 184 | \begin{memberdesc}{ruler}
|
|---|
| 185 | The character used to draw separator lines under the help-message
|
|---|
| 186 | headers. If empty, no ruler line is drawn. It defaults to
|
|---|
| 187 | \character{=}.
|
|---|
| 188 | \end{memberdesc}
|
|---|
| 189 |
|
|---|
| 190 | \begin{memberdesc}{use_rawinput}
|
|---|
| 191 | A flag, defaulting to true. If true, \method{cmdloop()} uses
|
|---|
| 192 | \function{raw_input()} to display a prompt and read the next command;
|
|---|
| 193 | if false, \method{sys.stdout.write()} and
|
|---|
| 194 | \method{sys.stdin.readline()} are used. (This means that by
|
|---|
| 195 | importing \refmodule{readline}, on systems that support it, the
|
|---|
| 196 | interpreter will automatically support \program{Emacs}-like line editing
|
|---|
| 197 | and command-history keystrokes.)
|
|---|
| 198 | \end{memberdesc}
|
|---|