CLI
provide.foundation.cli
¶
TODO: Add module docstring.
Classes¶
CLIAdapter
¶
Bases: Protocol
Protocol for CLI framework adapters.
Adapters convert framework-agnostic CommandInfo objects into framework-specific CLI commands and groups. This allows the hub to work with any CLI framework without tight coupling.
Currently provides ClickAdapter. Custom adapters for other frameworks (Typer, argparse) can be implemented by following the CLIAdapter protocol.
See: docs/guide/advanced/integration-patterns.md#custom-cli-adapters
Examples:
>>> from provide.foundation.cli import get_cli_adapter
>>> adapter = get_cli_adapter('click')
>>> command = adapter.build_command(command_info)
Functions¶
build_command
¶
Build framework-specific command from CommandInfo.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
info
|
CommandInfo
|
Framework-agnostic command information |
required |
Returns:
| Type | Description |
|---|---|
Any
|
Framework-specific command object (e.g., click.Command) |
Raises:
| Type | Description |
|---|---|
CLIBuildError
|
If command building fails |
Source code in provide/foundation/cli/base.py
build_group
¶
build_group(
name: str,
commands: list[CommandInfo] | None = None,
registry: Registry | None = None,
**kwargs: Any
) -> Any
Build framework-specific command group.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
Group name |
required |
commands
|
list[CommandInfo] | None
|
List of command names to include (None = all from registry) |
None
|
registry
|
Registry | None
|
Command registry (defaults to global) |
None
|
**kwargs
|
Any
|
Framework-specific options |
{}
|
Returns:
| Type | Description |
|---|---|
Any
|
Framework-specific group object (e.g., click.Group) |
Raises:
| Type | Description |
|---|---|
CLIBuildError
|
If group building fails |
Source code in provide/foundation/cli/base.py
ensure_parent_groups
¶
Ensure all parent groups in path exist.
Creates missing parent groups in the registry. For example, if parent_path is "db.migrate", ensures both "db" and "db.migrate" groups exist.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
parent_path
|
str
|
Dot-notation path to parent (e.g., "db.migrate") |
required |
registry
|
Registry
|
Command registry to update |
required |
Source code in provide/foundation/cli/base.py
CLIAdapterNotFoundError
¶
Bases: CLIError
Raised when CLI adapter dependencies are missing.
This error occurs when attempting to use a CLI framework adapter but the required framework package is not installed.
Examples:
Initialize with framework details.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
framework
|
str
|
Name of the CLI framework (e.g., 'click') |
required |
package
|
str | None
|
Optional package name to install |
None
|
Source code in provide/foundation/cli/errors.py
Functions¶
CLIBuildError
¶
CLIBuildError(
message: str,
*,
code: str | None = None,
context: dict[str, Any] | None = None,
cause: Exception | None = None,
**extra_context: Any
)
Bases: CLIError
Raised when CLI command/group building fails.
This error occurs during the conversion of framework-agnostic CommandInfo to framework-specific CLI objects.
Source code in provide/foundation/errors/base.py
CLIError
¶
CLIError(
message: str,
*,
code: str | None = None,
context: dict[str, Any] | None = None,
cause: Exception | None = None,
**extra_context: Any
)
Bases: FoundationError
Base error for CLI adapter operations.
Raised when CLI adapter operations fail.
Source code in provide/foundation/errors/base.py
CliTestRunner
¶
Test runner for CLI commands using Click's testing facilities.
Source code in provide/foundation/cli/utils.py
Functions¶
invoke
¶
invoke(
cli: Command | Group,
args: list[str] | None = None,
input: str | None = None,
env: dict[str, str] | None = None,
catch_exceptions: bool = True,
**kwargs: Any
) -> Result
Invoke a CLI command for testing.
Source code in provide/foundation/cli/utils.py
InvalidCLIHintError
¶
Bases: CLIError
Raised when an invalid CLI hint is provided in Annotated.
This error occurs when a parameter uses typing.Annotated with an invalid CLI rendering hint. Valid hints are 'option' and 'argument'.
Examples:
Initialize with hint and parameter details.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
hint
|
str
|
The invalid hint that was provided |
required |
param_name
|
str
|
Name of the parameter with invalid hint |
required |
Source code in provide/foundation/cli/errors.py
Functions¶
Functions¶
assert_cli_error
¶
assert_cli_error(
result: Result,
expected_error: str | None = None,
exit_code: int | None = None,
) -> None
Assert that a CLI command failed.
Source code in provide/foundation/cli/utils.py
assert_cli_success
¶
Assert that a CLI command succeeded.
Source code in provide/foundation/cli/utils.py
config_options
¶
Add configuration file options to a Click command.
Adds: - --config/-c: Path to configuration file - --profile/-p: Configuration profile to use
Source code in provide/foundation/cli/decorators.py
create_cli_context
¶
Create a CLIContext for CLI usage.
Loads from environment, then overlays any provided kwargs.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
**kwargs
|
Any
|
Override values for the context |
{}
|
Returns:
| Type | Description |
|---|---|
CLIContext
|
Configured CLIContext instance |
Source code in provide/foundation/cli/utils.py
echo_error
¶
echo_info
¶
echo_json
¶
echo_success
¶
echo_warning
¶
error_handler
¶
Decorator to handle errors consistently in CLI commands.
Catches exceptions and formats them appropriately based on debug mode and output format.
Source code in provide/foundation/cli/decorators.py
flexible_options
¶
Apply flexible CLI options that can be used at any command level.
Combines logging_options and config_options for consistent control at both group and command levels.
Source code in provide/foundation/cli/decorators.py
get_cli_adapter
¶
Get CLI adapter for specified framework.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
framework
|
str
|
CLI framework name ('click', 'typer', etc.) |
'click'
|
Returns:
| Type | Description |
|---|---|
CLIAdapter
|
CLIAdapter instance for the framework |
Raises:
| Type | Description |
|---|---|
CLIAdapterNotFoundError
|
If framework adapter is not available |
ValueError
|
If framework name is unknown |
Examples:
Source code in provide/foundation/cli/__init__.py
logging_options
¶
Add standard logging options to a Click command.
Adds: - --log-level/-l: Set logging verbosity (TRACE, DEBUG, INFO, WARNING, ERROR, CRITICAL) - --log-file: Write logs to file - --log-format: Choose log output format (json, text, key_value)
Source code in provide/foundation/cli/decorators.py
output_options
¶
Add output formatting options to a Click command.
Adds: - --json: Output in JSON format - --no-color: Disable colored output - --no-emoji: Disable emoji in output
Source code in provide/foundation/cli/decorators.py
pass_context
¶
Decorator to pass the foundation CLIContext to a command.
Creates or retrieves a CLIContext from Click's context object and passes it as the first argument to the decorated function.
Source code in provide/foundation/cli/decorators.py
setup_cli_logging
¶
Setup logging for CLI applications using a CLIContext object.
This function is the designated way to configure logging within a CLI application built with foundation. It uses the provided context object to construct a full TelemetryConfig and initializes the system.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
ctx
|
CLIContext
|
The foundation CLIContext, populated by CLI decorators. |
required |
reinit_logging
|
bool
|
Whether to force re-initialization of logging (default: True). Set to False when embedding Foundation in a host application to avoid clobbering the host's logging configuration. |
True
|
Source code in provide/foundation/cli/utils.py
standard_options
¶
Apply all standard CLI options.
Combines logging_options, config_options, and output_options.
version_option
¶
Add a --version option to display version information.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
version
|
str | None
|
Version string to display |
None
|
prog_name
|
str | None
|
Program name to display |
None
|