cmdtools.callback

cmdtools.callback.base

class cmdtools.callback.base.Attributes(attrs: Dict[str, Any] | None = None)

Bases: object

An attributes container

Parameters:

attrs (dict) – A dictionary containing any objects.

class cmdtools.callback.base.Callback(func: Callable)

Bases: BaseCallback

A callback for handling command execution.

Parameters:

func (Callable) – A function to assign as callback.

error(func: Callable) ErrorCallback

Wraps a function and assigns it as an error callback.

Parameters:

func (Callable) – The function to wrap.

Return type:

An ErrorCallback object.

make_context(command: Cmd, attrs: AttributeType = None) Context

Create a new context based on a command object.

Parameters:
  • command (Cmd) – The command object.

  • attrs (AttributeType) – Additional attributes to be passed to the callback context.

Return type:

The context created.

class cmdtools.callback.base.Context(command: Cmd, options: Options | None = None, attrs: AttributeType = None)

Bases: BaseContext

A context to be passed to a Callback when command is executed.

Parameters:
  • command (Cmd) – The command object to be executed.

  • options (Options) – The options of the callback.

  • attrs (AttributeType) – Additional attributes to be passed to the callback context.

Raises:

ConversionError – If converter fails to convert the value of an option.

class cmdtools.callback.base.ErrorCallback(func: Callable)

Bases: BaseCallback

A callback for error handling.

make_context(command: Cmd, error: Exception, attrs: AttributeType = None)

Create a new context based on a command object with the exception that was raised.

Parameters:
  • command (Cmd) – The command object.

  • error (Exception) – The exception that gets raised.

  • attrs (AttributeType) – Additional attributes to be passed to the callback context.

Return type:

The context created with the exception.

class cmdtools.callback.base.ErrorContext(command: Cmd, error: Exception, attrs: AttributeType = None)

Bases: BaseContext

A context to be passed to an error callback.

cmdtools.callback.base.add_option(name: str, *, default: ~typing.Any | None = None, modifier: ~cmdtools.callback.option.OptionModifier = OptionModifier.NoModifier, type: ~typing.Type[int | float | str | bool] = <class 'str'>)

A decorator for adding an option to a callback.

This decorator also wraps the function as a callback if it’s not a Callback.

Parameters:
  • name (str) – The option name.

  • default (str) – Default value if argument is not specified.

  • modifier (OptionModifier) – The option modifier

  • type (BasicType) – Converts the value to the specified type.

Raises:

TypeError – If the target is not a function or a Callback.

cmdtools.callback.base.callback_init(func: Callable) Callback

Wraps a function as Callback.

Parameters:

func (Callable) – The function to wrap.

Return type:

A Callback object.

cmdtools.callback.option

class cmdtools.callback.option.Option(name: str, value: int | float | str | bool | None = None, default: int | float | str | bool | None = None, modifier: ~cmdtools.callback.option.OptionModifier = OptionModifier.NoModifier, type: ~typing.Type[int | float | str | bool] = <class 'str'>)

Bases: object

An option dataclass.

Parameters:
  • name (str) – The name of the option.

  • value (BaseType) – The value of the option.

  • default (BaseType) – The default value of the option.

  • modifier (OptionModifier) – The option modifier, some modifier used to modify the value.

  • type (BasicType) – Converter target type.

default: int | float | str | bool | None = None
modifier: OptionModifier = 'no_modifier'
name: str
type

alias of str

value: int | float | str | bool | None = None
class cmdtools.callback.option.OptionModifier(value)

Bases: Enum

An option modifier.

NoModifier

Does not modify the value.

ConsumeRest

Consume the rest of the arguments in the command

ConsumeRest = 'consume_rest'
NoModifier = 'no_modifier'
class cmdtools.callback.option.Options(options: List[Option] | None = None)

Bases: object

An option container class.

Parameters:

options (List[Option]) – List of options.

add(name: str, default: ~typing.Any | None = None, modifier: ~cmdtools.callback.option.OptionModifier = OptionModifier.NoModifier, append: bool = False, type: ~typing.Type[int | float | str | bool] = <class 'str'>)

Adds an option to the container.

Parameters:
  • name (str) – The option name.

  • default (Any) – The default value.

  • modifier (OptionModifier) – The option modifier.

  • append (bool) – Adds option with append mode.

  • type (BasicType) – Converter target type.

copy()

Creates a new Options instance with copies of all stored options.

get(name: str) Option | None

Gets an option by name.

Parameters:

name (str) – The name of the option.

has_option(name: str) int | None

Checks if the container has an option.

Parameters:

name (str) – The name of the option.