From 1f9af575eb6fe24d91231e904625b7adee5ce7ee Mon Sep 17 00:00:00 2001 From: Casper da Costa-Luis Date: Fri, 17 Jun 2022 13:58:10 +0100 Subject: [PATCH] fix #74 bash completion with space - vis. https://stackoverflow.com/questions/1146098/properly-handling-spaces-and-quotes-in-bash-completion --- shtab/__init__.py | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/shtab/__init__.py b/shtab/__init__.py index 5467cfd..96aa7cd 100644 --- a/shtab/__init__.py +++ b/shtab/__init__.py @@ -225,8 +225,8 @@ def recurse(parser, prefix): this_positional_choices.append(str(choice)) if this_positional_choices: - choices.append(u"{}_pos_{}_choices='{}'".format( - prefix, i, " ".join(this_positional_choices))) + choices.append(u"{}_pos_{}_choices=('{}')".format( + prefix, i, "' '".join(this_positional_choices))) # skip default `nargs` values if positional.nargs not in (None, "1", "?"): @@ -266,8 +266,8 @@ def recurse(parser, prefix): this_optional_choices.append(str(choice)) if this_optional_choices: - choices.append(u"{}_{}_choices='{}'".format( - prefix, wordify(option_string), " ".join(this_optional_choices))) + choices.append(u"{}_{}_choices=('{}')".format( + prefix, wordify(option_string), "' '".join(this_optional_choices))) # Check for nargs. if optional.nargs is not None and optional.nargs != 1: @@ -421,8 +421,9 @@ def complete_bash(parser, root_prefix=None, preamble="", choice_functions=None): COMPREPLY=( $(compgen -W "${current_option_strings[*]}" -- "${completing_word}") ) else # use choices & compgen - COMPREPLY=( $(compgen -W "${current_action_choices}" -- "${completing_word}"; \\ - [ -n "${current_action_compgen}" ] \\ + local IFS=$'\n' + COMPREPLY=( $(compgen -W "${current_action_choices}" -- "${completing_word}") \\ + $([ -n "${current_action_compgen}" ] \\ && "${current_action_compgen}" "${completing_word}") ) fi