argparse config with pydantic model.
Project description
Argparse Pydantic
Config for argparse with pydantic model.
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
Built Distribution
File details
Details for the file argparse_pydantic-0.1.4.tar.gz
.
File metadata
- Download URL: argparse_pydantic-0.1.4.tar.gz
- Upload date:
- Size: 17.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.0.0 CPython/3.10.13
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 3877f6095fec0fa9de3dd409eeff9da2481612b7dcb5f230eefad7607c06b3cd |
|
MD5 | 06cb4ce93c81784ecdb4ae825a0af2c2 |
|
BLAKE2b-256 | fb2638d00594c8d5ed0c7b1fb1ed199fd8e8a2d6e6db58d598133aa17513126f |
File details
Details for the file argparse_pydantic-0.1.4-py3-none-any.whl
.
File metadata
- Download URL: argparse_pydantic-0.1.4-py3-none-any.whl
- Upload date:
- Size: 13.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.0.0 CPython/3.10.13
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 0ea92cab510dc419960584b6c06cd78f19f47b4a5d5ae77ed9cca7eb3e5e147f |
|
MD5 | 9eb3fd9966053d5b8d35e06a23637a33 |
|
BLAKE2b-256 | 4751ac9d6a52ce48d3c72073d9f202cf07045de3212e16c7250f41df29cca32d |