Skip to content

Commit

Permalink
Improve the README introduction (#60)
Browse files Browse the repository at this point in the history
  • Loading branch information
rogiervandergeer authored Mar 25, 2024
1 parent 8e18af6 commit 3ada81b
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 16 deletions.
81 changes: 66 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,18 +1,11 @@
# pydargs

Pydargs allows configuring a (Pydantic) dataclass through command line arguments.

## Installation

Pydargs can be installed with your favourite package manager. For example:

```
pip install pydargs
```
Easily configure a CLI application using a (Pydantic) dataclass.

## Usage

A minimal usage example would be:
Pydargs instantiates a dataclass that is used as (configuration) input of your entrypoint from command line arguments.
For example, in `example.py`:

```python
from dataclasses import dataclass
Expand All @@ -24,18 +17,76 @@ class Config:
number: int
some_string: str = "abc"


def main(config: Config) -> None:
"""Your main functionality"""
print(f"> Hello {config.number} + {config.some_string}")


if __name__ == "__main__":
config = parse(Config)
main(config)
```

After which this entrypoint can be called with
Here the `Config` dataclass serves as input (configuration) of the `main` function. Pydargs facilitates
instantiating the `config` instance, allowing the user to use command line arguments to set or override the
values of its fields:


```shell
entrypoint --number 42
$ python example.py --number 1
> Hello 1 abc
$ python example.py --number 2 --some-string def
> Hello 2 def
$ python example.py --help
usage: example.py [-h] --number NUMBER [--some-string SOME_STRING]

options:
-h, --help show this help message and exit
--number NUMBER
--some-string SOME_STRING
(default: abc)
```
or
```shell
entrypoint --number 42 --some-string abcd

This saves you from having to maintain boilerplate code such as

```python
from argparse import ArgumentParser
from dataclasses import dataclass


@dataclass
class Config:
number: int
some_string: str = "abc"


def main(config: Config) -> None:
"""Your main functionality"""
print(f"> Hello {config.number} + {config.some_string}")


if __name__ == "__main__":
parser = ArgumentParser()
parser.add_argument("--number", type=int)
parser.add_argument("--some-string", dest="some_string", default="abc")
namespace = parser.parse_args()
config = Config(number=namespace["number"], some_string=namespace["some_string"])
main(config)
```

Aside from that, pydargs supports:
- [a wide variety of field types](#supported-field-types),
- [nested dataclasses](#nested-dataclasses),
- [subparsers / commands](#subparsers),
- [pydantic dataclasses](https://docs.pydantic.dev/latest/concepts/dataclasses/) to help you with validation.

## Installation

Pydargs can be installed with your favourite package manager. For example:

```
pip install pydargs
```

## ArgumentParser arguments
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ authors = [
{name = "Guus Verstegen", email = "[email protected]"},
{name = "Rogier van der Geer", email = "[email protected]"},
]
description = "Pydargs allows configuring a dataclass through command line arguments."
description = "Easily configure a CLI application using a (Pydantic) dataclass."
readme = "README.md"
requires-python = ">=3.9"
license = {text = "BSD"}
Expand Down

0 comments on commit 3ada81b

Please sign in to comment.