CLI module¶
The CLI module defines the boldi
CLI and the classes necessary
for other modules to define boldi
CLI subcommands via an internal plugin system.
Install¶
Boldi's CLI module is distributed as the
boldi-cli
Python package,
thus to install it, run:
...or add "boldi-cli"
as a dependency to your project.
Run¶
The Boldi CLI can be invoked using the boldi
command:
Subcommands¶
boldi
¶
Same as boldi help
.
boldi help
¶
Shows the help message for a subcommand.
boldi dev
¶
Subcommand used for developing Boldi's Python libraries.
Available if the boldi-dev
Python package is installed.
See: Development module.
Plugins¶
Subcommands for boldi
(such as boldi help
or boldi dev
) can be defined using plugins.
The plugin must be declared as a Python package entry point, with the following properties:
- The entry point group must be
boldi.cli.action
. - The entry point name will be used as the name of the subcommand.
- The entry point must refer to function that takes the following arguments:
- The class's
__init__
method must implement the subcommand argument parser. - The class must call
subparser.set_defaults(action=<some-function>)
to perform an action.
- The class's
API¶
boldi.cli
¶
CliCtx
¶
Bases: Ctx
Extends Ctx
with a console feature for rich CLI output.
Source code in pkg/boldi-cli/boldi/cli.py
console: Console = field(default_factory=_CliCtxDefaultConsole)
¶
A rich console that outputs to self.stderr
by default.
cwd: Path = field(default_factory=Path.cwd)
¶
Replacement for pathlib.Path.cwd
.
env: MutableMapping[str, str] = field(default_factory=lambda: os.environ)
¶
Replacement for os.environ
.
msg_FAIL = partialmethod(msg, style='FAIL')
¶
msg_PASS = partialmethod(msg, style='PASS')
¶
msg_WARN = partialmethod(msg, style='WARN')
¶
msg_fail = partialmethod(msg, style='fail')
¶
msg_info = partialmethod(msg, style='info')
¶
msg_pass = partialmethod(msg, style='pass')
¶
msg_warn = partialmethod(msg, style='warn')
¶
parser: ArgumentParser = field(default_factory=ArgumentParser)
¶
The root argparse.ArgumentParser
for the boldi
CLI.
stderr: TextIO = field(default_factory=lambda: sys.stderr)
¶
Replacement for sys.stderr
.
stdout: TextIO = field(default_factory=lambda: sys.stdout)
¶
Replacement for sys.stdout
.
verbose: bool = field(default=False)
¶
Whether to enable verbose output.
__enter__() -> Self
¶
__exit__(*exc_info) -> bool | None
¶
__init__(stack: ExitStack = ExitStack(), stdin: TextIO = lambda: sys.stdin(), stdout: TextIO = lambda: sys.stdout(), stderr: TextIO = lambda: sys.stderr(), argv: list[str] = lambda: sys.argv(), env: MutableMapping[str, str] = lambda: os.environ(), cwd: Path = Path.cwd(), console: Console = _CliCtxDefaultConsole(), parser: ArgumentParser = ArgumentParser(), verbose: bool = False) -> None
¶
__post_init__()
¶
chdir(path: Path)
¶
Change the current working directory to path
and restore later via self.stack
.
msg(*args, **kwargs)
¶
run(*args: Union[str, List[Any]], **kwargs: Unpack[RunArgs]) -> subprocess.CompletedProcess
¶
Run a subprocess using the provided command line arguments and updated defaults.
Parameters:
-
args
(Union[str, List[Any]]
, default:()
) –Command line arguments, provided as positional arguments. As defined in
boldi.proc.args_iter
. -
kwargs
(Unpack[RunArgs]
, default:{}
) –Arguments to
subprocess.run
. Defaults tocheck=True
,text=True
, and values set inself.{stdin,stdout,stderr,env,cwd}
, unless otherwise set by the caller.
Returns:
-
CompletedProcess
–Completed process object.
Source code in pkg/boldi-ctx/boldi/ctx.py
run_py(*args: Union[str, List[Any]], **kwargs: Unpack[RunArgs]) -> subprocess.CompletedProcess
¶
Run a subprocess using the current Python interpreter, the provided command line arguments and updated defaults.
Parameters:
-
args
(Union[str, List[Any]]
, default:()
) –Command line arguments, provided as positional arguments. As defined in
boldi.proc.args_iter
. -
kwargs
(Unpack[RunArgs]
, default:{}
) –Arguments to
subprocess.run
. As defined inrun
.
Returns:
-
CompletedProcess
–Completed process object.
Source code in pkg/boldi-ctx/boldi/ctx.py
CliUsageException
¶
error_handler(ctx: CliCtx)
¶
Context manager that catches all exceptions and converts them to console messages.
Source code in pkg/boldi-cli/boldi/cli.py
esc(obj: object) -> str
¶
main(ctx: CliCtx | None = None)
¶
Main entry point that implements the boldi
CLI.