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