具有嵌套控制功能的 zsh 补全

zsh completion with nested control functions

我正在为 zsh 编写完成函数。我把 cargo completion function as a basis for mine. For the most part it works fine except for -h and --help options. Completion ouput for these functions is unaligned and repeated multiple times (presented below). This only happens when control functions_describe_arguments 等)都存在于 case 结构中。 为什么会发生这种情况,我该如何解决这个问题?

完成函数:

#compdef test
_test() {
    local context state state_descr line
    typeset -A opt_args
    _arguments \
        "(- 1 *)"{-h,--help}"[Help]" \
        "1: :->command" \
        "*:: :->args"

    case $state in
        command)
            _alternative 'arguments:custom arg:(a b c)'
            ;;
        args)
            _arguments \
              "-a[All]" \
              "-n[None]"
    esac
}
_test

Shell 输出:

> test -[TAB]
--help
-h
-- Help
--help
-h
-- Help
--help
-h
-- Help

我刚遇到同样的问题。解决方案是从完成函数中将 return 归零:

#compdef test

_test() {
    ...

    return 0
}

_test

我见过的所有完成脚本都使用 ret 变量,该变量最初是 1,然后如果任何完成函数成功则设置为 0

#compdef test
_test() {
    local context state state_descr line
    local ret=1

    typeset -A opt_args
    _arguments \
        "(- 1 *)"{-h,--help}"[Help]" \
        "1: :->command" \
        "*:: :->args" && ret=0

    case $state in
        command)
            _alternative 'arguments:custom arg:(a b c)' && ret=0
            ;;
        args)
            _arguments \
              "-a[All]" \
              "-n[None]" && ret=0
    esac

    return ret
}

_test

虽然我不确定他们为什么这样做。