Skip to content
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
18 changes: 14 additions & 4 deletions shtab/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -606,10 +606,17 @@ def command_case(prefix, options):

return f"""\
{prefix}() {{
local context state line curcontext="$curcontext" one_or_more='(*)' remainder='(-)*'

if ((${{{prefix}_options[(I)${{(q)one_or_more}}*]}} + ${{{prefix}_options[(I)${{(q)remainder}}*]}} == 0)); then # noqa: E501
{prefix}_options+=(': :{prefix}_commands' '*::: :->{name}')
local context state line \
curcontext="$curcontext" one_or_more='(*)' remainder='(-)*' default='*::: :->{name}'

# Add default positional/remainder specs only if none exist, and only once per session
if (( ! {prefix}_defaults_added )); then
if (( ${{{prefix}_options[(I)${{(q)one_or_more}}*]}} +\
${{{prefix}_options[(I)${{(q)remainder}}*]}} +\
${{{prefix}_options[(I)${{(q)default}}]}} == 0 )); then
{prefix}_options+=(': :{prefix}_commands' '*::: :->{name}')
fi
{prefix}_defaults_added=1
fi
_arguments -C -s ${prefix}_options

Expand All @@ -631,6 +638,9 @@ def command_option(prefix, options):
{prefix}_options=(
{arguments}
)

# guard to ensure default positional specs are added only once per session
{prefix}_defaults_added=0
"""

def command_list(prefix, options):
Expand Down
2 changes: 2 additions & 0 deletions tests/test_shtab.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,8 @@ def test_prog_scripts(shell, caplog, capsys):
elif shell == "zsh":
assert script_py == [
"#compdef script.py", "_describe 'script.py commands' _commands",
'local context state line curcontext="$curcontext" '
"one_or_more='(*)' remainder='(-)*' default='*::: :->script.py'",
"_shtab_shtab_options+=(': :_shtab_shtab_commands' '*::: :->script.py')", "script.py)",
"compdef _shtab_shtab -N script.py"]
elif shell == "tcsh":
Expand Down