Permalink
Browse files

Fix bug that captured value from pattern.

  • Loading branch information...
1 parent cedb062 commit 8ce79dfcd1de42d15959bacb91db6cf1ed363a16 @keleshev keleshev committed Jul 31, 2012
Showing with 50 additions and 2 deletions.
  1. +5 −2 docopt.py
  2. +38 −0 language_agnostic_test/language_agnostic_tester.py
  3. +7 −0 test_docopt.py
View
@@ -326,7 +326,10 @@ def parse_shorts(tokens, options):
raise tokens.error('-%s requires argument' % opt.short[0])
raw = tokens.move()
value, raw = raw, ''
- opt.value = value
+ if tokens.error is DocoptExit:
+ opt.value = value
+ else:
+ opt.value = None if value else False
parsed.append(opt)
return parsed
@@ -446,5 +449,5 @@ def docopt(doc, argv=sys.argv[1:], help=True, version=None):
extras(help, version, argv, doc)
matched, left, collected = pattern.fix().match(argv)
if matched and left == []: # better error message if left?
- return Dict((a.name, a.value) for a in (pattern.flat + collected))
+ return Dict((a.name, a.value) for a in (pattern.flat + options + collected))
raise DocoptExit()
@@ -598,6 +598,33 @@
"user-error" # not a unique prefix
#
+# test_bug_option_argument_should_not_capture_default_value_from_pattern
+#
+
+r"""usage: prog [--file=<f>]
+
+"""
+$ prog
+{"--file": null}
+
+
+r"""usage: prog [--file=<f>]
+
+--file <a>
+
+"""
+{"--file": null}
+
+
+r"""Usage: tau [-a <host:port>]
+
+-a, --address <host:port> TCP address [default: localhost:6283].
+
+"""
+$ prog
+{"--address": "localhost:6283"}
+
+#
# Counting number of flags
#
@@ -607,6 +634,7 @@
$ prog -v
{"-v": true}
+
r"""Usage: prog [-v -v]
"""
@@ -619,6 +647,7 @@
$ prog -vv
{"-v": 2}
+
r"""Usage: prog -v ...
"""
@@ -634,6 +663,7 @@
$ prog -vvvvvv
{"-v": 6}
+
r"""Usage: prog [-v | -vv | -vvv]
This one is probably most readable user-friednly variant.
@@ -651,6 +681,14 @@
$ prog -vvvv
"user-error"
+
+r"""usage: prog [--ver --ver]
+
+"""
+$ prog --ver --ver
+{"--ver": 2}
+
+
#
# Counting commands
#
View
@@ -504,6 +504,12 @@ def test_bug_option_argument_should_not_capture_default_value_from_pattern():
assert docopt('usage: prog [--file=<f>]', '') == {'--file': None}
assert docopt('usage: prog [--file=<f>]\n\n--file <a>', '') == \
{'--file': None}
+ doc = """Usage: tau [-a <host:port>]
+
+ -a, --address <host:port> TCP address [default: localhost:6283].
+
+ """
+ assert docopt(doc, '') == {'--address': 'localhost:6283'}
def test_issue34_unicode_strings():
@@ -523,6 +529,7 @@ def test_count_multiple_flags():
assert docopt('usage: prog [-vv]', '-vvv')
assert docopt('usage: prog [-v | -vv | -vvv]', '-vvv') == {'-v': 3}
assert docopt('usage: prog -v...', '-vvvvvv') == {'-v': 6}
+ assert docopt('usage: prog [--ver --ver]', '--ver --ver') == {'--ver': 2}
def test_count_multiple_commands():

0 comments on commit 8ce79df

Please sign in to comment.