Command Manager#
- class ignis.command_manager.CommandManager(*args: Any, **kwargs: Any)#
A class for managing custom commands. A command is a function that accepts
*args: str
as args and optionally returns astr
as output.Example usage:
from ignis.command_manager import CommandManager command_manager = CommandManager.get_default() # Add a command command_manager.add_command("command-name", lambda *_: "output message") command_manager.add_command( "command-name", lambda fmt="%H:%M", *_: set_format(fmt) ) # Add a command by decorator. # "name" is optional, defaults to the function name. @command_manager.command(name="foobar") def foo_bar(*_) -> None: pass # A regular form of callbacks @command_manager.command(name="regular-command") def regular_callback( arg1: str, arg_optional: str | None = None, *args: str ) -> str | None: if arg1 == "Hello": return "world!" if arg_optional: return f"arg_optional: {arg_optional}" return None # Nothing will be printed in CLI # Remove a command by name command_manager.remove_command("command-name") # Run a command command_manager.run_command("command-name") # Run with args and output output = command_manager.run_command("command-name", *["arg1", "arg2"]) output = command_manager.run_command("command-name", "arg1", "arg2") # Run from the `ignis` CLI: # $ ignis run-command command-name # $ ignis run-command command-name arg1 arg2 # Get the callback of a command callback = command_manager.get_command("command-name")
- remove_command(command_name: str) None #
Remove a command by its name.
- Parameters:
command_name (
str
) -- The command's name.- Raises:
CommandNotFoundError -- If a command with the given name does not exist.
- Return type:
- run_command(command_name: str, *command_args: str) str | None #
Run a command by its name.
- Parameters:
- Raises:
CommandNotFoundError -- If a command with the given name does not exist.
Exception -- If the given arguments don't match the command's callback, or if the callback raises an arbitrary Exception.
- Return type: