Skip to content

Commit 3f87d11

Browse files
authored
Merge pull request #3 from python-project-templates/tkp/rm
Update readme
2 parents bb59eae + 33c8c1d commit 3f87d11

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

README.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,27 @@ pydantic models to argparse CLIs
99

1010
## Overview
1111

12+
A function is provided to automatically expose fields as command line arguments.
13+
14+
```python
15+
import sys
16+
from pydantic import BaseModel
17+
from p2a import parse_extra_args_model
18+
19+
20+
class MyPluginConfig(BaseModel, validate_assignment=True):
21+
extra_arg: bool = False
22+
extra_arg_with_value: str = "default"
23+
extra_arg_literal: Literal["a", "b", "c"] = "a"
24+
25+
26+
model = MyPluginConfig()
27+
parse_extra_args_model(model, sys.argv)
28+
29+
# > my-cli --extra-arg --extra-arg-with-value "test" --extra-arg-literal b
30+
```
31+
32+
For integration with existing `argparse` CLIs, a helper function to create an `argparse.SubParser` is also provided: `p2a.create_model_parser`.
33+
1234
> [!NOTE]
1335
> 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).

0 commit comments

Comments
 (0)