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