1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
|
#!/bin/bash completion for st-status
_in_array() {
local x
local needle=$1
declare -a ARRAY="$2"
[ -z "${ARRAY}" ] && return 1
[ -z "${needle}" ] && return 1
for x in ${ARRAY[@]}; do
[ "${x}" == "${needle}" ] && return 0
done
return 1
}
_in_asso() {
local x
local needle=$1
local -n ARRAY=$2
[ "${#ARRAY[@]}" -eq "0" ] && return 1
[ -z "${needle}" ] && return 1
for x in "${!ARRAY[@]}"; do
[ "${x}" == "${needle}" ] && return 0
done
return 1
}
_st_status() {
local cur prev
local -A ARGS=( [--disk-fmin]=10 [--disk-fmax]=90)
COMPREPLY=()
cur="${COMP_WORDS[COMP_CWORD]}"
prev="${COMP_WORDS[COMP_CWORD-1]}"
OPTS="--all --json --cluster-status --cluster-reason --cluster-ntpdrift --cluster-validate --disk-unmounted --disk-usage --disk-filling --disk-deviation --base2"
case "${COMP_WORDS[1]}" in
"--all"|"-a"|"--json"|"-j")
if [ "${COMP_CWORD}" -le "2" ]; then
COMPREPLY=( $(compgen -W "--base2" -- ${cur}) )
fi
return 0
;;
*)
if _in_asso "${prev}" ARGS; then
# if previous complete is an arguments, complete with default value
COMPREPLY=($(compgen -W "${ARGS[$prev]}" -- "${cur}"))
else
for in_use in ${COMP_WORDS[@]}; do
if _in_array "$in_use" "${OPTS[@]}"; then
# Delete All and json option from the array OPTS
OPTS=("${OPTS/'--all'}")
OPTS=("${OPTS/'--json'}")
# Delete all previous complete options from the array OPTS
OPTS=("${OPTS/$in_use}")
fi
if _in_asso "$in_use" ARGS; then
# Delete all previous complete arguments from the associative array ARGS
unset ARGS[$in_use]
fi
done
compl="${OPTS} ${!ARGS[@]}"
COMPREPLY=($(compgen -W "${compl}" -- "${cur}"))
fi
return 0
;;
esac
}
complete -F _st_status st-status
|