docopt / docopt

Create *beautiful* command-line interfaces with Python
MIT License
7.95k stars 560 forks source link

Negative numbers are not properly parsed as argument value #397

Open delarosatrevin opened 6 years ago

delarosatrevin commented 6 years ago

Hi, first of all, many thanks for developing this library.

I'm starting to write a library for scientific image processing (Electron Microscopy images) and I was considering to use docopt for command line parsing. In the past I have written my own home-made tools, of course much more limited.

I have started playing with docopt and I have found that negatives values are not properly parsed. Maybe it is getting wrong the regex matching since the negative numbers start with a dash as the optional arguments. I tried the simple example of the calculator and it is not working as expected. I think this issue is important for any scientific application that needs to deal with negatives values.

SeeSpotRun commented 6 years ago

The - characters are processed before they get to docopt. This is more of a shell limitation than a docopt one.

Depending on your usecase you probably work around the issue by quoting negative numbers, eg:

$ calculator add 3 '-2'
delarosatrevin commented 6 years ago

Hi, thanks for your answer.

I think this is not a shell 'issue', since all arguments are passed in the sys.argv list (containing '-' or not). I know that quoting in literals could be a workaround, but then the programs invocation is a bit unnatural. Another workaround in docopt arguments and value parsing could be to check the next character after the '-', if it is a digit, then is very likely to be a number. I have tested that in my fork of the cpp port.

SeeSpotRun commented 6 years ago

Yes you're right; I was confusing myself.

galactics commented 6 years ago

Have you seen this solution ? I don't know if it's relevant to your use case, but it was a good fit for mine.

delarosatrevin commented 6 years ago

Thanks @galactics! I made a small change in my docopt-cpp fork and I'm happy with it so far. But I haven't decided yet if I will go fully with docopt anyway. I'm writing a library and so far only a few example programs in C++.