0

I wrote a CLI using bash script to enable the user the option of using TAB to auto complete commands. My issue is that I want to incorporate the use of flags with free input from the user, i.e.: the user may choose a command from a given list of commands using TAB,

run_my_prog

then add a flag which the user may choose from a given list of flags,

run_my_prog --ip

then enter free input,

run_my_prog --ip 10.10.0.8

and then use Tab to choose the next flag for the same command,

run_my_prog --ip 10.10.0.8 --port 443

I can't seem to under stand how to add the second flag after the user has entered an input not from the list of commands, in this example after he entered the ip, I can't add the "--port" flag.

here is basically what I did so far:

_cli_auto_complete(){

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

case ${COMP_CWORD} in
    1)
        COMPREPLY=($(compgen -W "run_my_prog" -- ${cur})) 
        ;;
    2)
        case ${prev} in # then comes the flag/flags
            run_my_prog)
                COMPREPLY=($(compgen -W "--ip" -- ${cur}))
                ;;
        esac
        ;;
    3)
        case ${prev} in 
            --ip)
                COMPREPLY=($(compgen -W "--port" -- ${cur}))
                ;;
        esac
        ;;
}
AHM
  • 15
  • 3
  • ok I got it, instead of using `${prev}` I can just refer to the index of a completion with `${COMP_WORDS[]}` – AHM Feb 08 '23 at 10:21

0 Answers0