I'm trying to construct a bash completion routine that will suggest command lines flags and suitable flag values. For example in the below fstcompose command I would like the competition routine to first suggest the compose_filter= flag, and then suggest possible values from [alt_sequence, auto, match, sequence].
fstcompose --compose_filter=
For any flags that do not have a set of associated of values I want the competition to fall back to the default mode of suggesting paths or files.
The one issue I'm facing is the = equal sign treated as an individual token and is set as the prev COMP_WORD. Is there a technique for detecting the whole flag before and including the previous = character ? Or is there a better way of implementing this type completion of flag with enumerable values? Below is a sample of the completion routine I'm working with.
_fstcompose()
{
local cur prev opts filters pprev
COMPREPLY=()
cur="${COMP_WORDS[COMP_CWORD]}"
prev="${COMP_WORDS[COMP_CWORD-1]}"
opts="--compose_filter= --connect"
filters="alt_sequence auto match sequence"
if [[ ${cur} == -* ]] ; then
COMPREPLY=($(compgen -W "${opts}" -- ${cur}))
return 0
fi
if [[ ${prev} == "--compose_filter=" ]] ; then
COMPREPLY=($(compgen -W "${filters}" -- ${cur}))
return 0
fi
_filedir
}
complete -o nospace -F _fstcompose fstcompose
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…