MCPcopy
hub / github.com/spf13/cobra / writePreamble

Function writePreamble

bash_completions.go:36–402  ·  view source on GitHub ↗
(buf io.StringWriter, name string)

Source from the content-addressed store, hash-verified

34)
35
36func writePreamble(buf io.StringWriter, name string) {
37 WriteStringAndCheck(buf, fmt.Sprintf("# bash completion for %-36s -*- shell-script -*-\n", name))
38 WriteStringAndCheck(buf, fmt.Sprintf(`
39__%[1]s_debug()
40{
41 if [[ -n ${BASH_COMP_DEBUG_FILE:-} ]]; then
42 echo "$*" >> "${BASH_COMP_DEBUG_FILE}"
43 fi
44}
45
46# Homebrew on Macs have version 1.3 of bash-completion which doesn't include
47# _init_completion. This is a very minimal version of that function.
48__%[1]s_init_completion()
49{
50 COMPREPLY=()
51 _get_comp_words_by_ref "$@" cur prev words cword
52}
53
54__%[1]s_index_of_word()
55{
56 local w word=$1
57 shift
58 index=0
59 for w in "$@"; do
60 [[ $w = "$word" ]] && return
61 index=$((index+1))
62 done
63 index=-1
64}
65
66__%[1]s_contains_word()
67{
68 local w word=$1; shift
69 for w in "$@"; do
70 [[ $w = "$word" ]] && return
71 done
72 return 1
73}
74
75__%[1]s_handle_go_custom_completion()
76{
77 __%[1]s_debug "${FUNCNAME[0]}: cur is ${cur}, words[*] is ${words[*]}, #words[@] is ${#words[@]}"
78
79 local shellCompDirectiveError=%[3]d
80 local shellCompDirectiveNoSpace=%[4]d
81 local shellCompDirectiveNoFileComp=%[5]d
82 local shellCompDirectiveFilterFileExt=%[6]d
83 local shellCompDirectiveFilterDirs=%[7]d
84
85 local out requestComp lastParam lastChar comp directive args
86
87 # Prepare the command to request completions for the program.
88 # Calling ${words[0]} instead of directly %[1]s allows handling aliases
89 args=("${words[@]:1}")
90 # Disable ActiveHelp which is not supported for bash completion v1
91 requestComp="%[8]s=0 ${words[0]} %[2]s ${args[*]}"
92
93 lastParam=${words[$((${#words[@]}-1))]}

Callers 1

GenBashCompletionMethod · 0.85

Calls 2

WriteStringAndCheckFunction · 0.85
activeHelpEnvVarFunction · 0.85

Tested by

no test coverage detected