Skip to content

Commit

Permalink
Merge pull request #6 from johnscillieri/master
Browse files Browse the repository at this point in the history
Fix magic with arguments that have a dash
  • Loading branch information
itdaniher authored Feb 19, 2021
2 parents bbed40a + 05790ef commit cbf1e8b
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 1 deletion.
2 changes: 1 addition & 1 deletion docopt.py
Original file line number Diff line number Diff line change
Expand Up @@ -651,7 +651,7 @@ def __repr__(self):
return "{%s}" % ",\n ".join("%r: %r" % i for i in sorted(self.items()))

def __getattr__(self, name: str) -> Optional[Union[str, bool]]:
return self.get(name) or {name: self.get(k) for k in self.keys() if name in [k.lstrip("-"), k.lstrip("<").rstrip(">")]}.get(name)
return self.get(name) or {name: self.get(k) for k in self.keys() if name in [k.lstrip("-").replace("-", "_"), k.lstrip("<").rstrip(">")]}.get(name)


def docopt(
Expand Down
29 changes: 29 additions & 0 deletions tests/test_docopt_ng.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,3 +143,32 @@ def test_even_more_indirect():
return test_indirect()

assert test_even_more_indirect() == {"--long": ""}


def test_docopt_ng_dot_access_with_dash():
doc = """Usage: prog [-vqrd] [FILE]
prog INPUT OUTPUT
prog --help
Options:
-d --dash-arg test this argument
-v print status messages
-q report only file names
-r show all occurrences of the same error
--help
"""
arguments = docopt.docopt(doc, "-v -d file.py")
assert arguments == {
"--dash-arg": True,
"-v": True,
"-q": False,
"-r": False,
"--help": False,
"FILE": "file.py",
"INPUT": None,
"OUTPUT": None,
}
assert arguments.v == True
assert arguments.FILE == "file.py"
assert arguments.dash_arg == True

0 comments on commit cbf1e8b

Please sign in to comment.