Skip to main content

Declare program arguments declaratively and type-safely

Project description

Arcparse

Declare program arguments declaratively and type-safely. Optionally set argument defaults dynamically (see Dynamic argument defaults)

Disclaimer: This library is young and probably highly unstable. Use at your own risk. Pull requests are welcome.

Example usage

from arcparse import ArcParser, positional


class Args(ArcParser):
    name: str = positional()
    age: int = positional()
    hobbies: list[str] = positional()
    happy: bool


args = Args.parse()
print(f"Hi, my name is {args.name}!")

For a complete overview of features see Features.

Features

Required and optional arguments

Arguments without explicitly assigned argument class are implicitly options (prefixed with --). A non-optional typehint results in required=True for options. Defaults can be set by directly assigning them. You can use option() to further customize the argument.

class Required(ArcParser):
    required: str
    optional: str | None
    default: str = "foo"
    default_with_help: str = option(default="bar", help="help message")

Positional arguments

Positional arguments use positional(). Type-hinting the argument as list[...] uses nargs="*" in the background for positional arguments.

class Positional(ArcParser):
    single: str = positional()
    multiple: list[str] = positional()

Flags

All arguments type-hinted as bool are flags, they use action="store_true" in the background. Use no_flag() to easily create a --no-... with action="store_false". Flags as well as options can also define short forms for each argument. They can also disable the long form with short_only=True.

class FlagArgs(ArcParser):
    sync: bool
    recurse: bool = no_flag(help="Do not recurse")

    debug: bool = flag("-d")  # both -d and --debug
    verbose: bool = flag("-v", short_only=True)  # only -v

Type conversions

Automatic type conversions are supported. The type-hint is used in type=... in the background (unless it's str, which does no conversion). Using a StrEnum instance as a type-hint automatically populates choices. A custom type-converter can be used by passing converter=... to either option or positional.

class TypeArgs(ArcParser):
    class Result(StrEnum):
        PASS = "pass"
        FAIL = "fail"

        @classmethod
        def from_int(cls, arg: str) -> Self:
            number = int(arg)
            return cls.PASS if number == 1 else cls.FAIL

    number: int
    result: Result
    custom: Result = option(converter=Result.from_int)

Name overriding

Type-hinting an option as list[...] uses action="append" in the background. Use this in combination with name_override=... to get rid of the ...s suffixes.

class NameOverrideArgs(ArcParser):
    values: list[str] = option(name_override="value")

Dynamic argument defaults

The parse() classmethod supports an optional dictionary of defaults, which replace the statically defined defaults before parsing arguments. This might be useful for saving some arguments in a config file allowing the user to provide only the ones that are not present in the config.

Credits

This project was inspired by swansonk14/typed-argument-parser.

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-0.1.1.tar.gz (5.6 kB view details)

Uploaded Source

Built Distribution

arcparse-0.1.1-py3-none-any.whl (6.6 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: arcparse-0.1.1.tar.gz
  • Upload date:
  • Size: 5.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.7.0 CPython/3.12.0 Linux/6.5.10-300.fc39.x86_64

File hashes

Hashes for arcparse-0.1.1.tar.gz
Algorithm Hash digest
SHA256 b9d00ff2120703fb443daec1d8ef8b6a658001d8246ec9fb18ed30806aa90a2b
MD5 c7202b3d7bf345511b5b648e72032900
BLAKE2b-256 a38fed5742931909cc89d8417431f19b7f3da75fb05ff627a8e5076ba07a4239

See more details on using hashes here.

File details

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

File metadata

  • Download URL: arcparse-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 6.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.7.0 CPython/3.12.0 Linux/6.5.10-300.fc39.x86_64

File hashes

Hashes for arcparse-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 dfc97d2afc174a2917ec066ff4025d54366779e456b1b0f5ba6b7287c5dcf9aa
MD5 b6eaa65f895e825022ae43ef2144aefa
BLAKE2b-256 ee0fc43433eac04c7fcb16cda8c58a5f06e5dd7196dacba50b60f03b6ae05af8

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