summaryrefslogtreecommitdiffstats
path: root/contrib/bash-completion
blob: 5315d836041098c48527fe51d0f5b367633a1ee9 (plain)
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
# 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 $_completion" -- $cur ) )
    fi
}
complete -F _hostinfo hostinfo