cmd2.command_definition

Supports the definition of commands in separate classes to be composed into cmd2.Cmd

cmd2.command_definition.CommandFunc = typing.Callable[..., typing.Union[bool, NoneType]]

Callable signature for a basic command function Further refinements are needed to define the input parameters

class cmd2.command_definition.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

add_settable(settable: cmd2.utils.Settable) → None

Convenience method to add a settable parameter to the CommandSet

Parameters:settable – Settable object being added
on_register(cmd: cmd2.Cmd) → None

Called by cmd2.Cmd as the first step to registering a CommandSet. 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).

Parameters:cmd – The cmd2 main application
on_registered() → None

Called by cmd2.Cmd after a CommandSet is registered and all its commands have been added to the CLI. Subclasses can override this to perform custom steps related to the newly added commands (e.g. setting them to a disabled state).

on_unregister() → None

Called by cmd2.Cmd as the first step to unregistering a CommandSet. Subclasses can override this to perform any cleanup steps which require their commands being registered in the CLI.

on_unregistered() → None

Called by cmd2.Cmd after a CommandSet has been unregistered and all its commands removed from the CLI. Subclasses can override this to perform remaining cleanup steps.

remove_settable(name: str) → None

Convenience method for removing a settable parameter from the CommandSet

Parameters:name – name of the settable being removed
Raises:KeyError if the Settable matches this name
cmd2.command_definition.with_default_category(category: str, *, heritable: bool = True) → Callable[[Type[cmd2.command_definition.CommandSet]], Type[cmd2.command_definition.CommandSet]]

Decorator that applies a category to all do_* command methods in a class that do not already have a category specified.

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.

Parameters:
  • category – category to put all uncategorized commands in
  • heritable – Flag whether this default category should apply to sub-classes. Defaults to True
Returns:

decorator function