Skip to main content

argparse config with pydantic model.

Project description

Argparse Pydantic

Config for argparse with pydantic model.

PyPI - Python Version PyPI version Tests Codecov

Simple wrapper for python argparse. Use pydantic model for you app config. It gives you typed config instead of default Namespace from argparse.

Tested on python 3.8 - 3.12

WIP

Install

Install from pypi:

pip install argparse_pydantic

Or install from github repo:

pip install git+https://github.com/ayasyrev/argparse_pydantic.git

Base use.

We use python argparse to parse arguments from command line. So, just create parser as usual:

import argparse

parser = argparse.ArgumentParser(prog="MyApp")

Than create config for you app with Pydantic.

from pydantic import  BaseModel


class AppCfg(BaseModel):
    echo: str

Now add argument to parser from you config.

from argparse_pydantic import add_args_from_model

parser = add_args_from_model(parser, AppCfg)

So we got parser with arguments from config.

It exactly like parser made classic way: parser.add_argument("echo")

Now we can use parser in you script usual way - parser.parse_args()

$ python my_app.py -h
usage: MyApp [-h] echo

positional arguments:
  echo

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

Parse command line as usual.

args = parser.parse_args(["argument from command line"])

When we parse command line, we got Namespace object. Bat we can convert it to config object.

from argparse_pydantic import create_model_obj

cfg = create_model_obj(AppCfg, args)

Now we got config with type checks / validation ont type hinting when use it at IDE.

cfg
output
AppCfg(echo='argument from command line')
cfg.echo
output
'argument from command line'

Optional if undefined.

We can use undefined arguments as positional or optional (but required).

class AppCfg2(BaseModel):
    arg_int: int
    arg_float: float = 0.1
parser = argparse.ArgumentParser(prog="MyApp")
parser = add_args_from_model(parser, AppCfg2, undefined_positional=False)
$ python my_app.py -h
usage: MyApp [-h] --arg_int ARG_INT [--arg_float ARG_FLOAT]

options:
  -h, --help            show this help message and exit
  --arg_int ARG_INT
  --arg_float ARG_FLOAT

Add types and defaults values.

And we can add type hints to help message from our config.

parser = argparse.ArgumentParser(prog="MyApp")
parser = add_args_from_model(
    parser,
    AppCfg2,
    undefined_positional=False,
    help_def_type=True,
)
$ python my_app.py -h
usage: MyApp [-h] --arg_int ARG_INT [--arg_float ARG_FLOAT]

options:
  -h, --help            show this help message and exit
  --arg_int ARG_INT     [int]
  --arg_float ARG_FLOAT
                        [float] default: 0.1

Examples

You can see examples at examples folder - Same examples as at python docs and tutorial for argparse.

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

argparse_pydantic-0.1.5.tar.gz (17.6 kB view details)

Uploaded Source

Built Distribution

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

argparse_pydantic-0.1.5-py3-none-any.whl (13.4 kB view details)

Uploaded Python 3

File details

Details for the file argparse_pydantic-0.1.5.tar.gz.

File metadata

  • Download URL: argparse_pydantic-0.1.5.tar.gz
  • Upload date:
  • Size: 17.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.4.20

File hashes

Hashes for argparse_pydantic-0.1.5.tar.gz
Algorithm Hash digest
SHA256 a3b4e35e8033a5d80c0bdaa36ebabb94c07b7a090ae31e9143cbfc8f500be021
MD5 b0ab35221c859b5becba95252ca31917
BLAKE2b-256 54856654670e2a16ed5696851fff242dc167366ea4504bf061474f46381313b8

See more details on using hashes here.

File details

Details for the file argparse_pydantic-0.1.5-py3-none-any.whl.

File metadata

File hashes

Hashes for argparse_pydantic-0.1.5-py3-none-any.whl
Algorithm Hash digest
SHA256 4212ead45cb337be600eb44646576e42891c963ba8fa61de9792b397349a8c8a
MD5 d221ff06e415dc65e101eec3f9b416a4
BLAKE2b-256 7227f4feac45e7df3e47e0f8fd1e1ddd86622ddfc89dbd901012b64f6feed04b

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