-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbash_completion
302 lines (252 loc) · 8.93 KB
/
bash_completion
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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
#!/bin/bash
function _comp_contains() {
# Takes two strings containing space separated words and checks if one of the
# words in the second list matches one in the first.
# E.g.: `_comp_contains "test test2" "no nope test"` returns true.
#
# Parameters
# list_lookup (string) Space separated words to search in
# list_search (string) Space separated words to search
#
# Returns
# retval (int) Error / Success
#
# Side effects
# None
#
for word in $2; do
if [[ $1 =~ (^|[[:space:]])${word}($|[[:space:]]) ]]; then
return 0
fi
done;
return 1
}
function _comp_get_parameter() {
# Returns the n-th parameter from the command line (not counting options
# or the command name).
#
# Parameters
# number (int) Parameter to select
#
# Returns
# retval (int) Error / Success
#
# Side effects
# result is saved in $arg variable
#
local COUNT
COUNT=0
arg=
for var in ${COMP_LINE}; do
# exclude options and the toolname
if [[ "$var" == -* ]] || [[ "$var" == "${COMP_WORDS[0]}" ]]; then
continue;
fi
COUNT=$((COUNT+1))
if [[ $COUNT -eq $1 ]]; then
arg="$var"
return 0
fi
done
return 1
}
function _comp_short_to_long() {
# Takes a short option name and a a list that represents the current option list.
# The functions assumes that the long option for a certain short option directly
# follows the short option inside the argument list. The corresponding long
# option is then returned inside the $long variable.
#
# Parameters
# short (string) Name of the short option (with - prefix)
# opts (list) Space separated words (option list)
#
# Returns
# retval (int) Error / Success
#
# Side effects
# stores the corresponding long option inside the $long variable
#
local short ret
short=$1
shift
ret=0
long=
while (( "$#" )); do
if [[ "$1" == "$short" ]]; then
shift
if [[ $1 =~ ^--[^[:space:]]+$ ]]; then
long=$1
return 0
fi
return 1
fi
shift
done
return 1
}
function _comp_filter() {
# Takes the name of a variable that contains the current option list as a string.
# Iterates over the current command line and removes all options that are already
# present.
#
# A second string of space separated words can be passed
# that are excluded from filtering. This is useful, when certain options are allowed
# to appear multiple times.
#
# By default, the function will try to remove long options if their corresponding short
# options are used on the command line. To work correctly, this requires that each short
# option has a long option that directly follows the short option in the options list. If
# this assumption is incorrect, you should set a third parameter for _comp_filter to 'false'.
#
# Parameters
# opts (string) Space separated words to filter (call by ref)
# exclude (string) Space separated words to exclude
# remove_longs (string) Decides if short-to-long translation is attempted (true|false)
#
# Returns
# retval (int) Error / Success
#
# Side effects
# filtered option list is stored in first variable (passed by ref)
#
local Opts=$1 filter exclude cur long remove_longs
cur="${COMP_WORDS[COMP_CWORD]}"
filter=" ${!Opts} "
exclude=$2
remove_longs=$3
# iterate over each word inside the current command line
for var in ${COMP_LINE}; do
# exclude the current word to allow full specified options to be space completed
if [[ "$var" == "$cur" ]]; then
continue;
# exclude words from the exclusion list
elif _comp_contains "$exclude" $var; then
continue
# otherwise remove the word from the filter list
else
# if short option, remove long option
if [[ "$remove_longs" != "false" && $var =~ ^-[a-zA-Z0-9]+$ ]]; then
_comp_short_to_long $var $filter && \
filter=( "${filter//[[:space:]]${long}[[:space:]]/ }" )
fi
# remove actual option
filter=( "${filter//[[:space:]]${var}[[:space:]]/ }" )
fi
done
_upvars -v $Opts "$filter"
}
function _comp_filter_shorts() {
# Takes a string of space separated words and removes all short options from it.
# Completion of short options is a matter of taste. The maintainers of bash-completion
# do not recommend it. Instead, completions should only handle completion for arguments
# that are required by short options, but to not complete short options themselves.
#
# Parameters
# opts (string) Space separated option list (call by ref)
#
# Returns
# retval (int) Error / Success
#
# Side effects
# filtered option list is stored in first variable (passed by ref)
#
local Opts=$1 filter
filter=" ${!Opts} "
for word in $filter; do
if [[ $word =~ ^-[a-zA-Z0-9]+$ ]]; then
filter=( "${filter//[[:space:]]${word}[[:space:]]/ }" )
fi
done
_upvars -v $Opts "$filter"
}
function _comp_remove_list() {
# Takes the current completion list and a string of space separated words.
# Removes each word from the completion list that was specified in the string.
#
# Parameters
# opts (string) Space separated option list (call by ref)
# remove (string) Space separated words
#
# Returns
# retval (int) Error / Success
#
# Side effects
# filtered option list is stored in first variable (passed by ref)
#
local Opts=$1 filter remove
filter=" ${!Opts} "
remove=$2
for word in $remove; do
filter=( "${filter//[[:space:]]${word}[[:space:]]/ }" )
done
_upvars -v $Opts "$filter"
}
function _comp_to_space() {
# In some situations, the completion list may contains newlines instead of spaces
# (e.g. when using the _parse_help function for completion). The completion helpers
# expect the options to be separated by spaces to function correctly. This function
# takes a newline separated option list and replaces the newlines with spaces
#
# Edit: The completion helper functions should now also work for newline separated
# option lists. However, this function is kept, since it might be useful in certain
# situations.
#
# Parameters
# opts (string) Newline separated option list (call by ref)
#
# Returns
# retval (int) Error / Success
#
# Side effects
# transformed option list is stored in first variable (passed by ref)
#
local Opts=$1
filter=" ${!Opts} "
filter=( "${filter//$'\n'/ }" )
_upvars -v $Opts "$filter"
}
function _comp_list() {
# This function is used for list completion (comma separated words). If an option
# expects a comma separated list of arguments, you can call this function with the
# desired set of available list arguments. The function does already populate the
# COMPREPLY variable and you normally want to return from the completion script
# after calling it.
#
# Additionally to the array of available options, it is possible to specify an
# integer as second argument that specifies the maximum ammount of selectable
# options. After this amount of options was specified, space completion is enabled.
#
# Parameters
# options (string) Space separated list of available list options
# max (int) Maximum number of list items
#
# Returns
# retval (int) Error / Success
#
local cur prev prev_arr options=" $1 " count=1
compopt -o nospace
cur="${COMP_WORDS[COMP_CWORD]}"
if [[ "$cur" != ?*,* ]]; then
mapfile -t COMPREPLY < <(compgen -W "${options}" -- "${cur}")
return 0
else
prev="${cur%,*}"
cur="${cur##*,}"
prev_arr=${prev//,/ }
for word in $prev_arr; do
count=$((count+1))
if _comp_contains "$options" $word; then
options=( "${options//[[:space:]]${word}[[:space:]]/ }" )
continue
fi
done
if [[ $count -ge $2 ]]; then
compopt +o nospace
fi
mapfile -t COMPREPLY < <(compgen -P "$prev," -W "${options}" -- "${cur}")
return 0
fi
}
for bcfile in ~/.bash_completion.d/* ; do
[ -f "$bcfile" ] && . $bcfile
done