Parse command-line arguments into a dataclass
Project description
argparcel
A minimalist library to parse command-line arguments into a dataclass.
Example usage
import dataclasses
import pathlib
from typing import Literal
import argparcel
@dataclasses.dataclass(kw_only=True, frozen=True, slots=True)
class Moo:
a: int | None
b: float
choice: Literal[1, 2, 3] | None = argparcel.arg(help="choose wisely")
path: pathlib.Path | None
c: bool = True
description: str | None = None
print(argparcel.parse(Moo, ["--a", "2", "--b", "3.2"]))
print(argparcel.parse(Moo, ["--a", "2", "--b", "3.2", "--no-c"]))
print(argparcel.parse(Moo, ["--b", "4", "--c"]))
print(argparcel.parse(Moo, ["--b", "4", "--c", "--description", "moo moo"]))
print(
argparcel.parse(
Moo,
[
"--b",
"4",
"--c",
"--description",
"moo moo",
"--path",
"/somewhere/over/the/rainbow",
],
)
)
Moo(a=2, b=3.2, choice=1, path=None, c=True, description=None)
Moo(a=2, b=3.2, choice=3, path=None, c=False, description=None)
Moo(a=None, b=4.0, choice=None, path=None, c=True, description=None)
Moo(a=None, b=4.0, choice=None, path=None, c=True, description='moo moo')
Moo(
a=None,
b=4.0,
choice=None,
path=PosixPath('/somewhere/over/the/rainbow'),
c=True,
description='moo moo'
)
Another example:
class Thingy(enum.Enum):
a = enum.auto()
b = enum.auto()
@dataclasses.dataclass(kw_only=True, frozen=True, slots=True)
class Moo2:
choice: Literal[1, 2, 3] | None
no_choice: Literal["foo", "bar"] = argparcel.arc(help="baz")
thingy: Thingy = Thingy.a
argparcel.parse(Moo2, ["--help"])
usage: moo.py [-h] [--choice {1,2,3}] --no-choice {foo,bar}
[--thingy {Thingy.a,Thingy.b}]
options:
-h, --help show this help message and exit
--choice {1,2,3}
--no-choice {foo,bar}
baz
--thingy {Thingy.a,Thingy.b}
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
argparcel-0.0.1.tar.gz
(4.4 kB
view details)
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 argparcel-0.0.1.tar.gz.
File metadata
- Download URL: argparcel-0.0.1.tar.gz
- Upload date:
- Size: 4.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: uv/0.7.2
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
ed96a5032dac26af2f548a4301155b5dcb4ed285e2e68348a59e8854ad846adb
|
|
| MD5 |
f10cda3a5e1e38f76bb01c2b5c90d70e
|
|
| BLAKE2b-256 |
b5ce04f8703c13181acb675a439ef813ae23619ef90d5f27c8ba9bb884ce7ad0
|
File details
Details for the file argparcel-0.0.1-py3-none-any.whl.
File metadata
- Download URL: argparcel-0.0.1-py3-none-any.whl
- Upload date:
- Size: 4.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: uv/0.7.2
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
901c39df08738c1dc21bf681ca06f37f261df7b47f60f4c39612f12dbf47c72b
|
|
| MD5 |
0bdb77bbd7f66590734a8004240ed0d5
|
|
| BLAKE2b-256 |
d2e3b58658ad1dcb29546bba994f5ae04b04724cf1eb570b19ae5a6ebbba0df5
|