From 35636bd5a8d3b380993ef20b1c3d2e7f15a53e59 Mon Sep 17 00:00:00 2001 From: barbagus <63465740+barbagus@users.noreply.github.com> Date: Tue, 10 Jan 2023 08:23:37 +0100 Subject: [PATCH 1/2] Allow quoting default values If a default value is (single or double) quoted, the quotes will be removed from the actual value. This allows for clearer default value containing spaces: ``` --name-sep= name field separator [default: " - "] ``` --- docopt/__init__.py | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/docopt/__init__.py b/docopt/__init__.py index 8f20a57..8a86a7c 100644 --- a/docopt/__init__.py +++ b/docopt/__init__.py @@ -321,7 +321,14 @@ def parse(class_, option_description: str) -> Option: argcount = 1 if argcount: matched = re.findall(r"\[default: (.*)\]", description, flags=re.I) - value = matched[0] if matched else None + if matched: + value = matched[0] + if value.startswith("'") and value.endswith("'"): + value = value[1:-1] + elif value.startswith('"') and value.endswith('"'): + value = value[1:-1] + else: + value = None return class_(short, longer, argcount, value) def single_match(self, left: list[LeafPattern]) -> TSingleMatch: From d1c77aa37c753f27fc2397bcdaa64aefbc169611 Mon Sep 17 00:00:00 2001 From: barbagus <63465740+barbagus@users.noreply.github.com> Date: Tue, 10 Jan 2023 08:28:34 +0100 Subject: [PATCH 2/2] Update __init__.py Fixes quoted default value for single characters --- docopt/__init__.py | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/docopt/__init__.py b/docopt/__init__.py index 8a86a7c..538c498 100644 --- a/docopt/__init__.py +++ b/docopt/__init__.py @@ -323,10 +323,11 @@ def parse(class_, option_description: str) -> Option: matched = re.findall(r"\[default: (.*)\]", description, flags=re.I) if matched: value = matched[0] - if value.startswith("'") and value.endswith("'"): - value = value[1:-1] - elif value.startswith('"') and value.endswith('"'): - value = value[1:-1] + if len(value) > 1: + if value.startswith("'") and value.endswith("'"): + value = value[1:-1] + elif value.startswith('"') and value.endswith('"'): + value = value[1:-1] else: value = None return class_(short, longer, argcount, value)