2017-11-02 16:41:53 +03:00
#
2021-04-19 19:40:14 +03:00
# @command@ completion support
2017-11-02 16:41:53 +03:00
#
2021-04-19 19:40:14 +03:00
_@command@_complete()
2017-11-02 16:41:53 +03:00
{
local words cword c=0 i=0 cur RO URI CMDLINE INPUT A
# Here, $COMP_WORDS is an array of words on the bash
# command line that user wants to complete. However, when
# parsing command line, the default set of word breaks is
# applied. This doesn't work for us as it mangles libvirt
# arguments, e.g. connection URI (with the default set it's
# split into multiple items within the array). Fortunately,
# there's a fixup function for the array.
_get_comp_words_by_ref -n "\"'><=;|&(:" -w words -i cword
COMP_WORDS=( "${words[@]}" )
COMP_CWORD=${cword}
cur=${COMP_WORDS[$COMP_CWORD]}
# See what URI is user trying to connect to and if they are
# connecting RO. Honour that.
while [ $c -le $COMP_CWORD ]; do
2021-11-14 09:06:40 +03:00
local word="${COMP_WORDS[c]}"
2017-11-02 16:41:53 +03:00
case "$word" in
-r|--readonly) RO=1 ;;
-c|--connect) c=$((++c)); URI=${COMP_WORDS[c]} ;;
*) if [ $c -ne 0 ] && [ $i -eq 0 ]; then i=$c; break; fi ;;
esac
c=$((++c))
done
2024-05-23 16:41:16 +03:00
CMDLINE=( "-q" )
2017-11-02 16:41:53 +03:00
if [ -n "${RO}" ]; then
2021-04-20 16:03:16 +03:00
CMDLINE+=("-r")
2017-11-02 16:41:53 +03:00
fi
if [ -n "${URI}" ]; then
2021-04-20 16:03:16 +03:00
CMDLINE+=("-c" "${URI}")
2017-11-02 16:41:53 +03:00
fi
INPUT=( "${COMP_WORDS[@]:$i:$COMP_CWORD}" )
2020-11-10 12:50:59 +03:00
INPUT[-1]=${INPUT[-1]//\\:/:}
2017-11-02 16:41:53 +03:00
# Uncomment these lines for easy debug.
# echo;
# echo "RO=${flag_ro}";
# echo "URI=${URI}";
# echo "CMDLINE=${CMDLINE}";
# echo "INPUT[${#INPUT[@]}]=**${INPUT[@]}**";
# echo "cur=${cur}";
# echo;
# return 0;
# Small shortcut here. According to manpage:
# When the function is executed, the first argument ($1) is
# the name of the command whose arguments are being
# completed.
# Therefore, we might just run $1.
2021-11-14 09:06:40 +03:00
local IFS=$'\n'
A=($($1 ${CMDLINE[@]} complete -- "${INPUT[@]}" 2>/dev/null))
2017-11-02 16:41:53 +03:00
COMPREPLY=($(compgen -W "${A[*]%--}" -- ${cur}))
__ltrim_colon_completions "$cur"
return 0
} &&
2021-04-19 19:40:14 +03:00
complete -o default -o filenames -F _@command@_complete @command@
2017-11-02 16:41:53 +03:00
# vim: ft=sh:et:ts=4:sw=4:tw=80