Create a file in:
/etc/bash_completion.d/foo
With the following content:
_foo()
{
local cur prev opts
COMPREPLY=()
cur="${COMP_WORDS[COMP_CWORD]}"
prev="${COMP_WORDS[COMP_CWORD-1]}"
opts="--help --verbose --version"
if [[ ${cur} == -* ]] ; then
COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) )
return 0
fi
}
complete -F _foo fooCOMP_WORDSis an array containing all individual words in the current command line.COMP_CWORDis an index of the word containing the current cursor position.COMPREPLYis an array variable from which Bash reads the possible completions.
And the compgen command returns the array of elements from --help, --verbose and --version
matching the current word "${cur}":
compgen -W "--help --verbose --version" -- "<userinput>"
See orignal post, source
Have look at /etc/bash_completion and /etc/bash_completion.d/* for some examples.
See GNU's manual