Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1 | |
| 2 | :mod:`cmd` --- Support for line-oriented command interpreters |
| 3 | ============================================================= |
| 4 | |
| 5 | .. module:: cmd |
| 6 | :synopsis: Build line-oriented command interpreters. |
| 7 | .. sectionauthor:: Eric S. Raymond <esr@snark.thyrsus.com> |
| 8 | |
| 9 | |
| 10 | The :class:`Cmd` class provides a simple framework for writing line-oriented |
| 11 | command interpreters. These are often useful for test harnesses, administrative |
| 12 | tools, and prototypes that will later be wrapped in a more sophisticated |
| 13 | interface. |
| 14 | |
| 15 | |
| 16 | .. class:: Cmd([completekey[, stdin[, stdout]]]) |
| 17 | |
| 18 | A :class:`Cmd` instance or subclass instance is a line-oriented interpreter |
| 19 | framework. There is no good reason to instantiate :class:`Cmd` itself; rather, |
| 20 | it's useful as a superclass of an interpreter class you define yourself in order |
| 21 | to inherit :class:`Cmd`'s methods and encapsulate action methods. |
| 22 | |
| 23 | The optional argument *completekey* is the :mod:`readline` name of a completion |
| 24 | key; it defaults to :kbd:`Tab`. If *completekey* is not :const:`None` and |
| 25 | :mod:`readline` is available, command completion is done automatically. |
| 26 | |
| 27 | The optional arguments *stdin* and *stdout* specify the input and output file |
| 28 | objects that the Cmd instance or subclass instance will use for input and |
Georg Brandl | 0c77a82 | 2008-06-10 16:37:50 +0000 | [diff] [blame] | 29 | output. If not specified, they will default to :data:`sys.stdin` and |
| 30 | :data:`sys.stdout`. |
| 31 | |
| 32 | If you want a given *stdin* to be used, make sure to set the instance's |
| 33 | :attr:`use_rawinput` attribute to ``False``, otherwise *stdin* will be |
| 34 | ignored. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 35 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 36 | |
| 37 | .. _cmd-objects: |
| 38 | |
| 39 | Cmd Objects |
| 40 | ----------- |
| 41 | |
| 42 | A :class:`Cmd` instance has the following methods: |
| 43 | |
| 44 | |
| 45 | .. method:: Cmd.cmdloop([intro]) |
| 46 | |
| 47 | Repeatedly issue a prompt, accept input, parse an initial prefix off the |
| 48 | received input, and dispatch to action methods, passing them the remainder of |
| 49 | the line as argument. |
| 50 | |
| 51 | The optional argument is a banner or intro string to be issued before the first |
| 52 | prompt (this overrides the :attr:`intro` class member). |
| 53 | |
| 54 | If the :mod:`readline` module is loaded, input will automatically inherit |
| 55 | :program:`bash`\ -like history-list editing (e.g. :kbd:`Control-P` scrolls back |
| 56 | to the last command, :kbd:`Control-N` forward to the next one, :kbd:`Control-F` |
| 57 | moves the cursor to the right non-destructively, :kbd:`Control-B` moves the |
| 58 | cursor to the left non-destructively, etc.). |
| 59 | |
| 60 | An end-of-file on input is passed back as the string ``'EOF'``. |
| 61 | |
| 62 | An interpreter instance will recognize a command name ``foo`` if and only if it |
| 63 | has a method :meth:`do_foo`. As a special case, a line beginning with the |
| 64 | character ``'?'`` is dispatched to the method :meth:`do_help`. As another |
| 65 | special case, a line beginning with the character ``'!'`` is dispatched to the |
| 66 | method :meth:`do_shell` (if such a method is defined). |
| 67 | |
| 68 | This method will return when the :meth:`postcmd` method returns a true value. |
| 69 | The *stop* argument to :meth:`postcmd` is the return value from the command's |
| 70 | corresponding :meth:`do_\*` method. |
| 71 | |
| 72 | If completion is enabled, completing commands will be done automatically, and |
| 73 | completing of commands args is done by calling :meth:`complete_foo` with |
| 74 | arguments *text*, *line*, *begidx*, and *endidx*. *text* is the string prefix |
| 75 | we are attempting to match: all returned matches must begin with it. *line* is |
| 76 | the current input line with leading whitespace removed, *begidx* and *endidx* |
| 77 | are the beginning and ending indexes of the prefix text, which could be used to |
| 78 | provide different completion depending upon which position the argument is in. |
| 79 | |
| 80 | All subclasses of :class:`Cmd` inherit a predefined :meth:`do_help`. This |
| 81 | method, called with an argument ``'bar'``, invokes the corresponding method |
| 82 | :meth:`help_bar`. With no argument, :meth:`do_help` lists all available help |
| 83 | topics (that is, all commands with corresponding :meth:`help_\*` methods), and |
| 84 | also lists any undocumented commands. |
| 85 | |
| 86 | |
| 87 | .. method:: Cmd.onecmd(str) |
| 88 | |
| 89 | Interpret the argument as though it had been typed in response to the prompt. |
| 90 | This may be overridden, but should not normally need to be; see the |
| 91 | :meth:`precmd` and :meth:`postcmd` methods for useful execution hooks. The |
| 92 | return value is a flag indicating whether interpretation of commands by the |
| 93 | interpreter should stop. If there is a :meth:`do_\*` method for the command |
| 94 | *str*, the return value of that method is returned, otherwise the return value |
| 95 | from the :meth:`default` method is returned. |
| 96 | |
| 97 | |
| 98 | .. method:: Cmd.emptyline() |
| 99 | |
| 100 | Method called when an empty line is entered in response to the prompt. If this |
| 101 | method is not overridden, it repeats the last nonempty command entered. |
| 102 | |
| 103 | |
| 104 | .. method:: Cmd.default(line) |
| 105 | |
| 106 | Method called on an input line when the command prefix is not recognized. If |
| 107 | this method is not overridden, it prints an error message and returns. |
| 108 | |
| 109 | |
| 110 | .. method:: Cmd.completedefault(text, line, begidx, endidx) |
| 111 | |
| 112 | Method called to complete an input line when no command-specific |
| 113 | :meth:`complete_\*` method is available. By default, it returns an empty list. |
| 114 | |
| 115 | |
| 116 | .. method:: Cmd.precmd(line) |
| 117 | |
| 118 | Hook method executed just before the command line *line* is interpreted, but |
| 119 | after the input prompt is generated and issued. This method is a stub in |
| 120 | :class:`Cmd`; it exists to be overridden by subclasses. The return value is |
| 121 | used as the command which will be executed by the :meth:`onecmd` method; the |
| 122 | :meth:`precmd` implementation may re-write the command or simply return *line* |
| 123 | unchanged. |
| 124 | |
| 125 | |
| 126 | .. method:: Cmd.postcmd(stop, line) |
| 127 | |
| 128 | Hook method executed just after a command dispatch is finished. This method is |
| 129 | a stub in :class:`Cmd`; it exists to be overridden by subclasses. *line* is the |
| 130 | command line which was executed, and *stop* is a flag which indicates whether |
| 131 | execution will be terminated after the call to :meth:`postcmd`; this will be the |
| 132 | return value of the :meth:`onecmd` method. The return value of this method will |
| 133 | be used as the new value for the internal flag which corresponds to *stop*; |
| 134 | returning false will cause interpretation to continue. |
| 135 | |
| 136 | |
| 137 | .. method:: Cmd.preloop() |
| 138 | |
| 139 | Hook method executed once when :meth:`cmdloop` is called. This method is a stub |
| 140 | in :class:`Cmd`; it exists to be overridden by subclasses. |
| 141 | |
| 142 | |
| 143 | .. method:: Cmd.postloop() |
| 144 | |
| 145 | Hook method executed once when :meth:`cmdloop` is about to return. This method |
| 146 | is a stub in :class:`Cmd`; it exists to be overridden by subclasses. |
| 147 | |
| 148 | Instances of :class:`Cmd` subclasses have some public instance variables: |
| 149 | |
| 150 | |
| 151 | .. attribute:: Cmd.prompt |
| 152 | |
| 153 | The prompt issued to solicit input. |
| 154 | |
| 155 | |
| 156 | .. attribute:: Cmd.identchars |
| 157 | |
| 158 | The string of characters accepted for the command prefix. |
| 159 | |
| 160 | |
| 161 | .. attribute:: Cmd.lastcmd |
| 162 | |
| 163 | The last nonempty command prefix seen. |
| 164 | |
| 165 | |
| 166 | .. attribute:: Cmd.intro |
| 167 | |
| 168 | A string to issue as an intro or banner. May be overridden by giving the |
| 169 | :meth:`cmdloop` method an argument. |
| 170 | |
| 171 | |
| 172 | .. attribute:: Cmd.doc_header |
| 173 | |
| 174 | The header to issue if the help output has a section for documented commands. |
| 175 | |
| 176 | |
| 177 | .. attribute:: Cmd.misc_header |
| 178 | |
| 179 | The header to issue if the help output has a section for miscellaneous help |
| 180 | topics (that is, there are :meth:`help_\*` methods without corresponding |
| 181 | :meth:`do_\*` methods). |
| 182 | |
| 183 | |
| 184 | .. attribute:: Cmd.undoc_header |
| 185 | |
| 186 | The header to issue if the help output has a section for undocumented commands |
| 187 | (that is, there are :meth:`do_\*` methods without corresponding :meth:`help_\*` |
| 188 | methods). |
| 189 | |
| 190 | |
| 191 | .. attribute:: Cmd.ruler |
| 192 | |
| 193 | The character used to draw separator lines under the help-message headers. If |
| 194 | empty, no ruler line is drawn. It defaults to ``'='``. |
| 195 | |
| 196 | |
| 197 | .. attribute:: Cmd.use_rawinput |
| 198 | |
| 199 | A flag, defaulting to true. If true, :meth:`cmdloop` uses :func:`input` to |
| 200 | display a prompt and read the next command; if false, :meth:`sys.stdout.write` |
| 201 | and :meth:`sys.stdin.readline` are used. (This means that by importing |
| 202 | :mod:`readline`, on systems that support it, the interpreter will automatically |
| 203 | support :program:`Emacs`\ -like line editing and command-history keystrokes.) |
| 204 | |