Yada
Yada (Yet Another Dataclass Argument Parser!) is a library to automatically generate argparse.ArgumentParser given data classes. Compared to some available options such as: Huggingface's HfArgumentParser, argparse_dataclass, and tap, it offers the following benefits:
- Static Type Checking
- Nested data classes and complex types
- Easy to extend and customize the parser
- Generate command line arguments given the data classes.
Installation
Install via PyPI (requires Python 3.8+):
pip install t2-yada
How to use
Yada's parser can be constructed from data classes. It relies on fieds' annotated types to construct correct argument parsers.
import yada
from dataclasses import dataclass
from typing import *
@dataclass
class CityArgs:
city: Literal["LA", "NY"]
@dataclass
class NestedArgs:
name: str
nested: CityArgs
parser = yada.YadaParser(NestedArgs)
args = parser.parse_args() # or use parser.parse_known_args() -- the two functions are similar to argparse.parse_args or argparse.parse_known_args
Note: YadaParser is annotated as a generic type: YadaParser[C, R] where C denotes the classes, and R denotes the instance of the classes created from the arguments. Therefore, in the above example, C is inferred as NestedArgs, but R is unknown, hence the type of args variable is unknown. To overcome this typing limitation, Yada provides several options for up to 10 data classes (yada.Parser1, yada.Parser2, ...). Below is two examples:
parser = yada.Parser1(NestedArgs)
args = parser.parse_args() # <-- args now has type NestedArgs
parser = yada.Parser2((NestedArgs, CityArgs))
args = parser.parse_args() # <-- args now has type Tuple[NestedArgs, CityArgs]
Note: we recommend to use one of the specific parsers yada.Parser<N> instead of the generic yada.YadaParser if possible as they provide strong typing support.
Configuring Yada
Add help message
Yada reads the help message from the key property of dataclasses.Field.metadata
import yada
from dataclasses import dataclass, field
from typing import *
@dataclass
class CityArgs:
city: Literal["LA", "NY"] = field(metadata={"help": "city's which you want to get the timezone"})
parser = yada.Parser1(CityArgs)
Metadata
Release files for t2-yada 1.2.2
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| t2_yada-1.2.2.tar.gz | 10.3 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| t2_yada-1.2.2-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 21.3 kB
Release files / t2_yada-1.2.2.tar.gz
| Download URL | t2_yada-1.2.2.tar.gz |
|---|---|
| Size | 10.3 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
0ccded189d9c4940128bc40b8e964784197410dd8f8355ead38d64e3619ab000
|
|
BLAKE2b-256 checksum How to use checksums |
22b2645073e93f28f27dfff59f16a77117793c42da63ca3abd63626b0fc89cf7
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/4.0.2 CPython/3.8.18
|
Release files / t2_yada-1.2.2-py3-none-any.whl
| Download URL | t2_yada-1.2.2-py3-none-any.whl |
|---|---|
| Size | 11.0 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
b82752a094435aa33b202ef6491eefd5fc1152957163969d2c1df15a8d879d3f
|
|
BLAKE2b-256 checksum How to use checksums |
d2e0dbf63729a6499f6fc82b497764f28b40eea9c1edeaab4f94e0289f08b4da
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/4.0.2 CPython/3.8.18
|