Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Experimental refactoring of docopt function.

  • Loading branch information...
commit 8e8cc15160240deb28404d5ad38a5c4486e97009 1 parent e5d5b5a
@keleshev keleshev authored
Showing with 27 additions and 21 deletions.
  1. +9 −8 docopt.py
  2. +18 −13 test_docopt.py
View
17 docopt.py
@@ -303,7 +303,10 @@ def parse_long(tokens, options):
value = tokens.move()
elif value is not None:
raise tokens.error('%s must not have an argument' % opt.name)
- opt.value = value or True #(True if tokens.error is DocoptExit else False)
+ if tokens.error is DocoptExit:
+ opt.value = value or True
+ else:
+ opt.value = None if value else False
return [opt]
@@ -329,7 +332,7 @@ def parse_shorts(tokens, options):
opt = Option(o.short, o.long, o.argcount, o.value)
raw = raw[1:]
if opt.argcount == 0:
- value = True #if tokens.error is DocoptExit else False
+ value = True if tokens.error is DocoptExit else False
else:
if raw == '':
if tokens.current() is None:
@@ -456,14 +459,12 @@ def __repr__(self):
def docopt(doc, argv=sys.argv[1:], help=True, version=None):
DocoptExit.usage = docopt.usage = usage = printable_usage(doc)
- pot_options = parse_doc_options(doc)
- pattern = parse_pattern(formal_usage(usage), options=pot_options)
- argv = parse_argv(argv, options=pot_options)
+ options = parse_doc_options(doc)
+ pattern = parse_pattern(formal_usage(usage), options)
+ argv = parse_argv(argv, options)
extras(help, version, argv, doc)
matched, left, collected = pattern.fix().match(argv)
if matched and left == []: # better error message if left?
- pot_arguments = [a for a in pattern.flat
- if type(a) in [Argument, Command]]
return Dict((a.name, a.value) for a in
- (pot_options + pot_arguments + collected))
+ (options + pattern.flat + collected))
raise DocoptExit()
View
31 test_docopt.py
@@ -118,21 +118,21 @@ def test_parse_argv():
def test_parse_pattern():
o = [Option('-h'), Option('-v', '--verbose'), Option('-f', '--file', 1)]
assert parse_pattern('[ -h ]', options=o) == \
- Required(Optional(Option('-h', None, 0, True)))
+ Required(Optional(Option('-h')))
assert parse_pattern('[ ARG ... ]', options=o) == \
Required(Optional(OneOrMore(Argument('ARG'))))
assert parse_pattern('[ -h | -v ]', options=o) == \
- Required(Optional(Either(Option('-h', None, 0, True),
- Option('-v', '--verbose', 0, True))))
- assert parse_pattern('( -h | -v [ --file f.txt ] )', options=o) == \
+ Required(Optional(Either(Option('-h'),
+ Option('-v', '--verbose'))))
+ assert parse_pattern('( -h | -v [ --file <f> ] )', options=o) == \
Required(Required(
- Either(Option('-h', None, 0, True),
- Required(Option('-v', '--verbose', 0, True),
- Optional(Option('-f', '--file', 1, 'f.txt'))))))
- assert parse_pattern('(-h|-v[--file=f.txt]N...)', options=o) == \
- Required(Required(Either(Option('-h', None, 0, True),
- Required(Option('-v', '--verbose', 0, True),
- Optional(Option('-f', '--file', 1, 'f.txt')),
+ Either(Option('-h'),
+ Required(Option('-v', '--verbose'),
+ Optional(Option('-f', '--file', 1, None))))))
+ assert parse_pattern('(-h|-v[--file=<f>]N...)', options=o) == \
+ Required(Required(Either(Option('-h'),
+ Required(Option('-v', '--verbose'),
+ Optional(Option('-f', '--file', 1, None)),
OneOrMore(Argument('N'))))))
assert parse_pattern('(N [M | (K | L)] | O P)', options=[]) == \
Required(Required(Either(
@@ -142,7 +142,7 @@ def test_parse_pattern():
Argument('L')))))),
Required(Argument('O'), Argument('P')))))
assert parse_pattern('[ -h ] [N]', options=o) == \
- Required(Optional(Option('-h', None, 0, True)),
+ Required(Optional(Option('-h')),
Optional(Argument('N')))
assert parse_pattern('[options]', options=o) == Required(
Optional(AnyOptions()))
@@ -150,7 +150,7 @@ def test_parse_pattern():
Optional(AnyOptions()),
Argument('A'))
assert parse_pattern('-v [options]', options=o) == Required(
- Option('-v', '--verbose', 0, True),
+ Option('-v', '--verbose'),
Optional(AnyOptions()))
assert parse_pattern('ADD', options=o) == Required(Argument('ADD'))
@@ -508,3 +508,8 @@ def test_issue40():
docopt('usage: prog --help-commands | --help', '--help')
assert docopt('usage: prog --aabb | --aa', '--aa') == {'--aabb': False,
'--aa': True}
+
+def test_experimental():
+ assert docopt('usage: prog [--file=<f>]', '') == {'--file': None}
+ assert docopt('usage: prog [--file=<f>]\n\n--file <a>', '') == \
+ {'--file': None}
Please sign in to comment.
Something went wrong with that request. Please try again.