From 6a7ba97813cbc356ff2a7ea76319bf2d5efe68a4 Mon Sep 17 00:00:00 2001 From: Niklas Reisser Date: Wed, 8 Jun 2022 15:23:08 +0200 Subject: [PATCH 1/2] Support compopt in completion functions --- shtab/__init__.py | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/shtab/__init__.py b/shtab/__init__.py index 87ca026..ac3fbee 100644 --- a/shtab/__init__.py +++ b/shtab/__init__.py @@ -421,10 +421,16 @@ def complete_bash(parser, root_prefix=None, preamble="", choice_functions=None): COMPREPLY=( $(compgen -W "${current_option_strings[*]}" -- "${completing_word}") ) else # use choices & compgen + # call current_action_compgen without a subshell to support compopt local IFS=$'\\n' + tmpfile="$(mktemp)" + [ -n "${current_action_compgen}" ] && \\ + "${current_action_compgen}" "${completing_word}" > "$tmpfile" + mapfile current_action_compreply < "$tmpfile" + rm "$tmpfile" + COMPREPLY=( $(compgen -W "${current_action_choices}" -- "${completing_word}") \\ - $([ -n "${current_action_compgen}" ] \\ - && "${current_action_compgen}" "${completing_word}") ) + ${current_action_compreply[@]} ) fi return 0 From 0a964b3f6589fac239e11d87dc1230e64c17d521 Mon Sep 17 00:00:00 2001 From: Sourcery AI <> Date: Fri, 5 Aug 2022 16:58:51 +0000 Subject: [PATCH 2/2] 'Refactored by Sourcery' --- shtab/__init__.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/shtab/__init__.py b/shtab/__init__.py index ac3fbee..b6f1ab6 100644 --- a/shtab/__init__.py +++ b/shtab/__init__.py @@ -211,8 +211,9 @@ def recurse(parser, prefix): new_nargs, ) = recurse( positional.choices[choice], - prefix + "_" + wordify(choice), + f"{prefix}_{wordify(choice)}", ) + sub_subparsers.extend(new_subparsers) sub_option_strings.extend(new_option_strings) sub_compgens.extend(new_compgens) @@ -530,7 +531,7 @@ def recurse(parser, prefix, paths=None): format_positional(opt) for opt in subparser._get_positional_actions() if not isinstance(opt.choices, dict) if opt.help != SUPPRESS) - new_pref = prefix + "_" + wordify(cmd) + new_pref = f"{prefix}_{wordify(cmd)}" options = all_commands[new_pref] = { "cmd": cmd, "help": (subparser.description or "").strip().split("\n")[0], "arguments": arguments, "paths": [*paths, cmd]}