Skip to main content

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 --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*.

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.6.tar.gz (4.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

cli_pydantic-0.2.6-py3-none-any.whl (5.4 kB view details)

Uploaded Python 3

File details

Details for the file cli_pydantic-0.2.6.tar.gz.

File metadata

  • Download URL: cli_pydantic-0.2.6.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

Hashes for cli_pydantic-0.2.6.tar.gz
Algorithm Hash digest
SHA256 de30fb1e507ab9167e1692901551871c0c3610693cc012dc4e3464653581f64f
MD5 06c3b86335298769fec19ed5df2267b5
BLAKE2b-256 9f7753702e52e27ca9ff440b07ecf28057f5b7745a667de218f8c5e25e372134

See more details on using hashes here.

File details

Details for the file cli_pydantic-0.2.6-py3-none-any.whl.

File metadata

  • Download URL: cli_pydantic-0.2.6-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

Hashes for cli_pydantic-0.2.6-py3-none-any.whl
Algorithm Hash digest
SHA256 8414bfc841854067b078c93bf6686f2fafa05bb74531ae93a6328adb2057625e
MD5 325560d499e1db32847d8e1db71fed75
BLAKE2b-256 819f4a8a34f196034a361314f594f1e9bb1710e383901b0288b13b6dcbadc2e6

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page