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