From 8f811d3cf345f1f496f7c35c85c17a22e143b11e Mon Sep 17 00:00:00 2001 From: John Scillieri <5930913+johnscillieri@users.noreply.github.com> Date: Sun, 13 Oct 2019 20:15:48 -0400 Subject: [PATCH 1/2] Add test for underscore magic in argument name --- tests/test_docopt_ng.py | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/tests/test_docopt_ng.py b/tests/test_docopt_ng.py index 65e8d5d..ae3487d 100644 --- a/tests/test_docopt_ng.py +++ b/tests/test_docopt_ng.py @@ -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 From 05790efdb3a8e30d073219c5f93d3efd8fc2e547 Mon Sep 17 00:00:00 2001 From: John Scillieri <5930913+johnscillieri@users.noreply.github.com> Date: Sun, 13 Oct 2019 20:17:03 -0400 Subject: [PATCH 2/2] fix underscore magic in argument name Allow users to do magic with an argument like --arg-name and convert it to args.arg_name --- docopt.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docopt.py b/docopt.py index 7702b6a..f9a28c7 100644 --- a/docopt.py +++ b/docopt.py @@ -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(