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

Fix magic with arguments that have a dash #6

Merged
merged 2 commits into from
Feb 19, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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