Guido van Rossum | 8668e8e | 1998-06-28 17:55:53 +0000 | [diff] [blame] | 1 | % Documentation by ESR |
| 2 | \section{Standard Module \module{cmd}} |
| 3 | \stmodindex{cmd} |
| 4 | \label{module-cmd} |
| 5 | |
Fred Drake | 579d366 | 1998-07-02 19:35:12 +0000 | [diff] [blame] | 6 | The \class{Cmd} class provides a simple framework for writing |
Guido van Rossum | 8668e8e | 1998-06-28 17:55:53 +0000 | [diff] [blame] | 7 | line-oriented command interpreters. These are often useful for |
| 8 | test harnesses, administrative tools, and prototypes that will |
| 9 | later be wrapped in a more sophisticated interface. |
| 10 | |
| 11 | \begin{classdesc}{Cmd}{} |
| 12 | A \class{Cmd} instance or subclass instance is a line-oriented |
Fred Drake | 579d366 | 1998-07-02 19:35:12 +0000 | [diff] [blame] | 13 | interpreter framework. There is no good reason to instantiate |
| 14 | \class{Cmd} itself; rather, it's useful as a superclass of an |
| 15 | interpreter class you define yourself in order to inherit |
| 16 | \class{Cmd}'s methods and encapsulate action methods. |
Guido van Rossum | 8668e8e | 1998-06-28 17:55:53 +0000 | [diff] [blame] | 17 | \end{classdesc} |
| 18 | |
| 19 | \subsection{Cmd Objects} |
| 20 | \label{Cmd-objects} |
| 21 | |
| 22 | A \class{Cmd} instance has the following methods: |
| 23 | |
Fred Drake | 579d366 | 1998-07-02 19:35:12 +0000 | [diff] [blame] | 24 | \begin{methoddesc}{cmdloop}{\optional{intro}} |
Guido van Rossum | 8668e8e | 1998-06-28 17:55:53 +0000 | [diff] [blame] | 25 | Repeatedly issue a prompt, accept input, parse an initial prefix off |
| 26 | the received input, and dispatch to action methods, passing them the |
| 27 | remainder of the line as argument. |
| 28 | |
| 29 | The optional argument is a banner or intro string to be issued before the |
| 30 | first prompt (this overrides the \member{intro} class member). |
| 31 | |
| 32 | If the \module{readline} module is loaded, input will automatically |
Fred Drake | 579d366 | 1998-07-02 19:35:12 +0000 | [diff] [blame] | 33 | inherit \program{bash}-like history-list editing (e.g. \kbd{Ctrl-P} |
| 34 | scrolls back to the last command, \kbd{Ctrl-N} forward to the next |
| 35 | one, \kbd{Ctrl-F} moves the cursor to the right non-destructively, |
| 36 | \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] | 37 | |
Fred Drake | 579d366 | 1998-07-02 19:35:12 +0000 | [diff] [blame] | 38 | 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] | 39 | |
Fred Drake | 579d366 | 1998-07-02 19:35:12 +0000 | [diff] [blame] | 40 | An interpreter instance will recognize a command name \samp{foo} if |
| 41 | and only if it has a method \method{do_foo()}. As a special case, |
| 42 | a line containing only the character \character{?} is dispatched to |
| 43 | the method \method{do_help()}. As another special case, a line |
| 44 | containing only the character \character{!} is dispatched to the |
| 45 | method \method{do_shell} (if such a method is defined). |
Guido van Rossum | 8668e8e | 1998-06-28 17:55:53 +0000 | [diff] [blame] | 46 | |
| 47 | All subclasses of \class{Cmd} inherit a predefined \method{do_help}. |
| 48 | This method, called with an argument \code{bar}, invokes the |
Fred Drake | 579d366 | 1998-07-02 19:35:12 +0000 | [diff] [blame] | 49 | corresponding method \method{help_bar()}. With no argument, |
| 50 | \method{do_help()} lists all available help topics (that is, all |
| 51 | commands with corresponding \method{help_*()} methods), and also lists |
| 52 | any undocumented commands. |
Guido van Rossum | 8668e8e | 1998-06-28 17:55:53 +0000 | [diff] [blame] | 53 | \end{methoddesc} |
| 54 | |
| 55 | \begin{methoddesc}{onecmd}{str} |
| 56 | Interpret the argument as though it had been typed in in |
| 57 | response to the prompt. |
| 58 | \end{methoddesc} |
| 59 | |
| 60 | \begin{methoddesc}{emptyline}{} |
| 61 | Method called when an empty line is entered in response to the prompt. |
| 62 | If this method is not overridden, it repeats the last nonempty command |
| 63 | entered. |
| 64 | \end{methoddesc} |
| 65 | |
| 66 | \begin{methoddesc}{default}{line} |
| 67 | Method called on an input line when the command prefix is not |
| 68 | recognized. If this method is not overridden, it prints an |
| 69 | error message and returns. |
| 70 | \end{methoddesc} |
| 71 | |
Fred Drake | 579d366 | 1998-07-02 19:35:12 +0000 | [diff] [blame] | 72 | \begin{methoddesc}{precmd}{} |
| 73 | Hook method executed just before the input prompt is issued. This |
| 74 | method is a stub in \class{Cmd}; it exists to be overridden by |
| 75 | subclasses. |
Guido van Rossum | 8668e8e | 1998-06-28 17:55:53 +0000 | [diff] [blame] | 76 | \end{methoddesc} |
| 77 | |
Fred Drake | 579d366 | 1998-07-02 19:35:12 +0000 | [diff] [blame] | 78 | \begin{methoddesc}{postcmd}{} |
Guido van Rossum | 8668e8e | 1998-06-28 17:55:53 +0000 | [diff] [blame] | 79 | Hook method executed just after a command dispatch is finished. This |
| 80 | method is a stub in \class{Cmd}; it exists to be overridden by |
| 81 | subclasses. |
| 82 | \end{methoddesc} |
| 83 | |
Fred Drake | 579d366 | 1998-07-02 19:35:12 +0000 | [diff] [blame] | 84 | \begin{methoddesc}{preloop}{} |
| 85 | Hook method executed once when \method{cmdloop()} is called. This |
| 86 | method is a stub in \class{Cmd}; it exists to be overridden by |
| 87 | subclasses. |
Guido van Rossum | 8668e8e | 1998-06-28 17:55:53 +0000 | [diff] [blame] | 88 | \end{methoddesc} |
| 89 | |
Fred Drake | 579d366 | 1998-07-02 19:35:12 +0000 | [diff] [blame] | 90 | \begin{methoddesc}{postloop}{} |
| 91 | Hook method executed once when \method{cmdloop()} is about to return. |
| 92 | 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] | 93 | subclasses. |
| 94 | \end{methoddesc} |
| 95 | |
| 96 | Instances of \class{Cmd} subclasses have some public instance variables: |
| 97 | |
| 98 | \begin{memberdesc}{prompt} |
| 99 | The prompt issued to solicit input. |
| 100 | \end{memberdesc} |
| 101 | |
| 102 | \begin{memberdesc}{identchars} |
| 103 | The string of characters accepted for the command prefix. |
| 104 | \end{memberdesc} |
| 105 | |
| 106 | \begin{memberdesc}{lastcmd} |
| 107 | The last nonempty command prefix seen. |
| 108 | \end{memberdesc} |
| 109 | |
| 110 | \begin{memberdesc}{intro} |
| 111 | A string to issue as an intro or banner. May be overridden by giving |
| 112 | the \method{cmdloop()} method an argument. |
| 113 | \end{memberdesc} |
| 114 | |
| 115 | \begin{memberdesc}{doc_header} |
Fred Drake | 579d366 | 1998-07-02 19:35:12 +0000 | [diff] [blame] | 116 | The header to issue if the help output has a section for documented |
| 117 | commands. |
Guido van Rossum | 8668e8e | 1998-06-28 17:55:53 +0000 | [diff] [blame] | 118 | \end{memberdesc} |
| 119 | |
| 120 | \begin{memberdesc}{misc_header} |
Fred Drake | 579d366 | 1998-07-02 19:35:12 +0000 | [diff] [blame] | 121 | The header to issue if the help output has a section for miscellaneous |
| 122 | help topics (that is, there are \method{help_*()} methods without |
| 123 | corresponding \method{do_*()} methods). |
Guido van Rossum | 8668e8e | 1998-06-28 17:55:53 +0000 | [diff] [blame] | 124 | \end{memberdesc} |
| 125 | |
| 126 | \begin{memberdesc}{undoc_header} |
| 127 | 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] | 128 | commands (that is, there are \method{do_*()} methods without |
| 129 | corresponding \method{help_*()} methods). |
Guido van Rossum | 8668e8e | 1998-06-28 17:55:53 +0000 | [diff] [blame] | 130 | \end{memberdesc} |
| 131 | |
| 132 | \begin{memberdesc}{ruler} |
| 133 | The character used to draw separator lines under the help-message |
Fred Drake | 579d366 | 1998-07-02 19:35:12 +0000 | [diff] [blame] | 134 | headers. If empty, no ruler line is drawn. It defaults to |
| 135 | \character{=}. |
Guido van Rossum | 8668e8e | 1998-06-28 17:55:53 +0000 | [diff] [blame] | 136 | \end{memberdesc} |
| 137 | |
| 138 | |