Skip to main content

No project description provided

Project description

negargparse

Tests License: MIT

An ArgumentParser which understands negative numbers.


Getting negargparse

The preferred way to use negargparse is to copy the module negargparse.py into your project. This is because currently one can expect frequent API changes. We do use semantic versioning to help you guard against it.

negargparse can also be installed from PyPI:

pip install negargparse

Usage

The argparse module in python provides tools to parse command line arguments. However, sometimes it doesn't interpret arguments correctly when dealing with negative numbers. Currently argparse correctly interprets negative numbers if they are strictly numbers and there are no numeral options like -1. However it doesn't work when working with non standard numeral formats. e.g. declination is usually written in the (+|-)(degree):(arcminute):(arcsecond) format like -16:32:45.46. argparse would interpret this as an option, rather than an argument and quit (because of finding an unknown argument) before we have a chance to examine it. Any way to solve this issue requires doing some hacky gymnastics, negargparse is here to do that for you.

negargparse provides a subclass of ArgumentParser called NegativeArgumentParser, which escapes all arguments which begin with a '-' followed by a digit. Now declare the type of arguments that you expect to be negative as one of the provided types NegString, NegInt, NegFloat to unescape them.

>>> parser = negargparse.NegativeArgumentParser()
>>> parser.add_argument("eggs", type=NegString)
>>> parser.parse_args(["-23h15m04s"])
Namespace(eggs='-23h15m04s')

License

This repository is distributed under the MIT License, though the module is available under the more permissive MIT-0 license. See the LICENSE or at the top of the module for the license text.

Known issues

Right now this project is more or less a framework to develop the module. Except for some narrow cases, it breaks more pre-existing functionality of argparse than provide new ones. Right now you are better off not using this if that bothers you.

  • If the user types a negative number in an unexpected (not of the provided NegString etc. types) argument, this would escape the said field, but since it is not declared as a NegString/NegInt/NegFloat, it wouldn't be unescaped.

  • This module doesn't respect the POSIX convention of leaving the arguments after --.

  • Currently single digit options are unusable.

  • This project currently lacks support for versions earlier than python 3.7. This is not because of lack of any features in python 3.6, but because of lack of support for annotations used in this module. At the release of v1, a special untyped release for python 3.6 shall be made.

Development

This section tells you how to set up the development environment and run tests, which can be used to either modify the module for their own purpose of for potential contributors.

Setup development environment

Ensure python >= 3.8

Ensure you have poetry installed. You can get it from https://github.com/python-poetry/poetry. This requires poetry >=1.2 which is currently only released as alpha.

  1. Clone the repository
  2. Install the package
poetry install --with dev
  1. Install pre-commit hooks
poetry run pre-commit install

Now whenever developing, enter virtual environment using

poetry shell

From now on this document assumes that you are within a virtual environment. Otherwise prepend you commands with poetry run.

Testing

Tests are written in the pytest framework, to run them use:

pytest

To run extensive tests within isolated environments use tox:

tox

Links

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

negargparse-0.2.0.tar.gz (5.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

negargparse-0.2.0-py3-none-any.whl (5.8 kB view details)

Uploaded Python 3

File details

Details for the file negargparse-0.2.0.tar.gz.

File metadata

  • Download URL: negargparse-0.2.0.tar.gz
  • Upload date:
  • Size: 5.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.2.0a2 CPython/3.10.0 Linux/5.10.16.3-microsoft-standard-WSL2

File hashes

Hashes for negargparse-0.2.0.tar.gz
Algorithm Hash digest
SHA256 246df52602370bceedf334022de73ab3efebcfc7c9698722fe6ea406263edc6e
MD5 c3d029b6258ae29dc5290c5ce80638a7
BLAKE2b-256 e21c24fb94992ea8b57dd7dd1aa7cd1a18f6854837174dade26c08248f3e7527

See more details on using hashes here.

File details

Details for the file negargparse-0.2.0-py3-none-any.whl.

File metadata

  • Download URL: negargparse-0.2.0-py3-none-any.whl
  • Upload date:
  • Size: 5.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.2.0a2 CPython/3.10.0 Linux/5.10.16.3-microsoft-standard-WSL2

File hashes

Hashes for negargparse-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 9d8fe178067750c34d96a18bc8a8ec16e4b8208c27658f26e3d1dffbac8f62aa
MD5 831836c85cdf16e6b18c2ac114c011f4
BLAKE2b-256 3f10d751c499365df4c7c192dc8d1a6526509de6ac66e1dd13260ba7b9d08a1f

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page