pallets / pallets/click

Support for Automatically Parsing Negative Number Arguments

Open
#2,676 3 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

docs
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

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. 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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.