Parse command-line arguments into a dataclass
Project description
argparcel
A minimalist library to parse command-line arguments into a dataclass.
Example usage
# examples/example_0.py
import dataclasses
import argparcel
@dataclasses.dataclass(kw_only=True, frozen=True, slots=True)
class _Args:
a: int
b: float
# A `bool` argument will create a linked pair of flags `--c` and `--no-c`.
c: bool
# A command line argument will be optional if and only if a default value is
# provided in the corresponding dataclass field.
d: str | None = None
if __name__ == "__main__":
print(argparcel.parse(_Args))
$ uv run examples/example_0.py --help
usage: example_0.py [-h] --a A --b B --c | --no-c [--d D]
options:
-h, --help show this help message and exit
--a A
--b B
--c, --no-c
--d D
$ uv run examples/example_0.py --a 2 --b 3.2 --c
_Args(a=2, b=3.2, c=True, d=None)
$ uv run examples/example_0.py --a 2 --b 3.2 --no-c
_Args(a=2, b=3.2, c=False, d=None)
$ uv run examples/example_0.py --a 2 --b 3.2 --no-c --d moo
_Args(a=2, b=3.2, c=False, d='moo')
We also support:
LiteralandEnums forcing specific choices- conversion to types whose
__init__accepts a string, e.g.pathlib.Path - 'help' can be provided too
# examples/example_1.py
import dataclasses
import enum
import pathlib
from typing import Literal
import argparcel
class Bird(enum.Enum):
puffin = enum.auto()
lark = enum.auto()
@dataclasses.dataclass(kw_only=True, frozen=True, slots=True)
class _Args:
# Using a `Literal` will force a choice between 1, 2, or 3.
a: Literal[1, 2, 3]
# An enum will force a choice between the names of the enum elements.
b: Bird = Bird.puffin
# A `Path` can be automatically converted from a string. Here we also specify a
# 'help' message along with a default by using `argparcel.arg`
c: pathlib.Path | None = argparcel.arg(help="specify a path", default=None) # noqa: RUF009
if __name__ == "__main__":
print(argparcel.parse(_Args))
$ uv run examples/example_1.py --help
usage: example_1.py [-h] --a {1,2,3} [--b {puffin,lark}] [--c C]
options:
-h, --help show this help message and exit
--a {1,2,3}
--b {puffin,lark}
--c C specify a path
$ uv run examples/example_1.py --a 2
_Args(a=2, b=<Bird.puffin: 1>, c=None)
$ uv run examples/example_1.py --a 2 --b lark --c /somewhere/to/go
_Args(a=2, b=<Bird.lark: 2>, c=PosixPath('/somewhere/to/go'))
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.2.tar.gz
(5.2 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.2.tar.gz.
File metadata
- Download URL: argparcel-0.0.2.tar.gz
- Upload date:
- Size: 5.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: uv/0.7.2
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
7bd4e99c04eb1c8a77e0bd2c7d5378bcb99345085a61a5b2dcdb5562634a575c
|
|
| MD5 |
05330bf0d9f0bb192e13a7c1a3cb1ee2
|
|
| BLAKE2b-256 |
b78da25fe9db5822acda7006cfb32a4f89c0ba9de2274b739900c1825327d15f
|
File details
Details for the file argparcel-0.0.2-py3-none-any.whl.
File metadata
- Download URL: argparcel-0.0.2-py3-none-any.whl
- Upload date:
- Size: 5.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: uv/0.7.2
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
bfb0e81d70539410394f661e6a9df859e5bd9e7835f813833a49f7da2b4cf213
|
|
| MD5 |
3d7fe31754d73fab20a3ec619bdb9681
|
|
| BLAKE2b-256 |
6bf232d9df24d8762de812d5ad40ac057c89f9300cf1de37bae1c005d08ed043
|