Skip to main content

coveralls PyTest PyPI version

Aaargs ...

I'm not a huge fan of the argparse library that ships with Python. Personally, I much prefer typer or click. But argparse is often used so this is my approach in bringing at least attribute autocompletion to the argparse library.

Let us take a look at the official documentation and use their examples:

import argparse

parser = argparse.ArgumentParser(
                    prog = 'ProgramName',
                    description = 'What the program does',
                    epilog = 'Text at the bottom of help')

parser.add_argument('filename')           # positional argument
parser.add_argument('-c', '--count')      # option that takes a value
parser.add_argument('-v', '--verbose',
                    action='store_true')  # on/off flag

args = parser.parse_args()
print(args.filename, args.count, args.verbose)

Why isn't the argparse.ArgumentParser a container class, like a dataclass?

So my approach to solve this looks like this:

from aaargs import ArgumentParser, Argument

class MyParser(ArgumentParser):
    rog = "ProgramName"
    description = "What the program does"
    epilog = "Text at the bottom of help"

    # You can define arguments directly
    filename = Argument(positional=True)  # positional argument
    encoding = Argument()  # keyword argument '--encoding'
    
    # or pass the 'name_or_flags' argument
    count = Argument("-c", "--count")
    verbose = Argument("-v", "--verbose", action="store_true")
    
    # annotations are also supported for boolean arguments
    debug: bool = Argument() # --debug with action="store_true"

parser: argparse.ArgumentParser = MyParser.get_parser()
args: MyParser = MyParser.parse_args()

You can also print the parser just like the original:

args = MyParser.parse_args(
        ["README.md", "--encoding", "utf-8", "-c", "3", "--debug"]
    )

print(args)
>>> MyParser(count='3', debug=True, encoding='utf-8', filename='README.md', verbose=False)
print(args.encoding) # this will autocomplete 🎉
>>> "utf-8"

You can also create a Parser using keyword arguments if you prefer (I don't):

from aaargs import ArgumentParser

class MyParser(
    ArgumentParser,
    prog="ProgramName",
    description="What the program does",
    epilog="Text at the bottom of help",
):
    ...

Metadata

Release files for aaargs 0.1.3

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for aaargs 0.1.3
File Size Uploaded Provenance
aaargs-0.1.3.tar.gz 9.6 kB Without provenance Details

Built distribution (wheel)

Table of built distributions (wheels) for aaargs 0.1.3
File Interpreter ABI Platform
aaargs-0.1.3-py3-none-any.whl Python 3 none any Details

Total release size: 19.0 kB

Release files / aaargs-0.1.3.tar.gz

Download URL aaargs-0.1.3.tar.gz
Size 9.6 kB
Tags Source
SHA-256 checksum
How to use checksums
a6e017812797a2df45b33a89e32bead44ae403341a9b0a44b986095fd44dd7f5
BLAKE2b-256 checksum
How to use checksums
dfbbcc34ccc9f8a7a18df92969dc6797659a05d709e15c2ed72e56310fcdeb84
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.2.2 CPython/3.8.13 Linux/5.4.0-131-generic

Release files / aaargs-0.1.3-py3-none-any.whl

Download URL aaargs-0.1.3-py3-none-any.whl
Size 9.4 kB
Tags Python 3
SHA-256 checksum
How to use checksums
b94c2cd4fe5ac8c3f370f9b0f2e815ef6c3248d86ee725cf6efaf8dca284f84b
BLAKE2b-256 checksum
How to use checksums
8efef0f00a01e4324904c0c1eaef0d7f0ced2e5f973c3107c16f5ac5f1f18bae
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.2.2 CPython/3.8.13 Linux/5.4.0-131-generic

Security for aaargs 0.1.3

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page