summaryrefslogtreecommitdiffstats
path: root/completions/carton
blob: 7f99c90307b664692d24ad11ce9f5919cc976247 (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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# carton(3pm) completion                                   -*- shell-script -*-

_comp_cmd_carton__commands()
{
    local cmds=$("$1" usage 2>&1 |
        command sed -ne '/.*command.* is one of/{n;p;q;}')
    _comp_compgen -aF $' \t\n,' -- -W "$cmds"
}

_comp_cmd_carton__command_help()
{
    local help=$(PERLDOC_PAGER=cat PERLDOC=-otext "$1" -h "$2" 2>&1)
    _comp_compgen -a -- -W '$help'
}

_comp_cmd_carton()
{
    local cur prev words cword was_split comp_args
    _comp_initialize -s -- "$@" || return

    local i command="" has_command=""
    for ((i = 1; i < cword; i++)); do
        case ${words[i]} in
            -*) ;;
            *)
                command=${words[i]}
                has_command=set
                break
                ;;
        esac
    done

    if [[ ! $has_command ]]; then
        _comp_cmd_carton__commands "$1"
        return
    fi

    case $prev in
        --version | --help | -[vh])
            return
            ;;
        --cpanfile)
            if [[ $command == install ]]; then
                _comp_compgen_filedir
                return
            fi
            ;;
        --path)
            if [[ $command == install ]]; then
                _comp_compgen_filedir -d
                return
            fi
            ;;
        --without)
            if [[ $command == install ]]; then
                local phases="configure build test runtime develop"
                _comp_compgen -a -- -W '$phases'
                return
            fi
            ;;
    esac

    [[ $was_split ]] && return

    if [[ $cur == -* ]]; then
        [[ $command == @(help|usage) ]] || COMPREPLY=(--help)
        _comp_cmd_carton__command_help "$1" "$command"
    fi

    case $command in
        exec)
            # skip all the options --, -v, etc. and identify the command name
            # position.
            for ((i++; i < cword; i++)); do
                case ${words[i]} in
                    --)
                        ((i++))
                        break
                        ;;
                    -*) ;;
                    *) break ;;
                esac
            done

            _comp_command_offset "$i"
            return
            ;;
        show | update)
            : # TODO modules completion
            ;;
    esac
} &&
    complete -F _comp_cmd_carton carton

# ex: filetype=sh