aboutsummaryrefslogtreecommitdiff
path: root/bash-completion/cave
blob: a107b57379cc8cac65d1f60217bea5da5f577960 (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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
# Bash completion function for cave
# Written by Mike Kelly
# vim: set et sw=4 sts=4 ts=4 ft=sh :

# Based in part upon 'git' bash completion from git-1.6.4.2
# Based in part upon a cave bash completion script by Ɓukasz Michalik

__cave_find_cmd() {
    local w c cmd

    for (( c=1; c < COMP_CWORD; c++ )) ; do
        w="${COMP_WORDS[c]}"
        case "${w}" in
            --*) ;;
            --help|-h) cmd="help"; break ;;
            config|@(display|execute)-resolution|help|import|perform|print-@(categories|commands|environment-metadata|id-@(contents|executables|metadata)|ids|owners|packages|repositor@(ies|y-formats)|sets|sync-protocols)|resolve|show|sync|update-world) cmd="${w}"; break ;;
        esac
    done

    echo "${cmd}"
}

__cave_enum_value() {
    local w c v enum="${1}"
    for (( c=COMP_CWORD; c > 1; c-- )) ; do
        w="${COMP_WORDS[c]}"
        if [[ "${w}" =~ ^${enum}$ ]] ; then
            v="${COMP_WORDS[c+1]}"
            break;
        fi
    done

    echo "${v}"
}

__cave_compgen() {
    COMPREPLY=( $(compgen -W "$*" -- "${cur}") )
}

__cave_global() {
    case "${prev}" in
        --log-level)
            __cave_compgen "debug qa warning silent"
            ;;
        --environment|-E)
            __cave_compgen "paludis: portage:"
            ;;
        *)
            __cave_compgen "$(cave print-commands -a) -E --environment --log-level"
            ;;
    esac
}

__cave_help() {
    # only complete once
    if [[ "${prev}" == "help" ]] ; then
        __cave_compgen "$(cave print-commands -a) -a --all -h --help"
    fi
}

__cave_sync() {
    __cave_compgen "$(cave print-repositories) --sequential -h --help"
}

__cave_show() {
    local type="$(__cave_enum_value '--type|-t')"
    case "${prev}" in
        --type|-t)
            __cave_compgen "auto repository set wildcard package"
            ;;
        *)
            case "${cur}" in
                -*)
                    __cave_compgen "--type -t --complex-keys -c --internal-keys -i --flat -f --raw-names -r --help"
                    ;;
                *)
                    case "${type}" in
                        repository)
                            __cave_compgen "$(cave print-repositories)"
                            ;;
                        set)
                            __cave_compgen "$(cave print-sets)"
                            ;;
                        package)
                            __cave_compgen "$(cave print-packages)"
                            ;;
                        auto|wildcard|*)
                            # TODO: provide reasonable completions for 'spec' here
                            COMPREPLY=()
                            ;;
                    esac
                    ;;
            esac
            ;;
    esac
}

__cave_resolve_options="-h --help
        -x --execute --no-execute
        -z --lazy --no-lazy
        -c --complete --no-complete
        -e --everything --no-everything
        -U --permit-uninstall
        -d --permit-downgrade
        -o --permit-old-version
        -K --keep-targets
        -k --keep
        -R --reinstall-scm
        -w --with
        -W --without
        -S --target-slots
        -s --slots
        -D --follow-installed-build-dependencies --no-follow-installed-build-dependencies
        -n --no-follow-installed-dependencies --no-no-follow-installed-dependencies
        -0 --no-dependencies-from
        -! --no-blockers-from
        --suggestions
        --recommendations
        -t --take
        -T --take-from
        -i --ignore
        -I --ignore-from
        -F --favour
        -A --avoid
        -p --preset
        -b --create-binaries
        -B --no-binaries-for
        -/ --install-to-root --no-install-to-root
        -2 --chroot-path
        --dump --no-dump
        --dump-restarts --no-dump-restarts
        -1 --preserve-world --no-preserve-world
        -C --continue-on-failure
        --skip-phase
        --abort-at-phase
        --skip-until-phase
        --change-phases-for
        --show-option-descriptions
        --show-descriptions
        -X --explain
        --show-all-jobs --no-show-all-jobs
        --display-resolution-program
        --execute-resolution-program
        --perform-program
        --update-world-program"

__cave_resolve() {
    case "${prev}" in
        -K|--keep-targets)
            __cave_compgen "auto never if-transient if-same if-same-version if-possible"
            ;;
        -k|--keep)
            __cave_compgen "never if-transient if-same if-same-version if-possible"
            ;;
        -R|--reinstall-scm)
            __cave_compgen "always daily weekly never"
            ;;
        -w|--with|-W|--without)
            __cave_compgen "$(cave print-packages)"
            ;;
        -S|--target-slots|-s|--slots)
            __cave_compgen "best-or-installed installed-or-best all best"
            ;;
        --suggestions|--recommendations)
            __cave_compgen "ignore display take"
            ;;
        -C|--continue-on-failure)
            __cave_compgen "never if-satisfied if-independent always"
            ;;
        --change-phases-for)
            __cave_compgen "all first last"
            ;;
        --show-option-descriptions)
            __cave_compgen "none new changed all"
            ;;
        --show-descriptions)
            __cave_compgen "none new all"
            ;;
        --skip-phase|--abort-at-phase|--skip-until-phase)
            __cave_compgen "fetch_extra killold init setup unpack prepare configure compile test test_expensive install strip preinst merge prerm postrm postinst tidyup"
            ;;
        -[UdawW0!tTiIFApBX]|--@(permit-@(uninstall|downgrade|any-version)|with?(out)|no-@(dependencies|blockers)-from|@(take|ignore)?(-from)|favor|avoid|preset|no-binaries-for|explain))
            # TODO: better completion here
            COMPREPLY=()
            ;;
        --@(@(display|execute)-resolution|perform|update-world)-program)
            _filedir
            ;;
        *)
            case "${cur}" in
                -*)
                    __cave_compgen "${__cave_resolve_options}"
                    ;;
                *)
                    __cave_compgen "${__cave_resolve_options}
                        $(cave print-packages)
                        $(cave print-sets)"
                    ;;
            esac
            ;;
    esac
}

__cave_import_options="${__cave_resolve_options}
    -l --location
    -u --install-under
    -r --rewrite-uids-over-to-root
    -D --description
    -B --build-dependency
    -R --run-dependency
    -P --preserve-metadata"

__cave_import() {
    case "${prev}" in
        -l|--location|-u|--install-under)
            _filedir
            ;;
        -r|--rewrite-uids-over-to-root)
            _uids
            ;;
        -D|--description)
            COMPREPLY=()
            ;;
        -B|--build-dependency|-R|--run-dependency|-P|--preserve-metadata)
            __cave_compgen "$(cave print-packages)"
            ;;
        -*)
            # all the other options are the same as with __cave_resolve
            __cave_resolve
            ;;
        *)
            case "${cur}" in
                -*)
                    __cave_compgen "${__cave_import_options}"
                    ;;
                *)
                    # TODO: make this more correct (only one package
                    # name, then version, then slot)
                    __cave_compgen "${__cave_import_options}
                        $(cave print-packages)"
                    ;;
            esac
            ;;
    esac
}

_cave() {
    local cmd=`__cave_find_cmd` prev=`_get_pword 2>/dev/null || echo ${COMP_WORDS[COMP_CWORD-1]}` cur=`_get_cword`

    COMPREPLY=()

    if [[ -z "${cmd}" ]] ; then
        __cave_global
    else
        if [[ "$(type -t "__cave_${cmd}")" == "function" ]] ; then
            "__cave_${cmd}"
        fi
    fi
}
complete -F _cave cave