https://github.com/python-project-templates/p2a
pydantic models to argparse CLIs
https://github.com/python-project-templates/p2a
argparse cli model pydantic python
Last synced: 9 days ago
JSON representation
pydantic models to argparse CLIs
- Host: GitHub
- URL: https://github.com/python-project-templates/p2a
- Owner: python-project-templates
- License: apache-2.0
- Created: 2025-11-11T02:34:30.000Z (4 months ago)
- Default Branch: main
- Last Pushed: 2026-02-15T05:56:27.000Z (21 days ago)
- Last Synced: 2026-02-15T12:31:47.178Z (21 days ago)
- Topics: argparse, cli, model, pydantic, python
- Language: Python
- Homepage:
- Size: 34.2 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
- Code of conduct: .github/CODE_OF_CONDUCT.md
Awesome Lists containing this project
README
# p2a
pydantic models to argparse CLIs
[](https://github.com/python-project-templates/p2a/actions/workflows/build.yaml)
[](https://codecov.io/gh/python-project-templates/p2a)
[](https://github.com/python-project-templates/p2a)
[](https://pypi.python.org/pypi/p2a)
## Overview
A function is provided to automatically expose fields as command line arguments.
```python
import sys
from pydantic import BaseModel
from p2a import parse_extra_args_model
class MyPluginConfig(BaseModel, validate_assignment=True):
extra_arg: bool = False
extra_arg_with_value: str = "default"
extra_arg_literal: Literal["a", "b", "c"] = "a"
model = MyPluginConfig()
parse_extra_args_model(model, sys.argv)
# > my-cli --extra-arg --extra-arg-with-value "test" --extra-arg-literal b
```
For integration with existing `argparse` CLIs, a helper function to create an `argparse.SubParser` is also provided: `p2a.create_model_parser`.
> [!NOTE]
> This library was generated using [copier](https://copier.readthedocs.io/en/stable/) from the [Base Python Project Template repository](https://github.com/python-project-templates/base).