Skip to content

Reference

Command

Command(
    name: str,
    handler: callable,
    description: Optional[str] = None,
    usage: Optional[str] = None,
    options: Optional[List[Option]] = None,
    inherit_options: Optional[bool] = False,
    arguments: Optional[List[Argument]] = None,
    inherit_arguments: Optional[bool] = False,
    subcommands: Optional[List[Command]] = None,
)

Parameters:

  • name

    (str) –

    The name of the command.

  • handler

    (callable) –

    The function to execute when the command is called.

  • description

    (Optional[str], default: None ) –

    The description of the command.

  • usage

    (Optional[str], default: None ) –

    The usage information for the command. Defaults to "[SUBCOMMANDS][OPTIONS][ARGUMENTS]" if not provided.

  • options

    (Optional[List[Option]], default: None ) –

    The options available for the command.

  • inherit_options

    (Optional[bool], default: False ) –

    Whether to inherit options from parent commands.

  • arguments

    (Optional[Argument], default: None ) –

    The arguments available for the command.

  • inherit_arguments

    (Optional[bool], default: False ) –

    Whether to inherit arguments from parent commands.

  • subcommands

    (Optional[List[Command]], default: None ) –

    The subcommands available for the command.

Methods:

Attributes:

Source code in saiuncli/command.py
def __init__(
    self,
    name: str,
    handler: callable,
    description: Optional[str] = None,
    usage: Optional[str] = None,
    options: Optional[List[Option]] = None,
    inherit_options: Optional[bool] = False,
    arguments: Optional[List[Argument]] = None,
    inherit_arguments: Optional[bool] = False,
    subcommands: Optional[List["Command"]] = None,
):
    """
    Initialize a Command object.

    Args:
        name (str):
            The name of the command.
        handler (callable):
            The function to execute when the command is called.
        description (Optional[str]):
            The description of the command.
        usage (Optional[str]):
            The usage information for the command.
            Defaults to "[SUBCOMMANDS][OPTIONS][ARGUMENTS]" if not provided.
        options (Optional[List[Option]]):
            The options available for the command.
        inherit_options (Optional[bool]):
            Whether to inherit options from parent commands.
        arguments (Optional[Argument]):
            The arguments available for the command.
        inherit_arguments (Optional[bool]):
            Whether to inherit arguments from parent commands.
        subcommands (Optional[List[Command]]):
            The subcommands available for the command.
    """
    self.name = name
    self.handler = handler
    self.description = description
    self.usage = usage or _DEFAULT_USAGE
    self.options = options or []
    self.inherit_options = inherit_options
    self.arguments = arguments or []
    self.inherit_arguments = inherit_arguments
    self.subcommands = subcommands or []

    for subcommand in self.subcommands:
        subcommand._parent = self
        subcommand._version_flags = self._version_flags
        subcommand._help_flags = self._help_flags

    self._validate_options(self.all_options)

all_argument_names property

all_argument_names: List[str]

Gather all argument names available to the command.

all_arguments property

all_arguments: List[Argument]

Gather all arguments available to the command.

all_option_flags property

all_option_flags: List[str]

Gather all option flags available to the command.

all_option_long_names property

all_option_long_names: List[str]

Gather all long option names available to the command.

all_option_names property

all_option_names: List[str]

Gather all option names available to the command.

all_option_short_names property

all_option_short_names: List[str]

Gather all short option names available to the command.

all_options property

all_options: List[Option]

Gather all options available to the command.

all_subcommand_names property

all_subcommand_names: List[str]

Gather all subcommand names available to the command.

arguments instance-attribute

arguments = arguments or []

description instance-attribute

description = description

handler instance-attribute

handler = handler

inherit_arguments instance-attribute

inherit_arguments = inherit_arguments

inherit_options instance-attribute

inherit_options = inherit_options

inherited_arguments property

inherited_arguments: List[Argument]

Gather arguments inherited from parent commands if inheritance is enabled.

inherited_options property

inherited_options: List[Option]

Gather options inherited from parent commands if inheritance is enabled.

name instance-attribute

name = name

options instance-attribute

options = options or []

subcommands instance-attribute

subcommands = subcommands or []

usage instance-attribute

usage = usage or _DEFAULT_USAGE

add_argument

add_argument(argument: Argument)

Add an argument to the command.

Source code in saiuncli/command.py
def add_argument(self, argument: Argument):
    """Add an argument to the command."""
    self.arguments.append(argument)
    self._validate_arguments(self.all_arguments)

add_arguments

add_arguments(arguments: List[Argument])

Add multiple arguments to the command.

Source code in saiuncli/command.py
def add_arguments(self, arguments: List[Argument]):
    """Add multiple arguments to the command."""
    self.arguments.extend(arguments)
    self._validate_arguments(self.all_arguments)

add_option

add_option(option: Option)

Add an option to the command.

Source code in saiuncli/command.py
def add_option(self, option: Option):
    """Add an option to the command."""
    self.options.append(option)
    self._validate_options(self.all_options)

add_options

add_options(options: List[Option])

Add multiple options to the command.

Source code in saiuncli/command.py
def add_options(self, options: List[Option]):
    """Add multiple options to the command."""
    self.options.extend(options)
    self._validate_options(self.all_options)

add_subcommand

add_subcommand(subcommand: Command)

Add a subcommand to the command.

Source code in saiuncli/command.py
def add_subcommand(self, subcommand: "Command"):
    """Add a subcommand to the command."""
    subcommand._parent = self
    subcommand._version_flags = self._version_flags
    subcommand._help_flags = self._help_flags
    self.subcommands.append(subcommand)

add_subcommands

add_subcommands(subcommands: List[Command])

Add multiple subcommands to the command.

Source code in saiuncli/command.py
def add_subcommands(self, subcommands: List["Command"]):
    """Add multiple subcommands to the command."""
    for subcommand in subcommands:
        subcommand._parent = self
        subcommand._version_flags = self._version_flags
        subcommand._help_flags = self._help_flags
    self.subcommands.extend(subcommands)

execute

execute(**handler_args)
Source code in saiuncli/command.py
def execute(self, **handler_args):
    self.handler(**handler_args)

find_subcommand

find_subcommand(name: str) -> Optional[Command]

Find a subcommand by name.

Source code in saiuncli/command.py
def find_subcommand(self, name: str) -> Optional["Command"]:
    """Find a subcommand by name."""
    for subcommand in self.subcommands:
        if subcommand.name == name:
            return subcommand
    return None

flag_to_option

flag_to_option(flag: str) -> Optional[Option]

Get an option by flag.

Source code in saiuncli/command.py
def flag_to_option(self, flag: str) -> Optional[Option]:
    """Get an option by flag."""
    for option in self.all_options:
        if flag in option.flags:
            return option
    return None