1 \section{\module{cmd
} ---
2 Support for line-oriented command interpreters
}
4 \declaremodule{standard
}{cmd
}
5 \sectionauthor{Eric S. Raymond
}{esr@snark.thyrsus.com
}
6 \modulesynopsis{Build line-oriented command interpreters.
}
9 The
\class{Cmd
} class provides a simple framework for writing
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.
14 \begin{classdesc
}{Cmd
}{}
15 A
\class{Cmd
} instance or subclass instance is a line-oriented
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.
22 \subsection{Cmd Objects
}
25 A
\class{Cmd
} instance has the following methods:
27 \begin{methoddesc
}{cmdloop
}{\optional{intro
}}
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.
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).
35 If the
\module{readline
} module is loaded, input will automatically
36 inherit
\program{bash
}-like history-list editing (e.g.
\kbd{Control-P
}
37 scrolls back to the last command,
\kbd{Control-N
} forward to the next
38 one,
\kbd{Control-F
} moves the cursor to the right non-destructively,
39 \kbd{Control-B
} moves the cursor to the left non-destructively, etc.).
41 An end-of-file on input is passed back as the string
\code{'EOF'
}.
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,
45 a line beginning with the character
\character{?
} is dispatched to
46 the method
\method{do_help()
}. As another special case, a line
47 beginning with the character
\character{!
} is dispatched to the
48 method
\method{do_shell
} (if such a method is defined).
50 All subclasses of
\class{Cmd
} inherit a predefined
\method{do_help
}.
51 This method, called with an argument
\code{bar
}, invokes the
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.
58 \begin{methoddesc
}{onecmd
}{str
}
59 Interpret the argument as though it had been typed in in
60 response to the prompt.
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
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.
75 \begin{methoddesc
}{precmd
}{}
76 Hook method executed just before the command line is interpreted, but
77 after the input prompt is generated and issued. This
78 method is a stub in
\class{Cmd
}; it exists to be overridden by
82 \begin{methoddesc
}{postcmd
}{}
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
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
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
100 Instances of
\class{Cmd
} subclasses have some public instance variables:
102 \begin{memberdesc
}{prompt
}
103 The prompt issued to solicit input.
106 \begin{memberdesc
}{identchars
}
107 The string of characters accepted for the command prefix.
110 \begin{memberdesc
}{lastcmd
}
111 The last nonempty command prefix seen.
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.
119 \begin{memberdesc
}{doc_header
}
120 The header to issue if the help output has a section for documented
124 \begin{memberdesc
}{misc_header
}
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).
130 \begin{memberdesc
}{undoc_header
}
131 The header to issue if the help output has a section for undocumented
132 commands (that is, there are
\method{do_*()
} methods without
133 corresponding
\method{help_*()
} methods).
136 \begin{memberdesc
}{ruler
}
137 The character used to draw separator lines under the help-message
138 headers. If empty, no ruler line is drawn. It defaults to
142 \begin{memberdesc
}{use_rawinput
}
143 A flag, defaulting to true. If true,
\method{cmdloop()
} uses
144 \function{raw_input()
} to display a prompt and read the next command;
145 if false,
\function{sys.stdout.write()
} and
146 \function{sys.stdin.readline()
} are used. (This means that by
147 importing
\module{readline
}, on systems that support it, the
148 interpreter will automatically support Emacs-like line editing
149 and command-history keystrokes.)