Skip to content

Commit 36a4fb0

Browse files
committed
Add readme for custom functions
1 parent a4de85d commit 36a4fb0

File tree

1 file changed

+38
-0
lines changed

1 file changed

+38
-0
lines changed

README.md

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,5 +15,43 @@ This library provides a minimal CLI `hatch-build`, equivalent to [`hatchling bui
1515
hatch-build -- --my-custom-plugin-arg
1616
```
1717

18+
As a convenience, we provide an `argparse` wrapper to extract the extra args:
19+
20+
```python
21+
from hatch_build import parse_extra_args
22+
23+
args, extras = parse_extra_args(my_argparse_parser)
24+
```
25+
26+
### Configuration
27+
28+
If you manage your hatch plugin config as a pydantic model, a function is provided to automatically expose fields as command line arguments.
29+
30+
```python
31+
from hatchling.builders.hooks.plugin.interface import BuildHookInterface
32+
from pydantic import BaseModel
33+
34+
35+
from hatch_build import parse_extra_args_model
36+
37+
38+
class MyPluginConfig(BaseModel, validate_assignment=True):
39+
extra_arg: bool = False
40+
extra_arg_with_value: str = "default"
41+
extra_arg_literal: Literal["a", "b", "c"] = "a"
42+
43+
class MyHatchPlugin(BuildHookInterface[MyPluginConfig]):
44+
PLUGIN_NAME = "my-hatch-plugin"
45+
46+
def initialize(self, version: str, build_data: dict[str, Any]) -> None:
47+
my_config_model = MyPluginConfig(**self.config)
48+
parse_extra_args_model(my_config_model)
49+
50+
print(f"{my_config_model.extra_arg} {my_config_model.extra_arg_with_value} {my_config_model.extra_arg_literal}")
51+
52+
# > hatch-build -- --extra-arg --extra-arg-with-value "test" --extra-arg-literal b
53+
# True test b
54+
```
55+
1856
> [!NOTE]
1957
> 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)