Skip to content

Commit ad3d4ce

Browse files
authored
Merge pull request #200 from ThomasWaldmann/doubled-rest-fix
zsh: prevent duplicate default REMAINDER spec in generated completions
2 parents acc76fd + 171be68 commit ad3d4ce

File tree

2 files changed

+16
-4
lines changed

2 files changed

+16
-4
lines changed

shtab/__init__.py

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -606,10 +606,17 @@ def command_case(prefix, options):
606606

607607
return f"""\
608608
{prefix}() {{
609-
local context state line curcontext="$curcontext" one_or_more='(*)' remainder='(-)*'
610-
611-
if ((${{{prefix}_options[(I)${{(q)one_or_more}}*]}} + ${{{prefix}_options[(I)${{(q)remainder}}*]}} == 0)); then # noqa: E501
612-
{prefix}_options+=(': :{prefix}_commands' '*::: :->{name}')
609+
local context state line \
610+
curcontext="$curcontext" one_or_more='(*)' remainder='(-)*' default='*::: :->{name}'
611+
612+
# Add default positional/remainder specs only if none exist, and only once per session
613+
if (( ! {prefix}_defaults_added )); then
614+
if (( ${{{prefix}_options[(I)${{(q)one_or_more}}*]}} +\
615+
${{{prefix}_options[(I)${{(q)remainder}}*]}} +\
616+
${{{prefix}_options[(I)${{(q)default}}]}} == 0 )); then
617+
{prefix}_options+=(': :{prefix}_commands' '*::: :->{name}')
618+
fi
619+
{prefix}_defaults_added=1
613620
fi
614621
_arguments -C -s ${prefix}_options
615622
@@ -631,6 +638,9 @@ def command_option(prefix, options):
631638
{prefix}_options=(
632639
{arguments}
633640
)
641+
642+
# guard to ensure default positional specs are added only once per session
643+
{prefix}_defaults_added=0
634644
"""
635645

636646
def command_list(prefix, options):

tests/test_shtab.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,8 @@ def test_prog_scripts(shell, caplog, capsys):
133133
elif shell == "zsh":
134134
assert script_py == [
135135
"#compdef script.py", "_describe 'script.py commands' _commands",
136+
'local context state line curcontext="$curcontext" '
137+
"one_or_more='(*)' remainder='(-)*' default='*::: :->script.py'",
136138
"_shtab_shtab_options+=(': :_shtab_shtab_commands' '*::: :->script.py')", "script.py)",
137139
"compdef _shtab_shtab -N script.py"]
138140
elif shell == "tcsh":

0 commit comments

Comments
 (0)