Add your description here
Project description
cli-pydantic
Turn a Pydantic model into a CLI. I dislike every other CLI library so here's another one.
- CLI defined by Pydantic
- Use multiple YAML / JSON configs with
--flagCLI overrides.
Install
pip install cli-pydantic
Usage
from pydantic import BaseModel, Field
from cli_pydantic import cli
class Data(BaseModel):
path: str = "./data"
splits: list[str] = ["train", "val"]
class Model(BaseModel):
arch: str = "resnet50"
lr: float = 1e-3
layers: list[int] = [64, 128, 256]
class Config(BaseModel):
data: Data = Data()
model: Model = Model()
epochs: int = 10
profile: bool = Field(False, description="dump chrome trace")
cfg = cli(Config, desc="Training pipeline")
# CLI, use default from Pydantic
python train.py --model.arch vit_base --model.lr 3e-4 --epochs 50
# From a config file
python train.py base.yaml
# Layer multiple configs, then override with flags
python train.py base.yaml fast.yaml --model.lr 0.05 --epochs 3
# Lists and booleans
python train.py --model.layers 16,32 --profile
Semantics
- Use
--foo baror--foo=bar - For lists:
--nums=1,2,3or--nums 1 --nums=2 --nums 3 - For bools:
--enable/--no-enable - Lists will replace previous configs on override -- not append!
Automatic help
$ python train.py --help
help: Training pipeline
usage: train.py [-h] [configs ...] [--overrides ...]
config arguments:
--data.path str (default: ./data)
--data.splits list[str] (default: ['train', 'val'])
--model.arch str (default: resnet50)
--model.lr float (default: 0.001)
--model.layers list[int] (default: [64, 128, 256])
--epochs int (default: 10)
--verbose bool dump chrome trace (default: False)
Errors
Validation
cli(Config, argv=['--epochs', 'not_a_number', '--model.lr', 'also_bad'])
Validation failed:
model.lr: Input should be a valid number, unable to parse string as a number
epochs: Input should be a valid integer, unable to parse string as an integer
Config
cli(Config, argv=['--nonexistent', 'val'])
Unknown option: --nonexistent
Docstring
cli[T: BaseModel](model_cls: type[T], desc: str = '', argv: list[str] | None = None, raise_on_error: bool = False) -> T
Build a CLI from a Pydantic model, merging config files and --overrides.
Positional arguments are paths to JSON/YAML config files (later files
override earlier ones). Any remaining ``--key value`` flags are parsed
as field overrides using dot-notation (e.g. ``--model.lr 0.01``).
Args:
model_cls: The Pydantic model class that defines the config schema.
desc: Optional description shown in ``--help`` output.
argv: Raw CLI args. Default: ``sys.argv[1:]``.
raise_on_error: If True, raise ``ConfigError`` on bad input.
If False (default), print the error to stderr and exit.
Returns:
A validated instance of *model_cls*.
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
cli_pydantic-0.2.5.tar.gz
(4.0 kB
view details)
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file cli_pydantic-0.2.5.tar.gz.
File metadata
- Download URL: cli_pydantic-0.2.5.tar.gz
- Upload date:
- Size: 4.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.10.9 {"installer":{"name":"uv","version":"0.10.9","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
34c1cfb7ea7fb9b63e729be5598896771b82f479e01070215dd4df1c357f46da
|
|
| MD5 |
5d8bdd7165b3deb7d57a3004ce50a65b
|
|
| BLAKE2b-256 |
a3b7ee61615c153a3d8725e6b8620d7e69b4960d0ca8856e23db2712879736d6
|
File details
Details for the file cli_pydantic-0.2.5-py3-none-any.whl.
File metadata
- Download URL: cli_pydantic-0.2.5-py3-none-any.whl
- Upload date:
- Size: 5.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.10.9 {"installer":{"name":"uv","version":"0.10.9","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
9f3d843f32b140ac320821721274daea35295d9ecb45de6068d844851ed2259d
|
|
| MD5 |
d0094e436b843535dcbc9639b9fec760
|
|
| BLAKE2b-256 |
0fc39393649132e4640e6b55f1574d936896c389cd956e5aeb5aedfa257f4e67
|