xnano.cli.command
xnano.cli.command
¶
xnano.cli.command
Define typed commands and subcommands from ordinary Python methods.
Classes:
-
Command–A command or subcommand group.
-
HelpException–Stop command parsing after help has been requested.
Command
dataclass
¶
Command(
name: str | None = None,
description: str | None = None,
strict: bool = False,
show_help: bool = True,
help: bool = True,
)
A command or subcommand group.
Attributes:
-
name(str | None) –The name of the command.
-
description(str | None) –A description of the command.
-
strict(bool) –Whether to validate parameter types against annotations.
-
show_help(bool) –Whether
--help/-hare recognized. -
help(bool) –Compatibility alias for
show_help. -
parameters(list[_Parameter]) –Resolved command parameters.
-
subcommands(dict[str, 'Command']) –Registered subcommands.
Example
command = Command(name="hello") @command ... def greet(name: str) -> str: ... return f"Hello, {name}" command.run(["Ada"]) 'Hello, Ada'
Methods:
-
option–Attach option names and help text to a command parameter.
-
command–Decorator to register a subcommand.
-
register_callback–Register the main callback for this command.
-
add_subcommand–Add a subcommand programmatically.
-
parse_arguments–Parse arguments without exiting the process.
-
run–Parse arguments and run the command, exiting on errors/help.
-
get_help–Return plain help text for this command.
-
__call__–Register a callback or run with
sys.argv.
description
class-attribute
instance-attribute
¶
description: str | None = None
Command summary shown in help.
strict
class-attribute
instance-attribute
¶
strict: bool = False
Whether annotations are validated strictly.
show_help
class-attribute
instance-attribute
¶
show_help: bool = True
Whether -h and --help are enabled.
UNSET
class-attribute
instance-attribute
¶
UNSET: Any = dataclasses.field(
default=UNSET, init=False, repr=False
)
Sentinel used for parameters without defaults.
option
staticmethod
¶
option(
name_or_flags: str | list[str],
*,
default: Any = None,
help: str | None = None,
is_flag: bool | None = None
) -> Callable[[Callable[..., Any]], Callable[..., Any]]
Attach option names and help text to a command parameter.
Source code in xnano/cli/command.py
command
¶
command(
name: str | None = None,
*,
description: str | None = None
) -> Callable[[Callable[..., Any]], Callable[..., Any]]
Decorator to register a subcommand.
Source code in xnano/cli/command.py
register_callback
¶
Register the main callback for this command.
add_subcommand
¶
Add a subcommand programmatically.
Source code in xnano/cli/command.py
parse_arguments
¶
Parse arguments without exiting the process.
Raises:
-
HelpRequested–When help was requested.
-
CliError–On usage / validation failures.
Source code in xnano/cli/command.py
run
¶
Parse arguments and run the command, exiting on errors/help.
Source code in xnano/cli/command.py
__call__
¶
Register a callback or run with sys.argv.