I am trying to write a Bash completion script for commands that can take long options on the form --option
or --param=value
. If the user has already entered an option on the command line, that option should be excluded from the completion list (assuming it only makes sense to specify a given option once on the command line).
Here is a first try:
_myprog()
{
local cur="${COMP_WORDS[$COMP_CWORD]}"
local words=(--help --param1= --param-state --param2=)
_exclude_cmd_line_opts
COMPREPLY=( $(compgen -W "${words[*]}" -- "$cur") )
}
complete -F _myprog myprog
_exclude_cmd_line_opts() {
local len=$(($COMP_CWORD - 1))
local i
for i in "${COMP_WORDS[@]:1:$len}" ; do
[[ $i == --* ]] && words=( "${words[@]/$i}" )
done
}
If source this script source script.sh
and then write:
$ myprog --param1= <tab><tab>
I get the following completion list:
= --help --param2= --param-state
so it works almost except for that I get a spurious '='
sign in the completion list.. Any suggestions?