Support for Automatically Parsing Negative Number Arguments
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 17.7k
- Forks
- 2.3k
- Avg merge
- 1d 30m
- Merged PRs (30d)
- 18
Description
I would like to re-open the discussion from issue #555 and #1624.
My program makes heavy use of numeric arguments, including negative numbers, special float values (NaN, Infinity..), negative hex numbers, etc.
At the moment I'm using the following monkey patch as a work-around:
from click.parser import OptionParser, ParsingState
def _is_number(arg: str) -> bool:
try:
_ = int(arg, 0)
except ValueError:
try:
_ = float(arg)
except ValueError:
return False
return True
# Allow negative numbers as arguments
#
# Based on click/parser.py v8.1.7
#
def _process_args_for_options(self, state: ParsingState) -> None:
while state.rargs:
arg = state.rargs.pop(0)
if arg == "--":
return
elif arg[:1] in self._opt_prefixes and len(arg) > 1 and not _is_number(arg):
self._process_opts(arg, state)
elif self.allow_interspersed_args:
state.largs.append(arg)
else:
state.rargs.insert(0, arg)
return
# Monkey-patch the OptionParser class
#
OptionParser._process_args_for_options = _process_args_for_options
It would be nice, if click would offer an official mode that accepts negative numbers.
I can also submit a PR, but would need further input on how to properly integrate the feature.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with click/parser.py, especially OptionParser._process_args_for_options and the v8.1.7 behavior shown in the workaround. Read issues #555 and #1624 before deciding how an official mode should integrate negative numbers, NaN, Infinity, and negative hex values. Done means the behavior and integration are agreed and covered for those argument forms.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python
- Domain
- cli
- Issue type
- Feature
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100