Skip to main content

Numeric range for argparse arguments

Project description

pypi github

argparse-range

Easily check that an argument is within a range for argparse

Use it like this:

>>> from argparse import ArgumentParser, ArgumentTypeError
>>> from argparse_range import range_action
>>> parser = ArgumentParser()
>>> _ = parser.add_argument("rangedarg", action=range_action(0, 10), help="An argument")
>>> args = parser.parse_args(["0"])
>>> args.rangedarg
0
>>> parser.parse_args(["20"])
Traceback (most recent call last):
    ....
argparse.ArgumentTypeError: Invalid choice: 20 (must be in range 0..=10)

Features

Helptext is added transparently

foo.py --help

usage: foo.py [-h] rangedarg

positional arguments:
  rangedarg   An argument (must be in range 0..=10)

optional arguments:
  -h, --help  show this help message and exit

Infers type by default

>>> from argparse import ArgumentParser
>>> from argparse_range import range_action
>>> parser = ArgumentParser()
>>> _ = parser.add_argument("intarg", action=range_action(0, 10))
>>> _ = parser.add_argument("floatarg", action=range_action(25.0, 40.0))
>>> _ = parser.add_argument("explicit", action=range_action(25.0, 40.0), type=int)
>>> args = parser.parse_args(["5", "30", "30"])
>>> assert isinstance(args.intarg, int)
>>> assert isinstance(args.floatarg, float)
>>> assert isinstance(args.explicit, int)

Handles optional arguments and defaults just like normal parsing

>>> from argparse import ArgumentParser
>>> from argparse_range import range_action
>>> parser = ArgumentParser()
>>> _ = parser.add_argument("--maybe", action=range_action(0, 10), nargs="?")
>>> parser.parse_args([])
Namespace(maybe=None)
>>> parser.parse_args(["--maybe"])
Namespace(maybe=None)
>>> parser.parse_args(["--maybe", "5"])
Namespace(maybe=5)
>>> parser.parse_args(["--maybe", "20"])
Traceback (most recent call last):
    ....
argparse.ArgumentTypeError: Invalid choice: 20 (must be in range 0..=10)

Handles multiple arguments just like normal parsing

>>> from argparse import ArgumentParser
>>> from argparse_range import range_action
>>> parser = ArgumentParser()
>>> _ = parser.add_argument("many", action=range_action(0, 10), nargs="*")
>>> parser.parse_args([])
Namespace(many=[])
>>> parser.parse_args(["5"])
Namespace(many=[5])
>>> parser.parse_args(["1", "2", "3", "4"])
Namespace(many=[1, 2, 3, 4])

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-range-0.1.2.tar.gz (3.2 kB view details)

Uploaded Source

Built Distribution

argparse_range-0.1.2-py3-none-any.whl (3.1 kB view details)

Uploaded Python 3

File details

Details for the file argparse-range-0.1.2.tar.gz.

File metadata

  • Download URL: argparse-range-0.1.2.tar.gz
  • Upload date:
  • Size: 3.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.1.12 CPython/3.8.10 Linux/5.10.60.1-microsoft-standard-WSL2

File hashes

Hashes for argparse-range-0.1.2.tar.gz
Algorithm Hash digest
SHA256 b99df03820ab1b1b662efac08c990253cc18718124e1d59f27f2e6dca0292224
MD5 a3a596a48d9db23b2ce645f7a1da2feb
BLAKE2b-256 5c7bfa5e4aef2da626e763b367da6900327ee7be29479fcd49734cba969bd599

See more details on using hashes here.

File details

Details for the file argparse_range-0.1.2-py3-none-any.whl.

File metadata

  • Download URL: argparse_range-0.1.2-py3-none-any.whl
  • Upload date:
  • Size: 3.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.1.12 CPython/3.8.10 Linux/5.10.60.1-microsoft-standard-WSL2

File hashes

Hashes for argparse_range-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 7b2963291ac3db2b3ed9f388cc7183b9cf9066ab5805e9bc08f0b65fd76af63a
MD5 7b777efe53cb11e40af1b9031d9b3a02
BLAKE2b-256 6b51069f53f8be6a0b0184fab8c988a8e4f69345aec3380bd4da63a0cc4b522b

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