Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for negative numbers #2676

Open
thomask77 opened this issue Feb 13, 2024 · 0 comments
Open

Support for negative numbers #2676

thomask77 opened this issue Feb 13, 2024 · 0 comments

Comments

@thomask77
Copy link

thomask77 commented Feb 13, 2024

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant