Skip to main content

A collection of argparse extensions

Project description

Motivation

I was having shower and this name came up.

At the moment this is a collection of argparse extensions I have written and found useful in recent times.

Features

  • @arg_converter decorator to write simple argument value parsers without the argparse.Action boilerplate

  • @subcommand decorator to save you from all the add_subparsers and set_defaults(func=).

  • @cli decorator to generate a command-line interface.

Example

The example below combines all the features, but the tool doesn’t enforce it on you.

If you have an existing argparse.ArgumentParser definition, you should be able to replace it with aarghparse by just changing the initialisation line to parser = aarghparse.ArgumentParse(...).

import datetime as dt

import dateutil.tz

from aarghparse import ArgumentParser, arg_converter, cli


@cli
def calendar_cli(parser: ArgumentParser, subcommand: ArgumentParser.subcommand):
    """
    Command-line calendar.

    Usage:

        python -m aarghparse.examples.calendar now --tz "Europe/Riga"

    """

    parser.add_argument(
        '--date-format',
        default=None,
    )

    @arg_converter
    def tz_arg(value):
        return dateutil.tz.gettz(value)

    @subcommand(
        name="now",
        args=[
            ["--tz", {
                "action": tz_arg,
                "help": "Timezone",
            }],
        ],
    )
    def now_cmd(args):
        """
        Prints today's date.
        """
        date_format = args.date_format or "%Y-%m-%d %H:%M:%S"
        print(dt.datetime.now(tz=args.tz).strftime(date_format))


if __name__ == "__main__":
    calendar_cli()

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

aarghparse-0.0.1.tar.gz (4.5 kB view details)

Uploaded Source

Built Distribution

aarghparse-0.0.1-py3-none-any.whl (5.3 kB view details)

Uploaded Python 3

File details

Details for the file aarghparse-0.0.1.tar.gz.

File metadata

  • Download URL: aarghparse-0.0.1.tar.gz
  • Upload date:
  • Size: 4.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.12.1 pkginfo/1.4.2 requests/2.20.0 setuptools/40.4.3 requests-toolbelt/0.8.0 tqdm/4.28.0 CPython/3.6.6

File hashes

Hashes for aarghparse-0.0.1.tar.gz
Algorithm Hash digest
SHA256 2209d3e8e8f50477fa9399be3cfbc540edb29e07c318e10458dcebd0d70bd4ac
MD5 bd7a8ee977728578ae2a594c3bd0fe12
BLAKE2b-256 2d8d350d61cd32a4219701c03c8851f60d51f991f46f67ef8abd11d5c428ac04

See more details on using hashes here.

File details

Details for the file aarghparse-0.0.1-py3-none-any.whl.

File metadata

  • Download URL: aarghparse-0.0.1-py3-none-any.whl
  • Upload date:
  • Size: 5.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.12.1 pkginfo/1.4.2 requests/2.20.0 setuptools/40.4.3 requests-toolbelt/0.8.0 tqdm/4.28.0 CPython/3.6.6

File hashes

Hashes for aarghparse-0.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 72d8c3458a29d645a574db5e83a745cb3255d06fcb8520911aa1ac277c124b41
MD5 ef59bc6fc9105bb2723efff1185ca984
BLAKE2b-256 ab285c34259fb35774dfa4797c5ddea56ca499a5286ec004e999cbc3e894bae9

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