Skip to content

Commit

Permalink
Fix bug that captured value from pattern.
Browse files Browse the repository at this point in the history
  • Loading branch information
Vladimir Keleshev committed Jul 31, 2012
1 parent cedb062 commit 8ce79df
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 2 deletions.
7 changes: 5 additions & 2 deletions docopt.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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()
38 changes: 38 additions & 0 deletions language_agnostic_test/language_agnostic_tester.py
Original file line number Diff line number Diff line change
Expand Up @@ -597,6 +597,33 @@
$ prog --a
"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
#
Expand All @@ -607,6 +634,7 @@
$ prog -v
{"-v": true}
r"""Usage: prog [-v -v]
"""
Expand All @@ -619,6 +647,7 @@
$ prog -vv
{"-v": 2}
r"""Usage: prog -v ...
"""
Expand All @@ -634,6 +663,7 @@
$ prog -vvvvvv
{"-v": 6}
r"""Usage: prog [-v | -vv | -vvv]
This one is probably most readable user-friednly variant.
Expand All @@ -651,6 +681,14 @@
$ prog -vvvv
"user-error"
r"""usage: prog [--ver --ver]
"""
$ prog --ver --ver
{"--ver": 2}
#
# Counting commands
#
Expand Down
7 changes: 7 additions & 0 deletions test_docopt.py
Original file line number Diff line number Diff line change
Expand Up @@ -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():
Expand All @@ -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():
Expand Down

0 comments on commit 8ce79df

Please sign in to comment.