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

Add argument name to Options class and make it accessible from the argument dict #346

Open
wants to merge 4 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
20 changes: 14 additions & 6 deletions docopt.py
Original file line number Diff line number Diff line change
Expand Up @@ -179,27 +179,31 @@ def single_match(self, left):

class Option(LeafPattern):

def __init__(self, short=None, long=None, argcount=0, value=False):
def __init__(self, short=None, long=None, argcount=0, value=False, atype=None):
assert argcount in (0, 1)
self.short, self.long, self.argcount = short, long, argcount
self.value = None if value is False and argcount else value
self.atype = atype

@classmethod
def parse(class_, option_description):
short, long, argcount, value = None, None, 0, False
short, long, argcount, value, atype = None, None, 0, False, None
options, _, description = option_description.strip().partition(' ')
options = options.replace(',', ' ').replace('=', ' ')
for s in options.split():
if s.startswith('--'):
long = s
elif s.startswith('-'):
short = s
elif s:
atype = s
argcount = 1
else:
argcount = 1
if argcount:
matched = re.findall('\[default: (.*)\]', description, flags=re.I)
value = matched[0] if matched else None
return class_(short, long, argcount, value)
return class_(short, long, argcount, value, atype)

def single_match(self, left):
for n, pattern in enumerate(left):
Expand All @@ -212,8 +216,8 @@ def name(self):
return self.long or self.short

def __repr__(self):
return 'Option(%r, %r, %r, %r)' % (self.short, self.long,
self.argcount, self.value)
return 'Option(%r, %r, %r, %r, %r)' % (self.short, self.long,
self.argcount, self.value, self.atype)


class Required(BranchPattern):
Expand Down Expand Up @@ -483,6 +487,10 @@ def extras(help, version, options, doc):


class Dict(dict):
def __init__(self, options, *args):
self.options = options
dict.__init__(self, *args)

def __repr__(self):
return '{%s}' % ',\n '.join('%r: %r' % i for i in sorted(self.items()))

Expand Down Expand Up @@ -577,5 +585,5 @@ def docopt(doc, argv=None, help=True, version=None, options_first=False):
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(options, ((a.name, a.value) for a in (pattern.flat() + collected)))
raise DocoptExit()
24 changes: 12 additions & 12 deletions test_docopt.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,28 +23,28 @@ def test_option():
assert Option.parse('-h --help') == Option('-h', '--help')
assert Option.parse('-h, --help') == Option('-h', '--help')

assert Option.parse('-h TOPIC') == Option('-h', None, 1)
assert Option.parse('--help TOPIC') == Option(None, '--help', 1)
assert Option.parse('-h TOPIC --help TOPIC') == Option('-h', '--help', 1)
assert Option.parse('-h TOPIC, --help TOPIC') == Option('-h', '--help', 1)
assert Option.parse('-h TOPIC, --help=TOPIC') == Option('-h', '--help', 1)
assert Option.parse('-h TOPIC') == Option('-h', None, 1, False, 'TOPIC')
assert Option.parse('--help TOPIC') == Option(None, '--help', 1, False, 'TOPIC')
assert Option.parse('-h TOPIC --help TOPIC') == Option('-h', '--help', 1, False, 'TOPIC')
assert Option.parse('-h TOPIC, --help TOPIC') == Option('-h', '--help', 1, False, 'TOPIC')
assert Option.parse('-h TOPIC, --help=TOPIC') == Option('-h', '--help', 1, False, 'TOPIC')

assert Option.parse('-h Description...') == Option('-h', None)
assert Option.parse('-h --help Description...') == Option('-h', '--help')
assert Option.parse('-h TOPIC Description...') == Option('-h', None, 1)
assert Option.parse('-h TOPIC Description...') == Option('-h', None, 1, False, 'TOPIC')

assert Option.parse(' -h') == Option('-h', None)

assert Option.parse('-h TOPIC Descripton... [default: 2]') == \
Option('-h', None, 1, '2')
Option('-h', None, 1, '2', 'TOPIC')
assert Option.parse('-h TOPIC Descripton... [default: topic-1]') == \
Option('-h', None, 1, 'topic-1')
Option('-h', None, 1, 'topic-1', 'TOPIC')
assert Option.parse('--help=TOPIC ... [default: 3.14]') == \
Option(None, '--help', 1, '3.14')
Option(None, '--help', 1, '3.14', 'TOPIC')
assert Option.parse('-h, --help=DIR ... [default: ./]') == \
Option('-h', '--help', 1, "./")
Option('-h', '--help', 1, "./", 'DIR')
assert Option.parse('-h TOPIC Descripton... [dEfAuLt: 2]') == \
Option('-h', None, 1, '2')
Option('-h', None, 1, '2', 'TOPIC')


def test_option_name():
Expand Down Expand Up @@ -613,4 +613,4 @@ def test_parse_section():

def test_issue_126_defaults_not_parsed_correctly_when_tabs():
section = 'Options:\n\t--foo=<arg> [default: bar]'
assert parse_defaults(section) == [Option(None, '--foo', 1, 'bar')]
assert parse_defaults(section) == [Option(None, '--foo', 1, 'bar', '<arg>')]