argparse config with pydantic model.
Project description
Argpare 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.7 - 3.11
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.
parser.print_help()
output
usage: MyApp [-h] --echo ECHOoptions: -h, --help show this help message and exit --echo ECHO </pre>
Parse command line as usual.
args = parser.parse_args(["--echo", "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 confic 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'
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
Hashes for argparse_pydantic-0.1.1-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 20c6a5b44d96d88a726c00b12e2da21c2f8be53c97ec52965904a5d6bfdb80d5 |
|
MD5 | 1dd61e0abf8fe1a0aec0fe93af3fc70a |
|
BLAKE2b-256 | 0e215d92e1111d5df159101b999152208714afa3744ae07cc332f500a82f2cdb |