xnano.cli
xnano.cli
¶
xnano.cli
Build typed commands, options, subcommands, and help screens.
Modules:
-
command–xnano.cli.command
-
errors–xnano.cli.errors
-
help–xnano.cli.help
-
parameters–xnano.cli.parameters
Classes:
-
Command–A command or subcommand group.
-
HelpException–Stop command parsing after help has been requested.
-
CliError–User-facing CLI failure.
-
Argument–Positional argument metadata.
-
Option–Option metadata attached via default or
Annotated.
Functions:
-
render_help–Render help for
command, optionally via terminal components.
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.
CliError
dataclass
¶
Argument
¶
Argument(
*,
help: str | None = None,
metavar: str | None = None,
choices: Sequence[Any] | None = None
)
Positional argument metadata.
Attributes:
-
help–Help text for this argument.
-
metavar–Optional metavar override.
-
choices–Optional allowed values.
Example
argument = Argument(help="Input file", metavar="PATH") argument.metavar 'PATH'
Source code in xnano/cli/parameters.py
Option
¶
Option(
*flags: str,
help: str | None = None,
metavar: str | None = None,
choices: Sequence[Any] | None = None,
hidden: bool = False
)
Option metadata attached via default or Annotated.
Attributes:
-
flags–Short/long option flags (e.g.
"-f","--force"). -
help–Help text for this option.
-
metavar–Optional metavar override.
-
choices–Optional allowed values.
-
hidden–When
True, omit from help.
Example
option = Option("-f", "--force", help="Overwrite output") option.flags ('-f', '--force')
Source code in xnano/cli/parameters.py
render_help
¶
Render help for command, optionally via terminal components.
Parameters:
-
command('Command') –Command to document.
-
stream(TextIO | None, default:None) –Unused except for TTY detection.
Returns:
-
str–Help text. Styled rendering falls back to plain text when the
-
str–terminal path is unavailable.