Skip to content

Commit 9342a30

Browse files
committed
fix(bash): Fix bash completion for suggestions that contain special characters.
Special characters include whitepace, so this is more general than spf13#1743. I added some test cases to cobra-completion-testing. This PR makes them pass. It also doesn't trip any of the performance regression tests.
1 parent bcfcff7 commit 9342a30

File tree

1 file changed

+24
-7
lines changed

1 file changed

+24
-7
lines changed

bash_completionsV2.go

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,10 @@ __%[1]s_get_completion_results() {
5959
# Prepare the command to request completions for the program.
6060
# Calling ${words[0]} instead of directly %[1]s allows handling aliases
6161
args=("${words[@]:1}")
62-
requestComp="${words[0]} %[2]s ${args[*]}"
62+
requestComp="${words[0]} %[2]s"
63+
if [[ "${#args[@]}" -gt 0 ]]; then
64+
requestComp+="$(printf " %%q" "${args[@]}")"
65+
fi
6366
6467
lastParam=${words[$((${#words[@]}-1))]}
6568
lastChar=${lastParam:$((${#lastParam}-1)):1}
@@ -224,15 +227,24 @@ __%[1]s_handle_completion_types() {
224227
# completions at once on the command-line we must remove the descriptions.
225228
# https://github.com/spf13/cobra/issues/1508
226229
local tab=$'\t' comp
227-
while IFS='' read -r comp; do
230+
local matches=()
231+
for comp in "${completions[@]}"; do
228232
[[ -z $comp ]] && continue
229233
# Strip any description
230234
comp=${comp%%%%$tab*}
231235
# Only consider the completions that match
232236
if [[ $comp == "$cur"* ]]; then
233-
COMPREPLY+=("$comp")
234-
fi
235-
done < <(printf "%%s\n" "${completions[@]}")
237+
# Strictly speaking we could append directly to COMPREPLY here.
238+
# But there's a pretty big performance hit involved with
239+
# creating one subshell to printf %%q for each completion that
240+
# matches. Instead, batch all the matches up so we can quote
241+
# them all at once in a single printf call.
242+
matches+=( "$comp" )
243+
fi
244+
done
245+
while IFS='' read -r comp; do
246+
COMPREPLY+=( "$comp" )
247+
done < <(printf "%%q\n" "${matches[@]}")
236248
;;
237249
238250
*)
@@ -247,7 +259,12 @@ __%[1]s_handle_standard_completion_case() {
247259
248260
# Short circuit to optimize if we don't have descriptions
249261
if [[ "${completions[*]}" != *$tab* ]]; then
250-
IFS=$'\n' read -ra COMPREPLY -d '' < <(compgen -W "${completions[*]}" -- "$cur")
262+
# compgen's -W option respects shell quoting, so we need to escape.
263+
local compgen_words="$(printf "%%q\n" "${completions[@]}")"
264+
# compgen appears to respect shell quoting _after_ checking whether
265+
# they have the right prefix, so we also need to quote cur.
266+
local compgen_cur="$(printf "%%q" "${cur}")"
267+
IFS=$'\n' read -ra COMPREPLY -d '' < <(IFS=$'\n'; compgen -W "${compgen_words}" -- "${compgen_cur}")
251268
return 0
252269
fi
253270
@@ -271,7 +288,7 @@ __%[1]s_handle_standard_completion_case() {
271288
__%[1]s_debug "COMPREPLY[0]: ${COMPREPLY[0]}"
272289
comp="${COMPREPLY[0]%%%%$tab*}"
273290
__%[1]s_debug "Removed description from single completion, which is now: ${comp}"
274-
COMPREPLY[0]=$comp
291+
COMPREPLY[0]="$(printf "%%q" "${comp}")"
275292
else # Format the descriptions
276293
__%[1]s_format_comp_descriptions $longest
277294
fi

0 commit comments

Comments
 (0)