Skip to main content

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

  • datetime_action(fmt='%Y-%m-%dT%H:%M:%S')

    Maps command-line arguments in the given format to datetime objects. Only accepts valid date-times in that format.

    eg. An action of datetime_action() would map a command-line argument of 2000-01-01T00:00:00 to the Python object datetime.datetime(2000, 1, 1, 0, 0).

  • date_action(fmt='%Y-%m-%d')

    Maps command-line arguments in the given format to date objects. Only accepts valid dates in that format.

    eg. An action of date_action() would map a command-line argument of 2000-01-01 to the Python object datetime.date(2000, 1, 1).

  • time_action(fmt='%H:%M:%S')

    Maps command-line arguments in the given format to time objects. Only accepts valid times in that format.

    eg. An action of time_action() would map a command-line argument of 00:00:00 to the Python object datetime.time(0, 0).

  • timedelta_action(fmt='%H:%M:%S')

    Maps command-line arguments in the given format to timedelta objects. Only accepts valid time-deltas in that format.

    Note: As this uses a timedelta object, this may behave in unexpected ways when attempting to use months or years.

    eg. An action of timedelta_action() would map a command-line argument of 01:00:00 to the Python object datetime.timedelta(0, 3600)).

  • json_action(**kwargs)

    Maps command-line arguments to JSON objects. Only accepts valid JSON. Passes kwargs on to json.loads.

    eg. An action of json_action() would map a command-line argument of {"a": 1, "b": 2} to the Python object {"a": 1, "b": 2}.

  • mapping_action(possible_values, key_normalizer=None)

    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.

    key_normalizer, if given, allows variants of the keys, by normalizing them before looking them up in the given mapping.

    eg. An action of

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

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

    Using str.lower as the key_normalizer makes the command-line argument case-insensitive.

  • enum_action(enum_class, key_normalizer=None)

    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.

    key_normalizer, if given, allows variants of the keys, by normalizing them before looking them up in the given mapping.

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

    enum_action(Colour, str.lower)
    

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

    Using str.lower as the key_normalizer makes the command-line argument case-insensitive.

  • python_literal_action()

    Maps command-line arguments to Python literals. Only accepts valid Python literal objects.

    Similar to json_action, but also allows tuples, and complex numbers.

    eg. An action of python_literal_action() would map a command-line argument of (1, 2) to the Python object (1, 2).

Installation

Install and update using the standard Python package manager pip:

pip install argparse-utils

Metadata

Release files for argparse-utils 1.3.0

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

Source distribution (sdist)

Source distribution for argparse-utils 1.3.0
File Size Uploaded Provenance
argparse_utils-1.3.0.tar.gz 5.1 kB Without provenance Details

Built distribution (wheel)

Table of built distributions (wheels) for argparse-utils 1.3.0
File Interpreter ABI Platform
argparse_utils-1.3.0-py3-none-any.whl Python 3 none any Details

Total release size: 11.9 kB

Release files / argparse_utils-1.3.0.tar.gz

Download URL argparse_utils-1.3.0.tar.gz
Size 5.1 kB
Tags Source
SHA-256 checksum
How to use checksums
2a9d6109968fe34f96c801991e9afc61dd1be14735bfb89e7e6333584acc52f0
BLAKE2b-256 checksum
How to use checksums
7a1b43805331672ef4c382d0706cdda33c03ce28e93e7f1c3d1a828176fa2312
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.46.0 CPython/3.8.2

Release files / argparse_utils-1.3.0-py3-none-any.whl

Download URL argparse_utils-1.3.0-py3-none-any.whl
Size 6.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
9d92e9bc662434bcec430e2e55f136167225bb7d1567d1c333a89d18e5cdac62
BLAKE2b-256 checksum
How to use checksums
1541f592d44283385f8e628ef69528262d3e65df7ed24ecf187d93f8cb9fb5a7
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.46.0 CPython/3.8.2

Security for argparse-utils 1.3.0

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