@@ -48,6 +48,54 @@ _multipass_complete()
48
48
opts=$( \eval $cmd | \grep -Ev '(\+--|Name)' | \cut -d',' -f 1 )
49
49
}
50
50
51
+ _multipass_instances_with_colon()
52
+ {
53
+ local opts_bak="${opts}"
54
+ opts=""
55
+
56
+ _multipass_instances
57
+ local instances="${opts}"
58
+ opts="$opts_bak"
59
+
60
+ for instance in ${instances}; do
61
+ opts="${opts} ${instance}:"
62
+ done
63
+ }
64
+
65
+ # When we are completing the `alias` command, this function makes sure that each argument was specified at most
66
+ # once. It fills $opts with the variables which were not yet specified.
67
+ # Precondition: the command must be "alias".
68
+ # Parameters: none.
69
+ _unspecified_alias_vars()
70
+ {
71
+ opts=""
72
+
73
+ local help_found=0
74
+ local definition_found=0
75
+ local verbose_found=0
76
+
77
+ for ((i=2; i<COMP_CWORD; ++i)); do
78
+ if [[ "${COMP_WORDS[$i]}" == '--help' ]]; then help_found=1; fi
79
+ if [[ "${COMP_WORDS[$i]}" == '--verbose' ]]; then verbose_found=1; fi
80
+ if [[ "${COMP_WORDS[$i]}" =~ ':' ]]; then definition_found=1; fi
81
+ done
82
+
83
+ if [[ ${help_found} == 0 ]]; then opts="${opts} --help"; fi
84
+ if [[ ${verbose_found} == 0 ]]; then opts="${opts} --verbose"; fi
85
+
86
+ if [[ ${definition_found} == 0 ]] && [[ "${cur}" != ':' ]]; then
87
+ _multipass_instances_with_colon
88
+ fi
89
+ }
90
+
91
+ # Set $multipass_aliases to the list of available aliases.
92
+ _multipass_aliases()
93
+ {
94
+ local cmd="multipass aliases --format=csv 2>/dev/null"
95
+
96
+ multipass_aliases=$( \eval $cmd | \grep -Ev '(\+--|Alias)' | \cut -d',' -f 1 )
97
+ }
98
+
51
99
# Removes the comma or equals sign at the end of $cur.
52
100
_remove_trailing_char()
53
101
{
@@ -126,17 +174,22 @@ _multipass_complete()
126
174
COMP_WORDBREAKS="${COMP_WORDBREAKS}="
127
175
fi
128
176
129
- local cur cmd opts prev prev2 prev_opts
177
+ local cur cmd opts prev prev2 prev_opts multipass_aliases
130
178
COMPREPLY=()
131
179
cur="${COMP_WORDS[COMP_CWORD]}"
132
180
prev="${COMP_WORDS[COMP_CWORD-1]}"
133
181
prev2="${COMP_WORDS[COMP_CWORD-2]}"
134
182
cmd="${COMP_WORDS[1]}"
135
183
prev_opts=false
136
184
multipass_cmds="transfer delete exec find help info launch list mount networks \
137
- purge recover shell start stop suspend restart umount version get set"
185
+ purge recover shell start stop suspend restart umount version get set \
186
+ alias aliases unalias"
187
+
188
+ if [[ "${multipass_cmds}" =~ " ${cmd} " || "${multipass_cmds}" =~ ^${cmd} || "${multipass_cmds}" =~ \ ${cmd}$ ]];
189
+ then
190
+ opts="--help --verbose"
191
+ fi
138
192
139
- opts="--help --verbose"
140
193
case "${cmd}" in
141
194
"info")
142
195
opts="${opts} --all --format"
@@ -165,6 +218,13 @@ _multipass_complete()
165
218
"find")
166
219
opts="${opts} --show-unsupported --format"
167
220
;;
221
+ "aliases")
222
+ opts="${opts} --format"
223
+ ;;
224
+ "unalias")
225
+ _multipass_aliases
226
+ opts="${opts} ${multipass_aliases}"
227
+ ;;
168
228
esac
169
229
170
230
if [[ ${prev} == -* ]]; then
@@ -266,14 +326,20 @@ _multipass_complete()
266
326
return
267
327
fi
268
328
;;
329
+ "alias")
330
+ _unspecified_alias_vars
331
+ # TODO: Do use spaces after the completion of an option starting with "--".
332
+ compopt -o nospace
333
+ ;;
269
334
"help")
270
335
opts=$multipass_cmds
271
336
;;
272
337
esac
273
338
fi
274
339
275
340
if [[ ${COMP_CWORD} -eq 1 ]]; then
276
- opts="${opts} ${multipass_cmds}"
341
+ _multipass_aliases
342
+ opts="${opts} ${multipass_cmds} ${multipass_aliases}"
277
343
fi
278
344
279
345
if [[ -n "${opts}" ]]; then
0 commit comments