Skip to main content

pydantic-parse

Pydantic adaptor for argparse

Example

from pydantic_parse import ArgField, ArgModel, PydanticArgParser

def main():

    class TestChoices(enum.StrEnum):
        alice = "Alice"
        bob = "Bob"

    class TestModel(ArgModel):
        name: TestChoices = ArgField(description="Name")
        some_value: Optional[str] = ArgField(
            description="value",
            optional=True,
            default=None,
            flag=True
        )
        flag: bool = ArgField(description="flag", default=False, flag=True)

    parser = PydanticArgParser()
    parser.add_arguments_from_model(TestModel)
    args = parser.parse_args()

CLI result:

$ pydantic-parse -h
usage: pydantic-parse [-h] [--some-value SOME_VALUE] [--flag] {Alice,Bob}

positional arguments:
  {Alice,Bob}           Name

options:
  -h, --help            show this help message and exit
  --some-value SOME_VALUE
                        value
  --flag                flag

CLI input:

my-package Alice --some-value 42

results in vars(args):

{
  'name': <TestChoices.alice: 'Alice'>,
  'some_value': '42',
  'flag': False
}

Or of course

model = TestModel(**vars(args))

gives

TestModel
name=<TestChoices.alice: 'Alice'> some_value='42' flag=False

Made with poetiq

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pydantic_parse-0.1.1.tar.gz (6.9 kB view details)

Uploaded Source

Built Distribution

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

pydantic_parse-0.1.1-py3-none-any.whl (10.6 kB view details)

Uploaded Python 3

File details

Details for the file pydantic_parse-0.1.1.tar.gz.

File metadata

  • Download URL: pydantic_parse-0.1.1.tar.gz
  • Upload date:
  • Size: 6.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/7.0.0 CPython/3.12.3

File hashes

Hashes for pydantic_parse-0.1.1.tar.gz
Algorithm Hash digest
SHA256 a8ac75c20d1a15b2811c74fa007db827cc2e22a4634fcd5c04f19e6577f30ab9
MD5 ca5ebf3582c956b2ad8a2e9b1cd14291
BLAKE2b-256 00f0955e925db3abc7f42f9894c4289652205085f7af9ac95065035d9a6f34d3

See more details on using hashes here.

File details

Details for the file pydantic_parse-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: pydantic_parse-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 10.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/7.0.0 CPython/3.12.3

File hashes

Hashes for pydantic_parse-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 77318980133ff9562b87f3784dcbfcd6dfc02819e043db99e5a075e4356adb3d
MD5 b4eec3d253349e4a633be471805f4b88
BLAKE2b-256 0f3320bba32e762116e9080aa663517472cc2595aed9a905b517a1832cc9c923

See more details on using hashes here.

Release history Release notifications | RSS feed

This release

0.1.1 This release

2 files

0.1.0

2 files

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