docopt / docopt.cpp

C++11 port of docopt
Boost Software License 1.0
1.03k stars 146 forks source link

Negative numbers are not properly parsed as argument value #88

Open delarosatrevin opened 6 years ago

delarosatrevin commented 6 years ago

Hi, first of all, many thanks for maintaining this library and in particular this port to C++.

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. The same happens also in the original docopt in Python (which I'm also interesting to use). 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.

Show I post this issues in the other repository as well?

Thanks in advance.

jaredgrubb commented 6 years ago

Yeh, I would post it to the original repo first; I like to try to keep feature parity. But what you suggest could be tricky to do, because I'm not sure how to tell the difference.

delarosatrevin commented 6 years ago

Hi @jaredgrubb, Thanks for you reply.

I will post the issue in the original Python repo, but it seems that the maintainer is not very active these days. I understand that you want to keep feature parity.

Regarding negative values, I think it is pretty safe to assume that if you have number after the "-", then it is a negative value and not an option. I have been playing in a fork and made some modification and seems to be working for me. I will not create a PR since I think you will like this to be address first in the Python docopt.

andrew-kennedy commented 6 years ago

I'm not sure about this because the first thing that comes to mind for me are things like xargs which has an option -0. Needless to say, negative values aren't applicable in this case, it represents null terminated arg handling. Maybe the negative value stuff should be behind an option in docopt? I don't think parsing dashes with digits should necessarily be negative by default.

ghost commented 5 years ago

I'm thinking of fixing this issue in my Unreal Engine port. The way I'd do it is, if I see a - character in a token then I'll check to see if the token starts with a - and not a --, a.k.a it's a short option. Then I'll check the next character after the - and if it's numeric then I'll parse it as a negative integer instead of a short option.

Wouldn't know how to do this outside of UE4 though, I'm not great with string manipulation in standard C++. So I may not be able to contribute the fix to this repo.