# hostinfo completion _get_first_hostinfo_arg() { local i arg= for (( i=1; i < COMP_CWORD; i++ )); do if [[ "${COMP_WORDS[i]}" != -* ]]; then arg=${COMP_WORDS[i]} break fi [[ "${COMP_WORDS[i]}" == "--path" || "${COMP_WORDS[i]}" == "-p" ]] && i=$((i+1)) done } _get_hostinfo_path() { local i path= for (( i=1; i < COMP_CWORD; i++ )); do if [[ "${COMP_WORDS[i]}" == "--path" || "${COMP_WORDS[i]}" == "-p" ]]; then path="--path ${COMP_WORDS[i+1]}" break fi done } _hostinfo() { local cur prev COMPREPLY=() _get_comp_words_by_ref cur prev if [[ "$prev" == "-p" || "$prev" == "--path" ]]; then COMPREPLY=( $( compgen -d -o dirnames -S "/" $cur ) ) compopt -o nospace else _get_first_hostinfo_arg _get_hostinfo_path if [[ -z "$arg" ]]; then _completion="$(hostinfo $path --hosts --short 2>/dev/null)" else _completion="$(hostinfo $path --keys $arg 2>/dev/null)" fi COMPREPLY=( $( compgen -W "-o --oneline -f --file -k --keys -h -? --help -p --path -n --nospaces -l --hosts -s --short -v --verbose --only-names -d --details -V --version $_completion" -- $cur ) ) fi } complete -F _hostinfo hostinfo