mlsimkit.common.cli.options_from_schema¶
- mlsimkit.common.cli.options_from_schema(model: Type[BaseModel], dest: str, prefix: str | None = None, help_group: str | None = None, yaml_file: bool | Dict = False) Callable[[Callable], Callable] ¶
Decorator to generate Click options for all fields in a Pydantic model.
This decorator generates Click options based on the fields of a given Pydantic model. The generated options can be used to parse command-line arguments and instantiate the model with the parsed values.
Example:
from pydantic import BaseModel class MySettings(BaseModel): level: int mode: bool @click.command(cls=BaseCommand) @options_from_schema(MySettings, dest='settings') def cmd(settings: MySettings): ...
- Parameters:
model (Type[BaseModel]) – The Pydantic model class to generate options for.
dest (str) – The attribute name to store the instantiated model object on the command context.
prefix (str, optional) – An optional prefix to add to each option name. Use this if duplicately named fields exist across different models used in the same command.
help_group (str, optional) – The help group text to display in the CLI.
yaml_file (Union[bool, Dict], optional) – Whether to add a hidden option for overriding the nested field from a YAML file. If set to True, a hidden option will be added with the default name. If set to a dictionary, it should contain the keys name (for the option name) and hidden (boolean indicating whether the option should be hidden).
- Returns:
- A decorator function that takes a command function and returns a
decorated version with the specified options.
- Return type:
Callable
Note
If multiple Pydantic models are used in the same command, any duplicate option names across models will be detected. Use the prefix argument to differentiate such options.