cli
CliCommands
dataclass
¶
Container to store the commands and subcommands for the cli.
Source code in m/cli/engine/types.py
CommandModule
dataclass
¶
FuncArgs
dataclass
¶
MetaModule
dataclass
¶
Container to store a metadata dictionary from a "meta" module.
Source code in m/cli/engine/types.py
Arg(default=PydanticUndefined, *, help, positional=None, required=None, aliases=None, nargs=None, validator=None)
¶
Create a pydantic Field
.
Field docs: https://docs.pydantic.dev/2.2/usage/fields
Defines properties used to create an argparse argument. This function
should work for most cases. If we need something that is not covered
we can use ArgProxy
instead which is untyped but provides all the arguments
and keyword arguments to argparse.
See https://docs.python.org/3/library/argparse.html.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
default |
Any
|
Default value if the field is not set. |
PydanticUndefined
|
help |
str
|
Human-readable description. |
required |
positional |
bool | None
|
Whether the argument is positional or not. |
None
|
required |
bool | None
|
Indicate whether an argument is required or optional. |
None
|
aliases |
list[str] | None
|
Alternative names for the argument. |
None
|
nargs |
int | Literal['?', '*', '+'] | None
|
Number of times the argument can be used. |
None
|
validator |
Callable[[str], str] | None
|
Function to validate the argument. |
None
|
Returns:
Type | Description |
---|---|
Any
|
A new |
Source code in m/cli/args.py
ArgProxy(*args, **kwargs)
¶
Wrap function to provide all argparse inputs.
This is a escape hatch and does not provide any typings. See https://docs.python.org/3/library/argparse.html.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
args |
Any
|
The arguments to argparse add_arguments. |
()
|
kwargs |
Any
|
The keyword arguments to argparse add arguments. |
{}
|
Returns:
Type | Description |
---|---|
Any
|
A new |
Source code in m/cli/args.py
Meta(*, help, description)
¶
Create the meta dictionary for a subcommand description.
In the case of the root command the help
may be set to empty since it
is not used.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
help |
str
|
The help message for the subcommand. |
required |
description |
str
|
The description for the command/subcommand. |
required |
Returns:
Type | Description |
---|---|
dict[str, str]
|
A dictionary with the help and description. |
Source code in m/cli/args.py
RemainderArgs(*, help=_Unset)
¶
Provide a list of unrecognized arguments.
This is a escape hatch and does not provide any typings. May be useful for commands that need to pass arguments to other commands.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
help |
str | None
|
Human-readable description. |
_Unset
|
Returns:
Type | Description |
---|---|
Any
|
A new |
Any
|
|
Source code in m/cli/args.py
add_arg(*args, **kwargs)
¶
Wrap FuncArgs arguments in a function.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
args |
Any
|
The arguments to argparse add_arguments. |
()
|
kwargs |
Any
|
The keyword arguments to argparse add arguments. |
{}
|
Returns:
Type | Description |
---|---|
FuncArgs
|
A FuncArgs instance. |
Source code in m/cli/engine/types.py
cli_commands(root_meta=None, **commands)
¶
Create a CliCommands instance.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
root_meta |
MetaModule | None
|
The meta for the root command. |
None
|
commands |
DecoratedRunFunction | CliSubcommands
|
The commands and subcommands for the cli. |
{}
|
Returns:
Type | Description |
---|---|
CliCommands
|
An instance of CliCommands. |
Source code in m/cli/engine/sys.py
cli_integration_token(integration, env_var)
¶
Return a function that takes in a parser.
This generated function registers a token argument in the parser which looks for its value in the environment variables.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
integration |
str
|
The name of the integration. |
required |
env_var |
str
|
The environment variable name. |
required |
Returns:
Type | Description |
---|---|
cli_global_option
|
A function to add arguments to an argparse parser. |
Source code in m/cli/cli.py
command(*, help, model, name=None)
¶
Apply a decorator to the run
function to make it into a command.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
name |
str | None
|
The command name. |
None
|
help |
str
|
A short description of the command. |
required |
model |
type[BaseModel]
|
A pydantic model to describe the cli arguments. |
required |
Returns:
Type | Description |
---|---|
partial[partial[int]]
|
A transformed run function aware of the arguments model. |
Source code in m/cli/engine/argparse.py
command_group(*, help, description, add_arguments=None)
¶
Create an instance of a MetaModule.
Named cmd_group
since it is used to describe the group of commands.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
help |
str
|
quick help describing the module. |
required |
description |
str
|
Detailed description about the module. |
required |
add_arguments |
Callable[[ArgumentParser], None] | None
|
Optional function to handle the argparse instance. |
None
|
Returns:
Type | Description |
---|---|
MetaModule
|
An instance of a MetaModule. |
Source code in m/cli/engine/sys.py
create_issue_handler(use_warning)
¶
create_json_handler(pretty)
¶
create_yaml_handler(pretty)
¶
env_var(arg_value)
¶
env_var_or_empty(arg_value)
¶
Read a value from the environment.
Unlike the env_var validator, this will only
allow the arg_value
to pass through if is not in the form of an
environment variable. That is, if the value is all uppercase letters and
underscores it will attempt to read from the environment and return an empty
string if not defined.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
arg_value |
str
|
The input provided by argparse. |
required |
Returns:
Type | Description |
---|---|
str
|
The environment variable value if it exists. Otherwise, empty string. |
Source code in m/cli/validators.py
exec_cli(cli_commands)
¶
Execute the cli application.
usage::
def create_cli_commands() -> CliCommands:
# We may import CliCommand objects from other projects and create
# a new one with them.
return import_cli_commands('cli.command.module')
def main():
cli_commands = create_cli_commands()
exec_cli(cli_commands)
This is the preferred way to execute the cli application as it will allow other potential applications to use the cli commands.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
cli_commands |
CliCommands
|
The cli commands to execute. |
required |
Source code in m/cli/cli.py
import_cli_commands(commands_module)
¶
Gather the commands and subcommands for the cli.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
commands_module |
str
|
module containing all the commands. |
required |
Returns:
Type | Description |
---|---|
CliCommands
|
An instance of CliCommands gathered from the commands_module. |
Source code in m/cli/engine/sys.py
merge_cli_commands(base, overrides, **resolutions)
¶
Merge two CliCommands instances.
Resolutions may be provided to resolve merge conflicts between two subcommands.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
base |
CliCommands
|
The base CliCommands instance. |
required |
overrides |
CliCommands
|
The overrides CliCommands instance. |
required |
resolutions |
SubCmdResolution
|
A dictionary of resolutions for subcommands. |
{}
|
Returns:
Type | Description |
---|---|
CliCommands
|
A new CliCommands instance. |
Source code in m/cli/engine/sys.py
run_cli(commands_module, add_args=None)
¶
Run the cli application.
Deprecated, use exec_cli
instead.
usage::
def add_args(argp):
argp.add_argument(...)
def main():
run_cli('m.cli.commands', add_args)
We only need add_args
if we need to gain access to the
argparse.ArgumentParser
instance.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
commands_module |
str | None
|
The full name of the module containing the commands. |
required |
add_args |
Callable[[ArgumentParser], None] | None
|
Optional callback to gain access to the ArgumentParser. |
None
|
Source code in m/cli/cli.py
run_main(callback, result_handler=default_result_handler, issue_handler=default_issue_handler)
¶
Run the callback and print the returned value.
To change how the result or an issue should be display provide the optional
arguments handle_result
and handle_issue
. For instance, to display the
raw value provide the print
function.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
callback |
Callable[[], OneOf[Issue, Any]]
|
A function that returns a |
required |
result_handler |
Callable[[Any], None]
|
A function that takes in the Good result. |
default_result_handler
|
issue_handler |
Callable[[Issue], None]
|
A function that takes in the Issue. |
default_issue_handler
|
Returns:
Type | Description |
---|---|
int
|
0 if the callback is a |
Source code in m/cli/cli.py
subcommands(meta=None, **commands)
¶
Create a CliSubcommands instance.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
meta |
MetaModule | None
|
The meta for the command group. |
None
|
commands |
DecoratedRunFunction
|
The commands the group. |
{}
|
Returns:
Type | Description |
---|---|
CliSubcommands
|
An instance of CliSubcommands. |
Source code in m/cli/engine/sys.py
validate_file_exists(path)
¶
Assert that a file exists.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
path |
str
|
The path to the file. |
required |
Raises:
Type | Description |
---|---|
ArgumentTypeError
|
If the file does not exist. |
Returns:
Type | Description |
---|---|
str
|
The path if it exists. |
Source code in m/cli/validators.py
validate_json_payload(file_path)
¶
Return a dictionary from the contents of file_path.
This is a string that tell us to read from a file, stdin or just plain json data.
It can be used parse yaml
files as well. The extension should be
.yaml
or .yml
.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
file_path |
str
|
A string with the json payload. If it starts with |
required |
Raises:
Type | Description |
---|---|
ArgumentTypeError
|
If the file_path is meant to be a valid path and it does not exist. |
Returns:
Type | Description |
---|---|
Any
|
A parsed json payload |
Source code in m/cli/validators.py
validate_payload(file_path)
¶
Return the raw payload.
This allows us to read from a file or the stdin stream.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
file_path |
str
|
A string with the payload. If it starts with |
required |
Raises:
Type | Description |
---|---|
ArgumentTypeError
|
If the file_path is meant to be a valid path and it does not exist. |
Returns:
Type | Description |
---|---|
str
|
The payload found in the file. |