Cli¶
cli
¶
Public CLI API for external command implementations.
CliCommand(**kwargs)
¶
Bases: ABC
Abstract base class for CLI commands.
All CLI commands should inherit from this class and implement the run
method. This design allows for easy composition of commands, particularly
for batch operations.
Bind CLI kwargs to this command instance.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
**kwargs
|
Any
|
Keyword arguments corresponding to the options declared
by this command's |
{}
|
Source code in src/flepimop2/cli/_cli_command.py
config
property
¶
Get the primary configuration path for this command, if any.
Commands that bind the shared config argument use this default
implementation. Commands that use multiple configuration files or no
configuration file should return None or override as needed.
target
property
¶
Get the resolved logical target for this command, if any.
Commands that do not operate on a named configuration target should use this default implementation. Target-aware commands should override this property and return the resolved configuration entry name.
__call__()
¶
Execute this command using the kwargs bound at construction time.
Consumes 'verbosity' for logger setup. If 'verbosity' was
auto-appended (not in _literal_options), it is removed from the
kwargs passed to run(). The return value from run() is forwarded
to sys.exit().
Source code in src/flepimop2/cli/_cli_command.py
__str__()
¶
Render this command as a CLI invocation string.
Returns:
| Type | Description |
|---|---|
str
|
The command rendered as a CLI invocation. |
command_name()
classmethod
¶
Get the command name for CLI registration.
By default, converts the class name from CamelCase to kebab-case. For example, SimulateCommand -> simulate. Commands can override this method to provide a custom command name.
Returns:
| Type | Description |
|---|---|
str
|
The command name to use in the CLI. |
Source code in src/flepimop2/cli/_cli_command.py
critical(*args, **kwargs)
¶
debug(*args, **kwargs)
¶
error(*args, **kwargs)
¶
format(value)
staticmethod
¶
Format a value for logging output.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
value
|
object
|
The value to format. |
required |
Returns:
| Type | Description |
|---|---|
str
|
A string representation of the value. |
Examples:
>>> from pathlib import Path
>>> from flepimop2.cli import CliCommand
>>> CliCommand.format("abc")
'abc'
>>> CliCommand.format(Path("/some/path"))
'/some/path'
>>> CliCommand.format(Path("relative/path"))
'/.../relative/path'
>>> CliCommand.format(1000000)
'1,000,000'
>>> CliCommand.format(1234.5678)
'1,234.5678'
Source code in src/flepimop2/cli/_cli_command.py
help_text()
classmethod
¶
Get the help text for this command.
By default, extracts the help text from the class docstring. The first line becomes the short help (shown in command list), and the full docstring becomes the long help (shown in command --help).
Commands can override this method to provide custom help text.
Returns:
| Type | Description |
|---|---|
str
|
The help text for the command. |
Source code in src/flepimop2/cli/_cli_command.py
info(*args, **kwargs)
¶
log(level, *args, **kwargs)
¶
options()
classmethod
¶
Get the list of common CLI options/arguments this command uses.
By default, this method uses _literal_options() to get the keyword-only
parameters from the run method. If 'verbosity' is not present and
auto_append_verbosity is True, it will be automatically appended.
Commands can override this method to specify custom options that differ
from the run method's parameters, or when more complex logic is needed.
The options will be applied in the order specified, with arguments typically appearing before options in the list for proper Click behavior.
Returns:
| Type | Description |
|---|---|
list[str]
|
List of option names to request from |
Examples:
>>> from flepimop2.typing import ExitCode
>>> class MyCommand(CliCommand):
... def run(self, *, config: Path, dry_run: bool) -> ExitCode:
... return ExitCode.OKAY
>>> MyCommand.options()
['config', 'dry_run', 'verbosity']
>>> class MyCommandWithVerbosity(CliCommand):
... def run(
... self, *, config: Path, verbosity: int, dry_run: bool
... ) -> ExitCode:
... return ExitCode.OKAY
>>> MyCommandWithVerbosity.options()
['config', 'verbosity', 'dry_run']
Source code in src/flepimop2/cli/_cli_command.py
run(**kwargs)
abstractmethod
¶
Execute the command.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
**kwargs
|
Any
|
Command-specific arguments passed from Click options/arguments. |
{}
|
to_argv()
¶
Render this instance's bound kwargs back into argv tokens.
Walks cls.options() in declaration order and converts each bound
value to its CLI representation:
click.Argument: bare positional string (skipped ifNone).click.Optionwithis_flag=True: long flag name when truthy, omitted when falsy.click.Optionwithcount=True: repeated short flag (e.g.-vvvforverbosity=3).click.Optionotherwise:--name valuepair.
Path values are rendered as their absolute string form so the
resulting argv replays correctly on a remote with a shared filesystem.
Returns:
| Type | Description |
|---|---|
list[str]
|
A list of string tokens suitable for passing to |