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 comma
Your first return 0
prevents the second if
from being evaluated. Try using a case
statement ordered from most-specific to least-specific or at least order your if
statements that way.
Since "=" is included in $COMP_WORDBREAKS
, ${prev}
is "--compose_filter" without the "=".
If you remove "=" from $COMP_WORDBREAKS
then --compose_filter
is still ${cur}
rather than ${prev}
. Doing so without preserving and restoring its value will break other completions.
I'm not sure what other problems there may be.
You can pepper your function with echo
statements that are redirected to another terminal to aid in debugging. For example:
echo "cur: $cur, prev: $prev" > /dev/pts/2
this will help some people i think. The below code should work (wrote on the fly)
have fstcompose &&{
function elementExists(){
local i isRunning result
i=0
isRunning=1
result=0
#~ for i in ${opts_with_equal[@]}; do
while [ "$isRunning" -eq 1 ]; do
if [ "$i" -ge "${#opts_with_equal[@]}" ]; then
isRunning=0
elif [ "${opts_with_equal[$i]}" == "$1" ]; then
result=1
isRunning=0
fi
((i++))
done
echo $result
}
function _fstcompose(){
local prev cur opts opts_with_equal isEqualOptions
COMPREPLY=()
_get_comp_words_by_ref -n : cur prev
opts="--compose_filter --connect"
opts_with_equal=(--compose_filter)
case ${cur} in
'--compose_filter'*)
prev="${cur%%=*}="
cur=${cur#*=}
COMPREPLY=( ${prev}$(compgen -W "alt_sequence auto match sequence" -- "${cur}") )
return 0
;;
esac
if [[ "${cur}" != -* ]]; then
_filedir
else
isEqualOptions=$(elementExists "${cur}")
if [ "${isEqualOptions:-0}" -eq 1 ]; then
COMPREPLY=( $(compgen -W "${opts}" -S '=' -- "${cur}") )
else
COMPREPLY=( $(compgen -W "${opts}" -- "${cur}") )
fi
fi
}
complete -o nospace -F _fstcompose fstcompose
}
# Local variables:
# mode: shell-script
# sh-basic-offset: 4
# sh-indent-comment: t
# indent-tabs-mode: nil
# End:
# ex: ts=4 sw=4 et filetype=sh
Note: opts_with_equal it is an array in this example it contain only one parameter. Put into each parameter who use '='