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

Now arguments are also returned as attributes. #482

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
18 changes: 17 additions & 1 deletion docopt.py
Original file line number Diff line number Diff line change
Expand Up @@ -487,6 +487,19 @@ def __repr__(self):
return '{%s}' % ',\n '.join('%r: %r' % i for i in sorted(self.items()))


def clean_name(name):
""" Make name suitable for a class attribute """
# Remove starting -/--
if name[1] == '-':
name = name[2:]
elif name[0] == '-':
name = name[1:]
# Convert - to _
name = name.replace('-', '_')
# Finally make it lower case
return name.lower()


def docopt(doc, argv=None, help=True, version=None, options_first=False):
"""Parse `argv` based on command-line interface described in `doc`.

Expand Down Expand Up @@ -577,5 +590,8 @@ 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))
d = Dict((a.name, a.value) for a in (pattern.flat() + collected))
for a in (pattern.flat() + collected):
setattr(d, clean_name(a.name), a.value)
return d
raise DocoptExit()