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

Function genBashComp

bash_completionsV2.go:31–467  ·  bash_completionsV2.go::genBashComp
(buf io.StringWriter, name string, includeDesc bool)

Source from the content-addressed store, hash-verified

29}
30
31func genBashComp(buf io.StringWriter, name string, includeDesc bool) {
32 compCmd := ShellCompRequestCmd
33 if !includeDesc {
34 compCmd = ShellCompNoDescRequestCmd
35 }
36
37 WriteStringAndCheck(buf, fmt.Sprintf(`# bash completion V2 for %-36[1]s -*- shell-script -*-
38
39__%[1]s_debug()
40{
41 if [[ -n ${BASH_COMP_DEBUG_FILE-} ]]; then
42 echo "$*" >> "${BASH_COMP_DEBUG_FILE}"
43 fi
44}
45
46# Macs have bash3 for which the bash-completion package doesn't include
47# _init_completion. This is a 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# This function calls the %[1]s program to obtain the completion
55# results and the directive. It fills the 'out' and 'directive' vars.
56__%[1]s_get_completion_results() {
57 local requestComp lastParam lastChar args
58
59 # Prepare the command to request completions for the program.
60 # Calling ${words[0]} instead of directly %[1]s allows handling aliases
61 args=("${words[@]:1}")
62 requestComp="${words[0]} %[2]s ${args[*]}"
63
64 lastParam=${words[$((${#words[@]}-1))]}
65 lastChar=${lastParam:$((${#lastParam}-1)):1}
66 __%[1]s_debug "lastParam ${lastParam}, lastChar ${lastChar}"
67
68 if [[ -z ${cur} && ${lastChar} != = ]]; then
69 # If the last parameter is complete (there is a space following it)
70 # We add an extra empty parameter so we can indicate this to the go method.
71 __%[1]s_debug "Adding extra empty parameter"
72 requestComp="${requestComp} ''"
73 fi
74
75 # When completing a flag with an = (e.g., %[1]s -n=<TAB>)
76 # bash focuses on the part after the =, so we need to remove
77 # the flag part from $cur
78 if [[ ${cur} == -*=* ]]; then
79 cur="${cur#*=}"
80 fi
81
82 __%[1]s_debug "Calling ${requestComp}"
83 # Use eval to handle any environment variables and such
84 out=$(eval "${requestComp}" 2>/dev/null)
85
86 # Extract the directive integer at the very end of the output following a colon (:)
87 directive=${out##*:}
88 # Remove the directive

Callers 1

genBashCompletionMethod · 0.85

Calls 1

WriteStringAndCheckFunction · 0.85

Tested by

no test coverage detected