argclz.dispatch.core.Dispatch#

class argclz.dispatch.core.Dispatch[source]#

Bases: object

A dispatch() functions container that it is able to find and run the target function by corresponding name (command here).

Example

>>> from argclz.dispatch import Dispatch, dispatch
...     class Main(Dispatch):
...         @dispatch('A')
...         def run_a(self): ...
... Main().invoke_command('A')
__init__()#

Methods Summary

__init__()

build_command_usages([group, show_para, ...])

Build a help document for dispatch() functions in this class.

find_command(command[, group])

find dispatch() function according to command.

invoke_command(command, *args, **kwargs)

invoke a dispatch() function in the default group.

invoke_group_command(group, command, *args, ...)

invoke a dispatch() function in a certain group.

list_commands([group, all])

list all dispatch() functions.

Details

classmethod list_commands(group=Ellipsis, *, all=False)[source]#

list all dispatch() functions.

Parameters:
Returns:

list of DispatchCommand

Return type:

list[DispatchCommand]

classmethod find_command(command, group=Ellipsis)[source]#

find dispatch() function according to command.

Parameters:
Returns:

found DispatchCommand

Return type:

DispatchCommand | None

invoke_command(command, *args, **kwargs)[source]#

invoke a dispatch() function in the default group.

Parameters:
  • command (str) – command or one of command’s aliases

  • args – positional arguments of the target function

  • kwargs – keyword arguments of the target function

Returns:

target function’s return

Raises:

DispatchCommandNotFound

Return type:

Any

invoke_group_command(group, command, *args, **kwargs)[source]#

invoke a dispatch() function in a certain group.

Parameters:
  • group (str | DispatchGroup | BoundDispatchGroup) – dispatch group

  • command (str) – command or one of command’s aliases

  • args – positional arguments of the target function

  • kwargs – keyword arguments of the target function

Returns:

target function’s return

Raises:

DispatchCommandNotFound

Return type:

Any

classmethod build_command_usages(group=None, *, show_para=False, width=120, doc_indent=20)[source]#

Build a help document for dispatch() functions in this class.

Parameters:
  • group (str | None) – for functions in the group.

  • show_para (bool) – show parameters.

  • width (int) – text-wrap width.

  • doc_indent (int) – description indent.

Returns:

help document.

Return type:

str