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 | |
| 14 | \begin{classdesc}{Cmd}{} |
| 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. |
Guido van Rossum | 8668e8e | 1998-06-28 17:55:53 +0000 | [diff] [blame] | 20 | \end{classdesc} |
| 21 | |
| 22 | \subsection{Cmd Objects} |
| 23 | \label{Cmd-objects} |
| 24 | |
| 25 | A \class{Cmd} instance has the following methods: |
| 26 | |
Fred Drake | 579d366 | 1998-07-02 19:35:12 +0000 | [diff] [blame] | 27 | \begin{methoddesc}{cmdloop}{\optional{intro}} |
Guido van Rossum | 8668e8e | 1998-06-28 17:55:53 +0000 | [diff] [blame] | 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. |
| 31 | |
| 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). |
| 34 | |
| 35 | If the \module{readline} module is loaded, input will automatically |
Fred Drake | 579d366 | 1998-07-02 19:35:12 +0000 | [diff] [blame] | 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.). |
Guido van Rossum | 8668e8e | 1998-06-28 17:55:53 +0000 | [diff] [blame] | 40 | |
Fred Drake | 579d366 | 1998-07-02 19:35:12 +0000 | [diff] [blame] | 41 | 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] | 42 | |
Fred Drake | 579d366 | 1998-07-02 19:35:12 +0000 | [diff] [blame] | 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, |
Eric S. Raymond | 7ae3a5e | 2000-07-12 02:56:15 +0000 | [diff] [blame] | 45 | a line beginning with the character \character{?} is dispatched to |
Fred Drake | 579d366 | 1998-07-02 19:35:12 +0000 | [diff] [blame] | 46 | the method \method{do_help()}. As another special case, a line |
Eric S. Raymond | 7ae3a5e | 2000-07-12 02:56:15 +0000 | [diff] [blame] | 47 | beginning with the character \character{!} is dispatched to the |
Fred Drake | 579d366 | 1998-07-02 19:35:12 +0000 | [diff] [blame] | 48 | method \method{do_shell} (if such a method is defined). |
Guido van Rossum | 8668e8e | 1998-06-28 17:55:53 +0000 | [diff] [blame] | 49 | |
| 50 | All subclasses of \class{Cmd} inherit a predefined \method{do_help}. |
| 51 | This method, called with an argument \code{bar}, invokes the |
Fred Drake | 579d366 | 1998-07-02 19:35:12 +0000 | [diff] [blame] | 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. |
Guido van Rossum | 8668e8e | 1998-06-28 17:55:53 +0000 | [diff] [blame] | 56 | \end{methoddesc} |
| 57 | |
| 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} |
| 62 | |
| 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} |
| 68 | |
| 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} |
| 74 | |
Fred Drake | 579d366 | 1998-07-02 19:35:12 +0000 | [diff] [blame] | 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. |
Guido van Rossum | 8668e8e | 1998-06-28 17:55:53 +0000 | [diff] [blame] | 79 | \end{methoddesc} |
| 80 | |
Fred Drake | 579d366 | 1998-07-02 19:35:12 +0000 | [diff] [blame] | 81 | \begin{methoddesc}{postcmd}{} |
Guido van Rossum | 8668e8e | 1998-06-28 17:55:53 +0000 | [diff] [blame] | 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} |
| 86 | |
Fred Drake | 579d366 | 1998-07-02 19:35:12 +0000 | [diff] [blame] | 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. |
Guido van Rossum | 8668e8e | 1998-06-28 17:55:53 +0000 | [diff] [blame] | 91 | \end{methoddesc} |
| 92 | |
Fred Drake | 579d366 | 1998-07-02 19:35:12 +0000 | [diff] [blame] | 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 |
Guido van Rossum | 8668e8e | 1998-06-28 17:55:53 +0000 | [diff] [blame] | 96 | subclasses. |
| 97 | \end{methoddesc} |
| 98 | |
| 99 | Instances of \class{Cmd} subclasses have some public instance variables: |
| 100 | |
| 101 | \begin{memberdesc}{prompt} |
| 102 | The prompt issued to solicit input. |
| 103 | \end{memberdesc} |
| 104 | |
| 105 | \begin{memberdesc}{identchars} |
| 106 | The string of characters accepted for the command prefix. |
| 107 | \end{memberdesc} |
| 108 | |
| 109 | \begin{memberdesc}{lastcmd} |
| 110 | The last nonempty command prefix seen. |
| 111 | \end{memberdesc} |
| 112 | |
| 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} |
| 117 | |
| 118 | \begin{memberdesc}{doc_header} |
Fred Drake | 579d366 | 1998-07-02 19:35:12 +0000 | [diff] [blame] | 119 | The header to issue if the help output has a section for documented |
| 120 | commands. |
Guido van Rossum | 8668e8e | 1998-06-28 17:55:53 +0000 | [diff] [blame] | 121 | \end{memberdesc} |
| 122 | |
| 123 | \begin{memberdesc}{misc_header} |
Fred Drake | 579d366 | 1998-07-02 19:35:12 +0000 | [diff] [blame] | 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). |
Guido van Rossum | 8668e8e | 1998-06-28 17:55:53 +0000 | [diff] [blame] | 127 | \end{memberdesc} |
| 128 | |
| 129 | \begin{memberdesc}{undoc_header} |
| 130 | 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] | 131 | commands (that is, there are \method{do_*()} methods without |
| 132 | corresponding \method{help_*()} methods). |
Guido van Rossum | 8668e8e | 1998-06-28 17:55:53 +0000 | [diff] [blame] | 133 | \end{memberdesc} |
| 134 | |
| 135 | \begin{memberdesc}{ruler} |
| 136 | The character used to draw separator lines under the help-message |
Fred Drake | 579d366 | 1998-07-02 19:35:12 +0000 | [diff] [blame] | 137 | headers. If empty, no ruler line is drawn. It defaults to |
| 138 | \character{=}. |
Guido van Rossum | 8668e8e | 1998-06-28 17:55:53 +0000 | [diff] [blame] | 139 | \end{memberdesc} |
| 140 | |
| 141 | |