Skip to main content

attribute autocompletion and argument parsing

Project description

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",
):
    ...

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

aaargs-0.1.3.tar.gz (9.6 kB view details)

Uploaded Source

Built Distribution

aaargs-0.1.3-py3-none-any.whl (9.4 kB view details)

Uploaded Python 3

File details

Details for the file aaargs-0.1.3.tar.gz.

File metadata

  • Download URL: aaargs-0.1.3.tar.gz
  • Upload date:
  • Size: 9.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.2.2 CPython/3.8.13 Linux/5.4.0-131-generic

File hashes

Hashes for aaargs-0.1.3.tar.gz
Algorithm Hash digest
SHA256 a6e017812797a2df45b33a89e32bead44ae403341a9b0a44b986095fd44dd7f5
MD5 c4df95fcd9781a1a1e969e06d3cb5193
BLAKE2b-256 dfbbcc34ccc9f8a7a18df92969dc6797659a05d709e15c2ed72e56310fcdeb84

See more details on using hashes here.

File details

Details for the file aaargs-0.1.3-py3-none-any.whl.

File metadata

  • Download URL: aaargs-0.1.3-py3-none-any.whl
  • Upload date:
  • Size: 9.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.2.2 CPython/3.8.13 Linux/5.4.0-131-generic

File hashes

Hashes for aaargs-0.1.3-py3-none-any.whl
Algorithm Hash digest
SHA256 b94c2cd4fe5ac8c3f370f9b0f2e815ef6c3248d86ee725cf6efaf8dca284f84b
MD5 11bdf97374828212ced859c356a71511
BLAKE2b-256 8efef0f00a01e4324904c0c1eaef0d7f0ced2e5f973c3107c16f5ac5f1f18bae

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