summaryrefslogtreecommitdiffstats
path: root/python/mach/mach/commands/completion_templates/bash.template
blob: 53723087024e3470b9d877a7dae85ca2cf84ebc9 (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
56
57
58
59
60
61
62
_mach_complete()
{
    local com coms comsubs cur opts script sub subs
    COMPREPLY=()
    declare -A comsubs=( %(commands_subcommands)s )

    _get_comp_words_by_ref -n : cur words
    # for an alias, get the real script behind it
    if [[ $(type -t ${words[0]}) == "alias" ]]; then
        script=$(alias ${words[0]} | sed -E "s/alias ${words[0]}='(.*)'/\\1/")
    else
        script=${words[0]}
    fi
    # lookup for command and subcommand
    for word in ${words[@]:1}; do
        if [[ $word == -* ]]; then
            continue
        fi

        if [[ -z $com ]]; then
            com=$word
        elif [[ "${comsubs[$com]}" == *" $word "* ]]; then
            sub=$word
            break
        fi
    done
    # completing for an option
    if [[ ${cur} == -* ]] ; then
        if [[ -n $com ]]; then
            if [[ -n $sub ]]; then
                optkey="$com $sub"
            else
                optkey="$com"
            fi
            case $optkey in
%(case_options)s
            esac
        else
            # no command, complete global options
            opts="%(globalopts)s"
        fi
        COMPREPLY=($(compgen -W "${opts}" -- ${cur}))
        __ltrim_colon_completions "$cur"
        return 0;
    # completing for a command
    elif [[ $cur == $com ]]; then
        coms="%(commands)s"
        COMPREPLY=($(compgen -W "${coms}" -- ${cur}))
        __ltrim_colon_completions "$cur"
        return 0
    else
        if [[ -z $sub ]]; then
            case "$com" in
%(case_subcommands)s
            esac
            COMPREPLY=($(compgen -W "${subs}" -- ${cur}))
            __ltrim_colon_completions "$cur"
        fi
        return 0
    fi
}
complete -o default -F _mach_complete mach