From 33c8c1db88448457278c5bcefe79d78cdc5bce76 Mon Sep 17 00:00:00 2001 From: Tim Paine <3105306+timkpaine@users.noreply.github.com> Date: Tue, 11 Nov 2025 14:34:52 -0500 Subject: [PATCH] Update readme --- README.md | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/README.md b/README.md index 5d3ade7..e895782 100644 --- a/README.md +++ b/README.md @@ -9,5 +9,27 @@ pydantic models to argparse CLIs ## 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).