I've been trying to add bash completion support to a command line program I've been using lately, but it seems that I've hit a wall.
Here are the commands and subcommands I'd like to auto-complete
Main command is
foo, autocompletion should be performed for the subcommandsversion,process, andhelp. Further autcompletion depends on the subcommand.If the user enters
-after the main command, autocompletion performs completion for these word:--activateor--deactivateIf the user enters
--deactivate, it should perform autocompletion of a bash command output (let's sayls).
version: no autocompletion performedprocess: autocompletion defaults to listing directories (current and parent included)- If the user enters
-, autocompletion performs completion for these words:--color,--verboseand then stops afterward
- If the user enters
If the user enters
help, autocompletion is for the other subcommands (processorversion).
Here's my current implementation of the autocomplete script (which is failing badly):
_foo() {
local cur prev opts
COMPREPLY=()
cur="${COMP_WORDS[COMP_CWORD]}"
prev="${COMP_WORDS[COMP_CWORD-1]}"
words=("${COMP_WORDS[@]}")
cword=$COMP_WORD
opts="process version help"
case "$prev" in
-*)
local subopts="--activate --deactivate"
COMPREPLY=( $(compgen -W "${subopts}" -- ${cur}) )
case "$cur" in
--deactivate)
COMPREPLY=( $(ls) )
;;
esac
;;
version)
COMPREPLY=()
;;
process)
case "$cur" in
-*)
local subopts="--color --verbose"
COMPREPLY=( $(compgen -W "${subopts}" -- ${cur}) )
;;
*)
COMPREPLY=( $(compgen -A directory))
;;
esac
;;
help)
COMPREPLY=( $(compgen -W "process version" -- ${cur}) )
;;
esac
}
complete -F _foo foo
You can probably see that Bash is not my main forte as well. I was thinking of writing separate bash functions for each subcommands, but I don't know how to do it at the moment. If you have suggestions on how to implement this as well, I would really appreciate it.
Thanks!