Declare program arguments in a type-safe way
Project description
arcparse
Declare program arguments in a type-safe way.
This project builds on top of argparse by adding type-safety and allowing a more expressive argument parser definition.
Example usage
from arcparse import arcparser, flag, positional
from pathlib import Path
@arcparser
class Args:
path: Path = positional()
recurse: bool = flag("-r")
item_limit: int = 100
output_path: Path | None
args = Args.parse()
print(f"Scanning {args.path}...")
...
Help output of this parser
usage: program.py [-h] [-r] [--item-limit ITEM_LIMIT] [--output-path OUTPUT_PATH] path
positional arguments:
path
options:
-h, --help show this help message and exit
-r, --recurse
--item-limit ITEM_LIMIT
--output-path OUTPUT_PATH
For more examples see Examples.
Installation
# Using pip
$ pip install arcparse
Features
- Positional, Option and Flag arguments
- Name overriding
- Multiple values per argument
- Type conversions
- Mutually exclusive groups
- Subparsers
- Parser inheritance (with overriding)
- Presence validation
Credits
This project was inspired by swansonk14/typed-argument-parser.
Known issues
Annotations
from __future__ import annotations makes all annotations strings at runtime. This library relies on class variable annotations's types being actual types. inspect.get_annotations(obj, eval_str=True) is used to evaluate string annotations to types in order to assign converters. If an argument is annotated with a non-builtin type which is defined outside of the argument-defining class body the type can't be found which results in NameErrors. This is avoidable either by only using custom types which have been defined in the argument-defining class body (which is restrictive), or alternatively by not using the annotations import which should not be necessary from python 3.14 forward thanks to PEP 649.
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 arcparse-1.3.1.tar.gz.
File metadata
- Download URL: arcparse-1.3.1.tar.gz
- Upload date:
- Size: 12.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.8.4 CPython/3.13.1 Darwin/24.2.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
79f8eecf24a2178d6dde0fdf7551dc9c1e2346c1c41424ca12b2d3e16668cd04
|
|
| MD5 |
146cd0bbe1c9599e7d62c673b73219ab
|
|
| BLAKE2b-256 |
29caf9e60efd93d5ca6a73272a111f830b76f8276177ac870cf2d8bc1c956da2
|
File details
Details for the file arcparse-1.3.1-py3-none-any.whl.
File metadata
- Download URL: arcparse-1.3.1-py3-none-any.whl
- Upload date:
- Size: 15.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.8.4 CPython/3.13.1 Darwin/24.2.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
fcd58ef46da8868710bb5b477c1899ff45b4041126710734e9b33a8504a7af57
|
|
| MD5 |
9a8745c336c2139989e6d61d61a70312
|
|
| BLAKE2b-256 |
9895c0a3acfb5f666c524a41f9561ff18c05bbf157e5bc844ff974014d3fe314
|