Support different bash completion options (#1509)

https://github.com/spf13/cobra/issues/1508

Based on the documentation found here
https://www.gnu.org/software/bash/manual/html_node/Commands-For-Completion.html
we remove descriptions for the following completion types:
- menu-complete
- menu-complete-backward
- insert-completions

Signed-off-by: Marc Khouzam <marc.khouzam@montreal.ca>
This commit is contained in:
Marc Khouzam 2021-12-07 17:59:41 -05:00 committed by GitHub
parent 507caf5ac8
commit 3fed3ef5ad
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -138,13 +138,42 @@ __%[1]s_process_completion_results() {
_filedir -d _filedir -d
fi fi
else else
__%[1]s_handle_standard_completion_case __%[1]s_handle_completion_types
fi fi
__%[1]s_handle_special_char "$cur" : __%[1]s_handle_special_char "$cur" :
__%[1]s_handle_special_char "$cur" = __%[1]s_handle_special_char "$cur" =
} }
__%[1]s_handle_completion_types() {
__%[1]s_debug "__%[1]s_handle_completion_types: COMP_TYPE is $COMP_TYPE"
case $COMP_TYPE in
37|42)
# Type: menu-complete/menu-complete-backward and insert-completions
# If the user requested inserting one completion at a time, or all
# completions at once on the command-line we must remove the descriptions.
# https://github.com/spf13/cobra/issues/1508
local tab comp
tab=$(printf '\t')
while IFS='' read -r comp; do
# Strip any description
comp=${comp%%%%$tab*}
# Only consider the completions that match
comp=$(compgen -W "$comp" -- "$cur")
if [ -n "$comp" ]; then
COMPREPLY+=("$comp")
fi
done < <(printf "%%s\n" "${out[@]}")
;;
*)
# Type: complete (normal completion)
__%[1]s_handle_standard_completion_case
;;
esac
}
__%[1]s_handle_standard_completion_case() { __%[1]s_handle_standard_completion_case() {
local tab comp local tab comp
tab=$(printf '\t') tab=$(printf '\t')