Skip to main content

A collection of utilities for the Python standard-library argparse module

Project description

argparse_utils

argparse_utils provides a collection of utilities for the Python standard-library argparse module. These utilities assist with parsing command-line arguments to Python objects.

Example

Consider a simple command-line script which accepts a colour as it's only argument, and immediately prints the Python representation of that object.

from argparse import ArgumentParser
from enum import Enum

from argparse_utils import enum_action

class Colours(Enum):
    red = 1
    green = 2
    blue = 3

if __name__ == "__main__":
    parser = ArgumentParser()
    parser.add_argument('colour', action=enum_action(Colours))

    arguments = parser.parse_args()

    print(repr(arguments.colour))
$ argparse_utils_example.py red
<Colours.red: 1>

Without the enum_action action, arguments.colour would be the string 'red', rather than the enum value Colours.red. What's more, the action ensures that only the values given in the enum are allowed, instead of any string value.

Reference

  • mapping_action(possible_values)

    Takes a dictionary whose keys are the allowed values, and maps those values to the values found in the dictionary. Only the values found as keys in the dictionary are allowed as command-line arguments.

    eg. An action of

    mapping_action({
        'red': (255, 0, 0),
        'green': (0, 255, 0),
        'blue': (0, 0, 255)
    })
    

    would map a command-line argument of red to the Python object (255, 0, 0).

  • enum_action(enum_class)

    Takes an Enum class, and maps the string representation of the keys to the appropriate enum value. Only the values found in the enum are allowed as command-line arguments.

    eg. Using the Colour enum, from the first example, an action of

    enum_action(Colour)
    

    would map a command-line argument of red to the enum Colour.red value.

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

argparse_utils-1.0.0.tar.gz (2.3 kB view details)

Uploaded Source

Built Distribution

argparse_utils-1.0.0-py3-none-any.whl (2.9 kB view details)

Uploaded Python 3

File details

Details for the file argparse_utils-1.0.0.tar.gz.

File metadata

  • Download URL: argparse_utils-1.0.0.tar.gz
  • Upload date:
  • Size: 2.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.11.0 pkginfo/1.4.2 requests/2.18.4 setuptools/40.2.0 requests-toolbelt/0.8.0 tqdm/4.19.6 CPython/3.6.2

File hashes

Hashes for argparse_utils-1.0.0.tar.gz
Algorithm Hash digest
SHA256 25a81b3e286ea8002491a3188053722927931895d0fa75fc7da281265d114365
MD5 101861ef8a19cb6de36363dfdfb74f28
BLAKE2b-256 3fcbb10909dd98f03ee0638ca031eeaad012328ccaa0c6f64c98b23b57b707e0

See more details on using hashes here.

File details

Details for the file argparse_utils-1.0.0-py3-none-any.whl.

File metadata

  • Download URL: argparse_utils-1.0.0-py3-none-any.whl
  • Upload date:
  • Size: 2.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.11.0 pkginfo/1.4.2 requests/2.18.4 setuptools/40.2.0 requests-toolbelt/0.8.0 tqdm/4.19.6 CPython/3.6.2

File hashes

Hashes for argparse_utils-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 21a8860cf8af89c2fd3a635f4fc3e7edaa1c7708907c4ec1be3ca214ab14eda9
MD5 0a9a84598d13b21c89eba05b4eb1d5c9
BLAKE2b-256 08dafc2a67ff374d80360c5590cc8a8e79684f15ed6b7ab3c2aa14e23147c887

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