From c33102b048d57d0a7629d01188acceaf9e604e45 Mon Sep 17 00:00:00 2001 From: Dustin Lagoy Date: Fri, 1 Mar 2024 14:22:23 -0800 Subject: [PATCH 1/2] replace all non-word characters with wordify Use the regular expression set \W to replace all non-word characters instead of the fixed set [-.\s:] in wordify. Add a test for subcommands containing slashes in their name as an example. This could be tested more robustly. --- shtab/__init__.py | 4 ++-- tests/test_shtab.py | 19 +++++++++++++++++++ 2 files changed, 21 insertions(+), 2 deletions(-) diff --git a/shtab/__init__.py b/shtab/__init__.py index a34c474..4306036 100644 --- a/shtab/__init__.py +++ b/shtab/__init__.py @@ -129,8 +129,8 @@ def complete2pattern(opt_complete, shell: str, choice_type2fn) -> str: def wordify(string: str) -> str: - """Replace non-word chars [-. :] with underscores [_]""" - return re.sub(r"[-.\s:]", "_", string) + r"""Replace non-word chars [\W] with underscores [_]""" + return re.sub(r"\W", "_", string) def get_public_subcommands(sub): diff --git a/tests/test_shtab.py b/tests/test_shtab.py index de23513..a1cbe3c 100644 --- a/tests/test_shtab.py +++ b/tests/test_shtab.py @@ -241,6 +241,25 @@ def test_subparser_colons(shell, caplog): assert not caplog.record_tuples +@fix_shell +def test_subparser_slashes(shell, caplog): + parser = ArgumentParser(prog="test") + subparsers = parser.add_subparsers() + subparsers.add_parser("sub/cmd", help="help message") + with caplog.at_level(logging.INFO): + completion = shtab.complete(parser, shell=shell) + print(completion) + + if shell == "bash": + shell = Bash(completion) + shell.compgen('-W "${_shtab_test_subparsers[*]}"', "s", "sub/cmd") + shell.compgen('-W "${_shtab_test_pos_0_choices[*]}"', "s", "sub/cmd") + shell.test('-z "${_shtab_test_COMPGEN-}"') + elif shell == "zsh": + # make sure the slash was properly substituted to avoid syntax errors + assert "_shtab_test_sub/cmd" not in completion + + @fix_shell def test_add_argument_to_optional(shell, caplog): parser = ArgumentParser(prog="test") From 47e840156980256f0d2570429c5bb24de908ce77 Mon Sep 17 00:00:00 2001 From: Casper da Costa-Luis Date: Thu, 7 Mar 2024 18:27:50 +0000 Subject: [PATCH 2/2] slight tidy --- shtab/__init__.py | 4 ++-- tests/test_shtab.py | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/shtab/__init__.py b/shtab/__init__.py index 4306036..b751284 100644 --- a/shtab/__init__.py +++ b/shtab/__init__.py @@ -129,8 +129,8 @@ def complete2pattern(opt_complete, shell: str, choice_type2fn) -> str: def wordify(string: str) -> str: - r"""Replace non-word chars [\W] with underscores [_]""" - return re.sub(r"\W", "_", string) + """Replace non-word chars [\\W] with underscores [_]""" + return re.sub("\\W", "_", string) def get_public_subcommands(sub): diff --git a/tests/test_shtab.py b/tests/test_shtab.py index a1cbe3c..05ce08c 100644 --- a/tests/test_shtab.py +++ b/tests/test_shtab.py @@ -256,8 +256,8 @@ def test_subparser_slashes(shell, caplog): shell.compgen('-W "${_shtab_test_pos_0_choices[*]}"', "s", "sub/cmd") shell.test('-z "${_shtab_test_COMPGEN-}"') elif shell == "zsh": - # make sure the slash was properly substituted to avoid syntax errors assert "_shtab_test_sub/cmd" not in completion + assert "_shtab_test_sub_cmd" in completion @fix_shell