Dataclass extension to use argparse
Project description
Parseable dataclasses
This library provides parse_args method to your dataclass.
Install
pip install git+https://github.com/m5ep12/parseable_dataclasses.git
Examples
Simple dataclass
@parseable_dataclass
@dataclass
class DC:
a: int
b: str
opt: float = 3.141592
or
@parseable_dataclass
class DC:
a: int
b: str
opt: float = 3.141592
and parse
assert, hasattr(DC, "parse_args")
dc = DC.parse_args("1 hello".split())
# dc(a=1, b="hello", opt=3.141592)
dc = DC.parse_args("1 hello 2.71828".split())
# dc(a=1, b="hello", opt=2.71828)
Generate help-text
dc = DC.parse_args(["-h"])
usage: DC [-h] [--opt OPT] a b
positional arguments:
a int
b str
options:
-h, --help show this help message and exit
--opt OPT float
Make CLI
from pathlib import Path
from pprint import pprint
from parseable_dataclasses import parseable_dataclass
@parseable_dataclass
class SuperTool:
src: list[Path]
verbose: bool = False
timeout: float = 10.0
retly: int = 3
prefix: str = "Hello"
def __call__(self):
# implementation your tool
pprint(self)
if __name__ == "__main__":
tool = SuperTool.parse_args()
tool()
$ python examples/make_cli.py -h
usage: SuperTool [-h] [--verbose | --no-verbose] [--timeout TIMEOUT] [--retly RETLY] [--prefix PREFIX] src
positional arguments:
src Path
options:
-h, --help show this help message and exit
--verbose, --no-verbose
bool (default: False)
--timeout TIMEOUT float (default: 10.0)
--retly RETLY int (default: 3)
--prefix PREFIX str (default: Hello)
$ python examples/make_cli.py --verbose aaa/bbb.py
SuperTool(src=WindowsPath('aaa/bbb.py'),
verbose=True,
timeout=10.0,
retly=3,
prefix='Hello')
$
Limitations
tuple
Due to my inability, I have not implemented to accept a complex tuple like following. (I'd like to convert this dataclass, but I can't find a nice implementation...)
@parseable_dataclass
@dataclass
class SomeClass:
a: tuple[int, str, float]
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file parseable_dataclasses-1.0.0.tar.gz.
File metadata
- Download URL: parseable_dataclasses-1.0.0.tar.gz
- Upload date:
- Size: 34.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
a8ba90b03a6fdf70fd9469e9961b8122bebf10c38487e53c60ad5d29b4f1ddc9
|
|
| MD5 |
2e0793ade6bfacebf6877508bfee2ddd
|
|
| BLAKE2b-256 |
2ebd799b38a02a06e120d076dfad143ea1165ca844ecc3c251d7d2ddd1038e0b
|
Provenance
The following attestation bundles were made for parseable_dataclasses-1.0.0.tar.gz:
Publisher:
python-publish.yml on m5ep12/parseable_dataclasses
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
parseable_dataclasses-1.0.0.tar.gz -
Subject digest:
a8ba90b03a6fdf70fd9469e9961b8122bebf10c38487e53c60ad5d29b4f1ddc9 - Sigstore transparency entry: 233491939
- Sigstore integration time:
-
Permalink:
m5ep12/parseable_dataclasses@ce299c0abc78cc013a5347c89d89b5047d78b2fd -
Branch / Tag:
refs/tags/1.0.1 - Owner: https://github.com/m5ep12
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
python-publish.yml@ce299c0abc78cc013a5347c89d89b5047d78b2fd -
Trigger Event:
release
-
Statement type:
File details
Details for the file parseable_dataclasses-1.0.0-py3-none-any.whl.
File metadata
- Download URL: parseable_dataclasses-1.0.0-py3-none-any.whl
- Upload date:
- Size: 4.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
1276b66a6ff0fd7b6c5f19aba4ef5c67f637bf748f45019797eee01fda06f9de
|
|
| MD5 |
065e2a1878a3d85a9f6a1bbe8b9e8fff
|
|
| BLAKE2b-256 |
67ccaba5fd9e202f83e1197d5eff487ab9fb491dd513805c655db659e920927d
|
Provenance
The following attestation bundles were made for parseable_dataclasses-1.0.0-py3-none-any.whl:
Publisher:
python-publish.yml on m5ep12/parseable_dataclasses
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
parseable_dataclasses-1.0.0-py3-none-any.whl -
Subject digest:
1276b66a6ff0fd7b6c5f19aba4ef5c67f637bf748f45019797eee01fda06f9de - Sigstore transparency entry: 233491941
- Sigstore integration time:
-
Permalink:
m5ep12/parseable_dataclasses@ce299c0abc78cc013a5347c89d89b5047d78b2fd -
Branch / Tag:
refs/tags/1.0.1 - Owner: https://github.com/m5ep12
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
python-publish.yml@ce299c0abc78cc013a5347c89d89b5047d78b2fd -
Trigger Event:
release
-
Statement type: