Debian Codegraph @

autocomplete/bash_autocomplete

raw
1 # This is a shell completion script auto-generated by https://github.com/urfave/cli for bash.
2
3 # macOS still ships Bash 3, where bash-completion may not provide
4 # _init_completion. This is a minimal compatible fallback.
5 __%[1]s_init_completion() {
6 COMPREPLY=()
7 if declare -F _comp_initialize >/dev/null 2>&1; then
8 _comp_initialize "$@"
9 else
10 _get_comp_words_by_ref "$@" cur prev words cword
11 fi
12 }
13
14 __%[1]s_build_completion_request() {
15 local -a words_before_cursor=("${COMP_WORDS[@]:0:${COMP_CWORD}}")
16 local current_word="${COMP_WORDS[COMP_CWORD]}"
17
18 if [[ "${current_word}" == "-"* ]]; then
19 printf '%%s %%s --generate-shell-completion' "${words_before_cursor[*]}" "${current_word}"
20 else
21 printf '%%s --generate-shell-completion' "${words_before_cursor[*]}"
22 fi
23 }
24
25 # Keep Bash 3 compatibility: associative arrays require Bash 4+, so
26 # descriptions are looked up via parallel indexed arrays.
27 __%[1]s_find_description_for() {
28 local candidate="$1"
29 local i
30
31 for i in "${!__cli_completion_tokens[@]}"; do
32 if [[ "${__cli_completion_tokens[i]}" == "${candidate}" ]]; then
33 printf '%%s' "${__cli_completion_descriptions[i]}"
34 return 0
35 fi
36 done
37
38 return 1
39 }
40
41 __%[1]s_bash_autocomplete() {
42 local words=("${COMP_WORDS[@]}")
43
44 if [[ "${words[0]}" != "source" ]]; then
45 local cur opts
46 local cword="${COMP_CWORD}"
47 local request_comp
48
49 COMPREPLY=()
50 cur="${words[$cword]}"
51
52 __%[1]s_init_completion -n "=:" || return
53
54 request_comp="$(__%[1]s_build_completion_request)"
55 opts=$(eval "${request_comp}" 2>/dev/null)
56
57 # Completion output lines use "token:description" format.
58 # Keep token/description in parallel arrays for Bash 3 compatibility.
59 __cli_completion_tokens=()
60 __cli_completion_descriptions=()
61
62 local line
63 local longest=0
64 while IFS=$'\n' read -r line; do
65 local token="${line}"
66 local description=""
67
68 if [[ "${line}" == *:* ]]; then
69 token="${line%%%%:*}"
70 description="${line#*:}"
71 fi
72
73 if [[ -z "${token}" ]]; then
74 continue
75 fi
76
77 __cli_completion_tokens+=("${token}")
78 __cli_completion_descriptions+=("${description}")
79 (( ${#token} > longest )) && longest=${#token}
80 done <<< "${opts}"
81
82 local matches=( $(compgen -W "${__cli_completion_tokens[*]}" -- "${cur}") )
83
84 # COMP_TYPE=63 means Bash is listing matches (usually on second TAB).
85 if [[ "${COMP_TYPE:-}" == "63" && ${#matches[@]} -gt 0 ]]; then
86 local listed=()
87 local candidate
88 for candidate in "${matches[@]}"; do
89 local desc="$(__%[1]s_find_description_for "${candidate}")"
90
91 if [[ -n "${desc}" ]]; then
92 local padded="$(printf '%%-*s' "${longest}" "${candidate}")"
93 listed+=("${padded} -- ${desc}")
94 else
95 listed+=("${candidate}")
96 fi
97 done
98 COMPREPLY=("${listed[@]}")
99 else
100 COMPREPLY=("${matches[@]}")
101 fi
102
103 return 0
104 fi
105 }
106
107 complete -o bashdefault -o default -F __%[1]s_bash_autocomplete %[1]s