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

Negative number as positional argument with "options_first" #158

Open
miso-belica opened this issue Oct 23, 2013 · 9 comments
Open

Negative number as positional argument with "options_first" #158

miso-belica opened this issue Oct 23, 2013 · 9 comments

Comments

@miso-belica
Copy link

Hi @halst, I read your solution for negative numbers as positionals in #93. But I think It doesn't work properly. I use docopt from master branch. Below are my test cases.

# -*- coding: utf8 -*-

"""
Negative positionals.

Usage:
    ./proj2 --help
    ./proj2 --sqrt X
    ./proj2 --asin X
    ./proj2 --triangle AX AY BX BY CX CY

"""

from __future__ import absolute_import
from __future__ import division, print_function, unicode_literals

from docopt import docopt


if __name__ == "__main__":
    args = docopt(__doc__, options_first=True)
    print(args)

py27 neg.py --triangle -1 2 3 4 5 6
py27 neg.py --asin -1

When I pass negative number as 2nd/3rd/... argument for --triangle everything works as I expect. Is there any solution (except for [--]) for this?

@keleshev
Copy link
Member

I would suggest to use commands instead of your options:

Usage:
    ./proj2 sqrt X
    ./proj2 asin X
    ./proj2 triangle AX AY BX BY CX CY

Now asin -1 works. And you are not allowing confusing invocations like 0.5 --asin, only asin 0.5.

@miso-belica
Copy link
Author

Yes I know commands are better here. But usage is given and can't be changed.

Regardless of my example there are plenty of valid examples that should work IMO, e.g.:

Usage:
    neg.py [--verbose] X Y Z

python neg.py -1 -2 3
python neg.py --verbose -1 -2 3

It is possible to change behavior of docopt here so unresolved option would be treated as positional argument where it makes sense?

@keleshev
Copy link
Member

The general solution is to use --. It is specifically made for when arguments look like options.

To make --asin -1 work (assuming, you can't --asin=-1, or asin -1) is either:

  • fork docopt
  • sneakily append -- into sys.argv based on some rules, e.g. if you have only long options, you can find first short option -1 and append -- right before.

@miso-belica
Copy link
Author

Thanks for your suggestions but I didn't open this issue only for solving my particular problem. Maybe this should be considered being a bug. Especially with options_first=True. This issue may be closed if you think error on python neg.py -1 -2 3 is the proper behavior. I think users may be confused if hit this case. I was hoping docopt is smart enough to help me with these cases :(

@keleshev
Copy link
Member

Do you have any specific proposal on how docopt should treat it? I thought that -- is sufficient.

@miso-belica
Copy link
Author

As I said before: unknown option should be passed as positional argument. For example:
python neg.py --verbose -1 -2 -3 resolve --verbose as option then try to resolve -1 and because it's not known option it passes it as an positional argument because it makes sense. If there is not any positional argument error message (usage pattern) is shown like now. I think it's more intuitive behavior than the current one.

Or do you think there is any usage pattern where it doesn't make sense?

@keleshev
Copy link
Member

In this case if user mistypes --verobse instead of --verbose, it will be interpreted as an argument. That's not good.

@miso-belica
Copy link
Author

python neg.py --verobse -1 -2 -3 triggers the error due too many positional arguments. When I have pattern like this: proj [--verbose] X Y [Z] then --verobse is interpreted as an positional argument. But still --verobse is not number so it will raise exception.

IMHO example similar with --verobse is for this usage:

Usage:
    proj stdout
    proj FILE

If user mistypes python proj stduot then stduot is interpreted as FILE. It's not good too. But user should be responsible for her mistakes.

@Overdrivr
Copy link

@keleshev Is it possible to simply deactivate short options single dash for a specific command ? I believe in most cases we could be fine only using double dashes for options and then leaving single dash for negative numbers.

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

3 participants