https://github.com/supimdos/pydantic-argparse
Typed Argument Parsing with Pydantic
https://github.com/supimdos/pydantic-argparse
argparse pydantic python typed validation
Last synced: 6 months ago
JSON representation
Typed Argument Parsing with Pydantic
- Host: GitHub
- URL: https://github.com/supimdos/pydantic-argparse
- Owner: SupImDos
- License: mit
- Created: 2021-09-30T14:44:27.000Z (over 4 years ago)
- Default Branch: master
- Last Pushed: 2025-09-08T12:24:45.000Z (7 months ago)
- Last Synced: 2025-09-29T05:19:17.258Z (6 months ago)
- Topics: argparse, pydantic, python, typed, validation
- Language: Python
- Homepage: https://pydantic-argparse.supimdos.com
- Size: 2.55 MB
- Stars: 130
- Watchers: 3
- Forks: 21
- Open Issues: 9
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- Contributing: CONTRIBUTING.md
- License: LICENSE
Awesome Lists containing this project
README
## Help
See [documentation](https://pydantic-argparse.supimdos.com) for help.
## Requirements
Requires Python 3.8+, and is compatible with the Pydantic v1 API.
## Installation
Installation with `pip` is simple:
```console
$ pip install pydantic-argparse
```
## Example
```py
import pydantic.v1 as pydantic
import pydantic_argparse
class Arguments(pydantic.BaseModel):
# Required Args
string: str = pydantic.Field(description="a required string", aliases=["-s"])
integer: int = pydantic.Field(description="a required integer", aliases=["-i"])
flag: bool = pydantic.Field(description="a required flag", aliases=["-f"])
# Optional Args
second_flag: bool = pydantic.Field(False, description="an optional flag")
third_flag: bool = pydantic.Field(True, description="an optional flag")
def main() -> None:
# Create Parser and Parse Args
parser = pydantic_argparse.ArgumentParser(
model=Arguments,
prog="Example Program",
description="Example Description",
version="0.0.1",
epilog="Example Epilog",
)
args = parser.parse_typed_args()
# Print Args
print(args)
if __name__ == "__main__":
main()
```
```console
$ python3 example.py --help
usage: Example Program [-h] [-v] [-s STRING] [-i INTEGER] [-f | --flag | --no-flag]
[--second-flag] [--no-third-flag]
Example Description
required arguments:
-s STRING, --string STRING
a required string
-i INTEGER, --integer INTEGER
a required integer
-f, --flag, --no-flag
a required flag
optional arguments:
--second-flag an optional flag (default: False)
--no-third-flag an optional flag (default: True)
help:
-h, --help show this help message and exit
-v, --version show program's version number and exit
Example Epilog
```
```console
$ python3 example.py --string hello -i 42 -f
string='hello' integer=42 flag=True second_flag=False third_flag=True
```
## License
This project is licensed under the terms of the MIT license.