New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

aaargs

Package Overview
Dependencies
Maintainers
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

aaargs

attribute autocompletion and argument parsing

  • 0.1.3
  • PyPI
  • Socket score

Maintainers
1

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

Keywords

FAQs


Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc