mlsimkit.common.cli.ShorthandParamType

class mlsimkit.common.cli.ShorthandParamType(model, delimiter=',', quotechar='|')

Click Parameter type for a Pydantic model that accepts shorthand notation.

This parameter type allows users to specify values for a Pydantic model using a shorthand notation in the command line. It also supports loading values directly from a configuration file.

Usage example:

from pydantic import BaseModel
from typing import List

class MyOption(BaseModel):
    name: str
    columns: List[str]

@click.command()
@click.option(
    "-o", "--option",
    type=ShorthandParamType(MyOption),
    help="Specify an option in shorthand notation: 'name=value,columns=col1 col2'"
    callback=lambda ctx, param, value: value)
def my_command(option: MyOption):
    print(option)

if __name__ == '__main__':
    my_command()

From the command line:

$ python script.py -o "name=my_option,columns=col1 col2"
name='my_option' columns=['col1', 'col2']

Alternatively, values can be loaded from a configuration file when using program():

# config.yaml
option:
  name: my_option
  columns:
    - col1
    - col2
$ python script.py --config config.yaml
Parameters:

model (Type[BaseModel]) – The Pydantic model class to create an instance from the shorthand string or configuration file.

__init__(model, delimiter=',', quotechar='|')

Methods

__init__(model[, delimiter, quotechar])

convert(value, param, ctx)

Convert to an instance of the Pydantic model.

fail(message[, param, ctx])

Helper method to fail with an invalid value message.

get_metavar(param, ctx)

Returns the metavar default for this param if it provides one.

get_missing_message(param, ctx)

Optionally might return extra information about a missing parameter.

shell_complete(ctx, param, incomplete)

Return a list of CompletionItem objects for the incomplete value.

split_envvar_value(rv)

Given a value from an environment variable this splits it up into small chunks depending on the defined envvar list splitter.

to_info_dict()

Gather information that could be useful for a tool generating user-facing documentation.

Attributes

arity

envvar_list_splitter

if a list of this type is expected and the value is pulled from a string environment variable, this is what splits it up.

is_composite

name

the descriptive name of this type

__init__(model, delimiter=',', quotechar='|')
convert(value, param, ctx)

Convert to an instance of the Pydantic model.

This method handles converting different types of values to an instance of the Pydantic model associated with this parameter type. It supports three main cases:

  1. If the value is a list, it assumes that the parameter is marked as multiple=True. In this case, it recursively converts each element of the list to the Pydantic model and returns a list of model instances to match Click built-in behavior. If the parameter is not marked as multiple=True, it raises a BadParameter exception.

  2. If the value is a dictionary, it assumes that the value is loaded directly from a configuration file. In this case, it creates an instance of the Pydantic model by passing the dictionary as keyword arguments. This enables using config-only even for ShorthandParamTypes options.

  3. If the value is a string, it assumes that the string represents a shorthand notation for the Pydantic model. It calls the _parse_param_shorthand_str function to parse the string and create an instance of the Pydantic model.

If the value is not a list, dictionary, or string, it raises a BadParameter exception.

Parameters:
  • value (Any) – The value to be converted to a Pydantic model instance.

  • param (click.Parameter) – The Click parameter object associated with the value.

  • ctx (click.Context) – The Click context object associated with the value.

Returns:

An instance of the Pydantic model associated with this parameter type.

Return type:

BaseModel

Raises:

click.BadParameter – If the value is a list and the parameter is not marked as multiple=True, or if the value is not a list, dictionary, or string.