How do I autocomplete nested, multi-level subcommands? [duplicate]

拥有回忆 提交于 2019-12-04 23:51:56
Zero Piraeus

Here's an example script for your two-level case (with a section for subcommands of show to show how it's done - you can just delete those three lines if they're not relevant to your case):

_foo()
{
    local cur prev

    cur=${COMP_WORDS[COMP_CWORD]}
    prev=${COMP_WORDS[COMP_CWORD-1]}

    case ${COMP_CWORD} in
        1)
            COMPREPLY=($(compgen -W "configure show" -- ${cur}))
            ;;
        2)
            case ${prev} in
                configure)
                    COMPREPLY=($(compgen -W "CM DSP NPU" -- ${cur}))
                    ;;
                show)
                    COMPREPLY=($(compgen -W "some other args" -- ${cur}))
                    ;;
            esac
            ;;
        *)
            COMPREPLY=()
            ;;
    esac
}

complete -F _foo foo

Hopefully it's fairly obvious from that example how you'd extend it to three-level commands etc., as well.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!