Skip to main content

Build ArgumentParser from pydantic model.

Project description

pydantic-argify

Python PyPI version codecov Downloads License Code style: black

Build ArgumentParser from pydantic model.

Getting started

High level api

See ./examples/00_getting_started.py

from pydantic import BaseModel, Field
from pydantic_argify import sub_command, main

class ConfigCommand1(BaseModel):
    string: str = Field(description="string parameter")
    integer: int = Field(description="integer parameter")

class ConfigCommand2(BaseModel):
    string: str = Field(description="string parameter")

@sub_command("command1")
def command1(config: ConfigCommand1):
    print(config)

@sub_command("command2")
def command2(config: ConfigCommand2):
    print(config)

if __name__ == "__main__":
    main()
$ poetry run python -m examples.00_getting_started command1 -h
usage: 00_getting_started.py command1 [-h] --string STRING --integer INTEGER

options:
  -h, --help            show this help message and exit

ConfigCommand1:
  --string STRING, -s STRING
                        string parameter
  --integer INTEGER, -i INTEGER
                        integer parameter

Low level api

from argparse import ArgumentParser
from pydantic import BaseModel, Field
from pydantic_argify import build_parser

class Config(BaseModel):
    string: str = Field(description="string parameter")
    integer: int = Field(description="integer parameter")

parser = ArgumentParser()
build_parser(parser)
parser.print_help()
usage: basic.py [-h] --string STRING --integer INTEGER

optional arguments:
  -h, --help            show this help message and exit

Config:
  --string STRING, -s STRING
                        a required string
  --integer INTEGER, -i INTEGER
                        a required integer

What's difference with other projects.

This project is dedicated to crafting an argument parser based on the Pydantic model. Unlike many other projects where the ArgumentParser functionality is concealed within the library, this tool aims to simplify its use, even in complex scenarios. For instance, handling nested sub-parsers like aws s3 cp <some options> or supporting nested Pydantic models has been a challenge in existing solutions. This library overcomes these limitations, allowing you to effortlessly incorporate intricate functionalities.

Example

from argparse import ArgumentParser
from pydantic import BaseModel, Field
from pydantic_argify import build_parser

class SubConfigA(BaseModel):
    string: str = Field(description="string parameter")
    integer: int = Field(description="integer parameter")

class SubConfigB(BaseModel):
    double: float = Field(description="a required string")
    integer: int = Field(0, description="a required integer")


parser = ArgumentParser()
subparsers = parser.add_subparsers()
build_parser(subparsers.add_parser("alpha"), SubConfigA)
build_parser(subparsers.add_parser("beta"), SubConfigB)
parser.print_help()
usage: sub_parser.py [-h] {alpha,beta} ...

positional arguments:
  {alpha,beta}

optional arguments:
  -h, --help    show this help message and exit

Nested config

See: ./examples/06_nested_field

$ python -m examples.06_nested_field -h
usage: 06_nested_field.py [-h] --name NAME --child.name CHILD.NAME [--child.age CHILD.AGE] --child.is-active CHILD.IS_ACTIVE

options:
  -h, --help            show this help message and exit

Config:
  --name NAME, -n NAME

ChildConfig:
  --child.name CHILD.NAME, -c CHILD.NAME
  --child.age CHILD.AGE
  --child.is-active CHILD.IS_ACTIVE

Additional config

Behaviour of pydantic can be controlled via the Config class or extra arguments of Field. Config is affected all fields. Extra arguments of Field is affected specific field.

cli_disable_prefix
Prefix of argument of boolean type for `store_false`. Default to --disable-
cli_enable_prefix
Prefix of argument of boolean type for `store_true`. Default to --enable-

Future works

  • [ ]: Options completion for bash

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

pydantic_argify-0.4.0.tar.gz (10.8 kB view details)

Uploaded Source

Built Distribution

pydantic_argify-0.4.0-py3-none-any.whl (12.2 kB view details)

Uploaded Python 3

File details

Details for the file pydantic_argify-0.4.0.tar.gz.

File metadata

  • Download URL: pydantic_argify-0.4.0.tar.gz
  • Upload date:
  • Size: 10.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.2 CPython/3.11.7 Windows/10

File hashes

Hashes for pydantic_argify-0.4.0.tar.gz
Algorithm Hash digest
SHA256 ff01f4d4f1f1265ff8184f9fc74c1f33254e462c1b87d7851857d7b58e4ac150
MD5 57d001259207f5455dfb80c227c2ae6b
BLAKE2b-256 54161e1811e5926ff7e12a2ffed42bcf1e5cd8c0a50dbc9047d8fbe9c9a9d4d9

See more details on using hashes here.

File details

Details for the file pydantic_argify-0.4.0-py3-none-any.whl.

File metadata

  • Download URL: pydantic_argify-0.4.0-py3-none-any.whl
  • Upload date:
  • Size: 12.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.2 CPython/3.11.7 Windows/10

File hashes

Hashes for pydantic_argify-0.4.0-py3-none-any.whl
Algorithm Hash digest
SHA256 d826a67f7779418d204e39fe05b826b338284323be902753fe3b1bf4186281ae
MD5 efee3715bafcbf75c6f9cc0a5761314f
BLAKE2b-256 2fb338a3ecf135de25930cacd2893c611be8840b9423d8f9336003e64bf1629f

See more details on using hashes here.

Supported by

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