Skip to main content

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 --flag CLI 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 bar or --foo=bar
  • For lists: --nums=1,2,3 or --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 ``CliError`` on bad input.
            If False (default), print the error to stderr and exit.

    Returns:
        A validated instance of *model_cls*.

Metadata

Release files for cli-pydantic 0.2.6

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for cli-pydantic 0.2.6
File Size Uploaded Provenance Actions
cli_pydantic-0.2.6.tar.gz 4.0 kB Without provenance Details for cli_pydantic-0.2.6.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for cli-pydantic 0.2.6
File Interpreter ABI Platform Actions
cli_pydantic-0.2.6-py3-none-any.whl Python 3 none any Details for cli_pydantic-0.2.6-py3-none-any.whl

Total release size: 9.4 kB

Release files / cli_pydantic-0.2.6.tar.gz

Download URL cli_pydantic-0.2.6.tar.gz
Size 4.0 kB
Tags Source
SHA-256 checksum
How to use checksums
de30fb1e507ab9167e1692901551871c0c3610693cc012dc4e3464653581f64f
BLAKE2b-256 checksum
How to use checksums
9f7753702e52e27ca9ff440b07ecf28057f5b7745a667de218f8c5e25e372134
Upload date
Uploaded using Trusted Publishing?
What is 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}

Release files / cli_pydantic-0.2.6-py3-none-any.whl

Download URL cli_pydantic-0.2.6-py3-none-any.whl
Size 5.4 kB
Tags Python 3
SHA-256 checksum
How to use checksums
8414bfc841854067b078c93bf6686f2fafa05bb74531ae93a6328adb2057625e
BLAKE2b-256 checksum
How to use checksums
819f4a8a34f196034a361314f594f1e9bb1710e383901b0288b13b6dcbadc2e6
Upload date
Uploaded using Trusted Publishing?
What is 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}

Security for cli-pydantic 0.2.6

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page