mlsimkit.common.cli.options_from_schema_shorthand¶
- mlsimkit.common.cli.options_from_schema_shorthand(*param_decls, cls: type[Option] | None = None, model: Type[BaseModel], delimiter=',', quotechar='|', **attrs)¶
Decorator to generate a Click option from a Pydantic model using shorthand notation.
This decorator generates a single Click option that accepts a shorthand string representation of the Pydantic model’s fields. The shorthand string should be in the format
"key1=value1, key2=value2, ..., keyN=valueN"
.Example:
from pydantic import BaseModel class MySettings(BaseModel): level: int mode: bool @click.command() @options_from_schema_shorthand('--settings', model=MySettings) def cmd(settings: MySettings): print(settings)
From the command line:
$ python script.py --settings "level=10, mode=true" level=10 mode=True
Standard
click.option
parameters are supported like short names (-o
,--option
, …) andmultiple=True
for accepting more than one shorthand strings:@click.command() @options_from_schema_shorthand( '-s', '--setting', 'settings', model=MySettings, multiple=True ) def cmd(settings: MySettings): for s in settings: print(s)
From the command line:
$ python script.py -s "level=1,mode=true" -s "level=2,mode=false" level=1 mode=True level=2 mode=False
Pydantic List fields and non-string types are supported:
class MyModel(BaseModel): names: List[str] ages: List[int] @click.command() @options_from_schema_shorthand("--option", model=MyModel) def cmd(option: MyModel): print(option)
From the command line:
$ python script.py --option "names=Alice Bob Charlie, ages=25 30 35" names=['Alice', 'Bob', 'Charlie'] ages=[25, 30, 35]
A shorthand option can be loaded configuration file just like
options_from_schema()
, which is convenient when usingprogram()
--config
functionality:# config.yaml option: names: [Alice, Bob, Charlie] ages: [25, 30, 35]
With
multiple=True
, the configuration supports lists:# config.yaml option: - names: [Alice, Bob, Charlie] ages: [25, 30, 35] - names: [Apple, Orange, Banana] ages: [1, 2, 3] - names: [Blue, Red, Green] ages: [0, 0, 0]
And the corresponding program code:
@mlsimkit.cli.program(name="MyProgram", version="0.1", use_config_file=True) def program(ctx, config_file): ctx.obj["config_file"] = config_file @program.command() @options_from_schema_shorthand("--option", model=MyModel, multiple=True) def cmd(ctx, option: MyModel): for opt in option: print(opt)
- Parameters:
*param_decls (str) – Parameter declarations for the Click option.
cls (type[click.Option], optional) – The Click option class to use. Defaults to click.Option.
model (Type[BaseModel]) – The Pydantic model class to generate the option for.
**attrs – Additional keyword arguments to pass to the underlying click.Option.
- Returns:
- A decorator function that takes a command function and returns a
decorated version with the specified options.
- Return type:
Callable
- Raises:
ValueError – If type or callback are specified in **attrs, as they are handled automatically by this decorator.