Skip to main content

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
from pathlib import Path

@arcparser
class Args:
    path: Path
    recurse: bool = flag("-r")
    item_limit: int = 100
    output_path: Path | None

args = Args.parse()
print(f"Scanning {args.path}...")
...

For more examples see Examples.

Installation

# Using pip
$ pip install arcparse

Features

  • Positional, Option and Flag arguments
  • Multiple values per argument
  • Name overriding
  • Type conversions
  • Mutually exclusive groups
  • Subparsers
  • Parser inheritance

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.13 forward thanks to PEP 649.

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

arcparse-1.1.0.tar.gz (10.6 kB view details)

Uploaded Source

Built Distribution

arcparse-1.1.0-py3-none-any.whl (12.8 kB view details)

Uploaded Python 3

File details

Details for the file arcparse-1.1.0.tar.gz.

File metadata

  • Download URL: arcparse-1.1.0.tar.gz
  • Upload date:
  • Size: 10.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.3 CPython/3.12.3 Linux/5.15.167.4-microsoft-standard-WSL2

File hashes

Hashes for arcparse-1.1.0.tar.gz
Algorithm Hash digest
SHA256 f898b09119375e32fe70ad441f9b32e819555736e8c8741b74cf8374a586659b
MD5 bdea266befc0061a7b2c58e8da60190e
BLAKE2b-256 f51cb01ad2ea6c11ecb344dd40412889193e270248230addecaa6b65c3c2b10a

See more details on using hashes here.

File details

Details for the file arcparse-1.1.0-py3-none-any.whl.

File metadata

  • Download URL: arcparse-1.1.0-py3-none-any.whl
  • Upload date:
  • Size: 12.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.3 CPython/3.12.3 Linux/5.15.167.4-microsoft-standard-WSL2

File hashes

Hashes for arcparse-1.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 9fd481e3a83b10cb6df1b2a12a979466edc7c3ca07884327594f8c86659fd48a
MD5 e83e1f15419ebca296703214243177da
BLAKE2b-256 c17cb55ecda75497be3d4571055d79e4cc117fd241642f38183562afc977aa26

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page