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

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


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.