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

Allow negative floats and integers fix #397 #416

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 7 additions & 1 deletion docopt.py
Original file line number Diff line number Diff line change
Expand Up @@ -434,13 +434,19 @@ def parse_argv(tokens, options, options_first=False):
argv ::= [ long | shorts | argument ]* [ '--' [ argument ]* ] ;

"""
def isanumber(x):
try:
float(x)
return True
except ValueError:
return False
parsed = []
while tokens.current() is not None:
if tokens.current() == '--':
return parsed + [Argument(None, v) for v in tokens]
elif tokens.current().startswith('--'):
parsed += parse_long(tokens, options)
elif tokens.current().startswith('-') and tokens.current() != '-':
elif tokens.current().startswith('-') and tokens.current() != '-' and not isanumber(tokens.current()):
parsed += parse_shorts(tokens, options)
elif options_first:
return parsed + [Argument(None, v) for v in tokens]
Expand Down
9 changes: 9 additions & 0 deletions testcases.docopt
Original file line number Diff line number Diff line change
Expand Up @@ -337,6 +337,15 @@ $ prog 10
$ prog 10 20
"user-error"

$ prog -1
{"<arg>": "-1"}

$ prog -10
{"<arg>": "-10"}

$ prog -3.14159265
{"<arg>": "-3.14159265"}

$ prog
"user-error"

Expand Down