Interactive Command Sessions
The
cmd
module offers a simple way to handle
interactive sessions of commands. Each command is a line of text. The
first word of each command is a verb defining the requested action.
The rest of the line is passed as an argument to the method that
implements the action that the verb requests.
Module cmd
supplies class Cmd
to use as a base class, and you define your own subclass of
cmd.Cmd
. The subclass supplies methods with names
starting with do_
and help_
,
and may also optionally override some of
Cmd
’s methods. When the user
enters a command line such as verb
and
the
rest
, as long as the subclass defines a
method named do_
verb
,
Cmd.onecmd
calls:
self.do_verb
('and
the
rest
')
Similarly, as long as the subclass defines a method named
help_
verb
,
Cmd.do_help
calls it when the command line starts
with either 'help
verb
' or
'?
verb
‘.
Cmd
, by default, also shows suitable error
messages if the user tries to use, or asks for help about, a verb for
which the subclass does not define a needed method.
Methods of Cmd Instances
An instance
c
of a subclass of class
Cmd
supplies the following methods (many of these
methods are meant to be overridden by the subclass).
Attributes of Cmd Instances
An
instance c
of a subclass of class
Cmd
supplies the following attributes:
-
identchars
A string that contains all characters that can be part of a verb; by default,
c
.identchars
contains letters, digits, and underscore (_
)-
intro
The message that
cmdloop
outputs first, ...
Get Python in a Nutshell now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.