Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix bash completion with space #81

Merged
merged 3 commits into from
Jun 17, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ default_language_version:
python: python3
repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.1.0
rev: v4.3.0
hooks:
- id: check-added-large-files
- id: check-case-conflict
Expand Down Expand Up @@ -39,7 +39,7 @@ repos:
- flake8-string-format
- flake8-type-annotations
- repo: https://github.com/pre-commit/mirrors-mypy
rev: v0.930
rev: v0.961
hooks:
- id: mypy
- repo: https://github.com/google/yapf
Expand Down
13 changes: 7 additions & 6 deletions shtab/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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", "?"):
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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

Expand Down
12 changes: 6 additions & 6 deletions tests/test_shtab.py
Original file line number Diff line number Diff line change
Expand Up @@ -197,11 +197,11 @@ def test_subparser_aliases(shell, caplog):
if shell == "bash":
shell = Bash(completion)
shell.compgen('-W "${_shtab_test_subparsers[*]}"', "s", "sub")
shell.compgen('-W "$_shtab_test_pos_0_choices"', "s", "sub")
shell.compgen('-W "${_shtab_test_pos_0_choices[*]}"', "s", "sub")
shell.compgen('-W "${_shtab_test_subparsers[*]}"', "x", "xsub")
shell.compgen('-W "$_shtab_test_pos_0_choices"', "x", "xsub")
shell.compgen('-W "${_shtab_test_pos_0_choices[*]}"', "x", "xsub")
shell.compgen('-W "${_shtab_test_subparsers[*]}"', "y", "ysub")
shell.compgen('-W "$_shtab_test_pos_0_choices"', "y", "ysub")
shell.compgen('-W "${_shtab_test_pos_0_choices[*]}"', "y", "ysub")
shell.test('"$($_shtab_test_sub_pos_0_COMPGEN o)" = "one"')
shell.test('-z "$_shtab_test_COMPGEN"')

Expand Down Expand Up @@ -245,9 +245,9 @@ def test_add_argument_to_positional(shell, caplog, capsys):
if shell == "bash":
shell = Bash(completion)
shell.compgen('-W "${_shtab_test_subparsers[*]}"', "c", "completion")
shell.compgen('-W "$_shtab_test_pos_0_choices"', "c", "completion")
shell.compgen('-W "$_shtab_test_completion_pos_0_choices"', "ba", "bash")
shell.compgen('-W "$_shtab_test_completion_pos_0_choices"', "z", "zsh")
shell.compgen('-W "${_shtab_test_pos_0_choices[*]}"', "c", "completion")
shell.compgen('-W "${_shtab_test_completion_pos_0_choices[*]}"', "ba", "bash")
shell.compgen('-W "${_shtab_test_completion_pos_0_choices[*]}"', "z", "zsh")

assert not caplog.record_tuples

Expand Down