If I try to pass a negative number to click, it interprets it as an option. This happens when I define the number either as int, float or leave it to default to string.
Here's a tiny example to illustrate the issue
import click
@click.command()
@click.argument('number', type=float)
def main(number):
print(number)
if __name__ == '__main__':
main()
and I run it as
โฏ python tmp.py '-10'
Error: no such option: -1
or even as
โฏ python tmp.py -10
Error: no such option: -1
EDIT: I am running Python 3.5.1 and Click version 6.3 installed through the Anaconda python distribution.
@ZeeD26 That worked! Out of curiosity - Would it be useful to have a feature in click (probably passed through the context setting) that forces it to read the first N command line inputs as the arguments and only then read the options?
That would effectively be the reverse of the current situation, since I will have to specify the options first followed by -- and then the arguments.
I'm unsure if that is useful. Feel free to open a new issue about that, I'm going to close this one because this parsing behavior is basically impossible to get fixed.
Why is it that argparse is able to handle this case? Does it not allow single dash options to begin with a number, and is that something that click purposely wants to allow (a number as option with no space in between)?
import argparse
p = argparse.ArgumentParser()
p.add_argument('number', type=float)
args = p.parse_args()
print(args.number)
$ python argtest.py -10
-10.0
@scottstanie restricting options to pure integers would be a way to mitigate this, but the issue still pertains when you want to pass something like -infinity as a number value. Also it would be a compat breaking change so I am not sure if this is worth fixing at this point vs just dealing with it (tbh I don't see the problem with adding -- -- that's basically how git's CLI parser works)
gotcha thanks for the quick response, I hadn't seen the -- in git or other CLIs before, so I didn't know it standard.
I also just bumped into this. The suggested workaround... is a workaround. It should be possible to disambiguate by checking:
I also still think this is pretty irritating @pjz has a pretty good fix outlined.
A -- token disables option parsing so all further tokens are considered arguments.
python example.py -- -10
Most helpful comment
I also just bumped into this. The suggested workaround... is a workaround. It should be possible to disambiguate by checking: