Build ArgumentParser from pydantic model.
Project description
pydantic-argify
Build ArgumentParser from pydantic model.
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.
</code></pre>
<h2>Example 1</h2>
<pre lang="python"><code>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
Example 2
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
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
Release history Release notifications | RSS feed
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 pydantic_argify-0.3.1.tar.gz
.
File metadata
- Download URL: pydantic_argify-0.3.1.tar.gz
- Upload date:
- Size: 10.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.2.2 CPython/3.10.11 Windows/10
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 2782497997843d59d717297946e882fb8e823cde878bddb1dd45b2820ef4a6fd |
|
MD5 | c6438a80c5416835a35468e827bccef9 |
|
BLAKE2b-256 | f6b941928426b54d3c9962a72260fb171e5e6462803adb56a0b49b37ab0c660b |
File details
Details for the file pydantic_argify-0.3.1-py3-none-any.whl
.
File metadata
- Download URL: pydantic_argify-0.3.1-py3-none-any.whl
- Upload date:
- Size: 10.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.2.2 CPython/3.10.11 Windows/10
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 5219be516cb6e25698301b1ade3af33446498075649d14a6bd360a56bac83acf |
|
MD5 | c86696938a93bf828a5d4efb95a0da90 |
|
BLAKE2b-256 | 4d2e913a725c1fb4f934518d2b07e2079174196b1a66d76fc3330edb8317f289 |