mlsimkit.common.cli.options¶
- mlsimkit.common.cli.options(*param_decls: str, cls: type[Option] | None = None, **attrs: Any) Callable[[Callable], Callable] ¶
Decorator to define Click command options using Pydantic schemas.
This decorator can be used in three ways:
1. Click.Option:
Add a standard click.options decorator (see Click documentation):
@click.command() @options('--level', type=int) @options('--mode', type=bool) def cmd(level, mode): ...
Click options are added as a conventional command-line interface:
$ python cmd.py --help Usage: cmd.py [OPTIONS] Options: --level INTEGER --mode BOOLEAN --help Show this message and exit.
2. Pydantic Shorthand:
Add an option that accepts a shorthand string converted to a Pydantic model. See
options_from_schema_shorthand()
for complete usage:from pydantic import BaseModel class MySettings(BaseModel): level: int mode: bool @click.command() @options('--settings', type=MySettings, shorthand=True) def cmd(settings: MySettings): print(settings)
The shorthand format is a comma-separate key-value string:
$ python cmd.py --settings "level=1,mode=True" level=1 mode=True
3. Pydantic Detailed:
Automatically generate options for ALL fields in a Pydantic model. See
options_from_schema()
for complete usage:from pydantic import BaseModel class MySettings(BaseModel): level: int mode: bool @click.command(cls=BaseCommand) @options(type=MySettings, dest='settings') def cmd(settings: MySettings): ...
The schema fields become options:
$ python cmd.py --help Usage: cmd.py [OPTIONS] Options: --level INTEGER [required] --mode / --no-mode [required] --help Show this message and exit.
- Parameters:
*param_decls (str) – The parameter names (e.g, ‘-o’, ‘–option’, ‘option’) for
options_from_schema_shorthand()
or click.options. Alternatively, for convenience, also accepts a Pydantic model type for adding detailed options viaoptions_from_schema()
.**attrs – Additional keyword arguments passed to click.options, or
options_from_schema_shorthand()
oroptions_from_schema()
. See their documentation for the available arguments.cls (type[click.Option], optional) – The Click option class to use. Defaults to click.Option.
- Returns:
- A decorator function that takes a command function and returns a
decorated version with the specified options.
- Return type:
Callable