cmd2.command_definition
cmd2.command_definition
Supports the definition of commands in separate classes to be composed into cmd2.Cmd.
CommandSetType
module-attribute
CommandSet
Base class for defining sets of commands to load in cmd2.
with_default_category can be used to apply a default category to all commands in the CommandSet.
do_, help_, and complete_ functions differ only in that self is the CommandSet instead of the cmd2 app
Private reference to the CLI instance in which this CommandSet running.
This will be set when the CommandSet is registered and it should be accessed by child classes using the self._cmd property.
Source code in cmd2/command_definition.py
settable_prefix
property
Read-only accessor for the underlying private settable_prefix field.
on_register
First step to registering a CommandSet, called by cmd2.Cmd.
The commands defined in this class have not been added to the CLI object at this point. Subclasses can override this to perform any initialization requiring access to the Cmd object (e.g. configure commands and their parsers based on CLI state data).
| PARAMETER | DESCRIPTION |
|---|---|
cmd
|
The cmd2 main application
TYPE:
|
| RAISES | DESCRIPTION |
|---|---|
CommandSetRegistrationError
|
if CommandSet is already registered. |
Source code in cmd2/command_definition.py
on_registered
2nd step to registering, called by cmd2.Cmd after a CommandSet is registered and all its commands have been added.
Subclasses can override this to perform custom steps related to the newly added commands (e.g. setting them to a disabled state).
Source code in cmd2/command_definition.py
on_unregister
First step to unregistering a CommandSet, called by cmd2.Cmd.
Subclasses can override this to perform any cleanup steps which require their commands being registered in the CLI.
on_unregistered
2nd step to unregistering, called by cmd2.Cmd after a CommandSet is unregistered and all its commands removed.
Subclasses can override this to perform remaining cleanup steps.
Source code in cmd2/command_definition.py
add_settable
Add a settable parameter to the CommandSet.
| PARAMETER | DESCRIPTION |
|---|---|
settable
|
Settable object being added
TYPE:
|
Source code in cmd2/command_definition.py
remove_settable
Remove a settable parameter from the CommandSet.
| PARAMETER | DESCRIPTION |
|---|---|
name
|
name of the settable being removed
TYPE:
|
| RAISES | DESCRIPTION |
|---|---|
KeyError
|
if the Settable matches this name |
Source code in cmd2/command_definition.py
sigint_handler
Handle a SIGINT that occurred for a command in this CommandSet.
| RETURNS | DESCRIPTION |
|---|---|
bool
|
True if this completes the interrupt handling and no KeyboardInterrupt will be raised. False to raise a KeyboardInterrupt. |
Source code in cmd2/command_definition.py
with_default_category
Apply a category to all do_* command methods in a class that do not already have a category specified (Decorator).
CommandSets that are decorated by this with heritable set to True (default) will set a class attribute that is
inherited by all subclasses unless overridden. All commands of this CommandSet and all subclasses of this CommandSet
that do not declare an explicit category will be placed in this category. Subclasses may use this decorator to
override the default category.
If heritable is set to False, then only the commands declared locally to this CommandSet will be placed in the
specified category. Dynamically created commands and commands declared in sub-classes will not receive this
category.
| PARAMETER | DESCRIPTION |
|---|---|
category
|
category to put all uncategorized commands in
TYPE:
|
heritable
|
Flag whether this default category should apply to sub-classes. Defaults to True
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Callable[[CommandSetType], CommandSetType]
|
decorator function |