Bash completion for names with ':' character.
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Adam Bozanich · committed by Adam Bozanich · parent 29cddf6 · Browse files · 3 files·+14 added−3 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
3 files+14 added−3 removed
- other:+14 added−3 removed
Provenance
- Commit
fe79245- Tree
b786284- Author
Adam Bozanich <adam.boz@gmail.com>- Committer
Adam Bozanich <adam.boz@gmail.com>- Parent
29cddf6
3 files changed+14 added−3 removed
modified:bash_completions_test.go+2 added-1 removed
32 unmodified lines33 34 func TestBashCompletions(t *testing.T) {35 c := initializeWithRootCmd()36 cmdEcho.AddCommand(cmdTimes)37- c.AddCommand(cmdEcho, cmdPrint, cmdDeprecated)37+ c.AddCommand(cmdEcho, cmdPrint, cmdDeprecated, cmdColon)38 39 // custom completion function40 c.BashCompletionFunction = bash_completion_func41 32 unmodified lines74 check(t, str, "_cobra-test")75 check(t, str, "_cobra-test_echo")76 check(t, str, "_cobra-test_echo_times")77 check(t, str, "_cobra-test_print")78+ check(t, str, "_cobra-test_cmd__colon")79 80 // check for required flags81 check(t, str, `must_have_one_flag+=("--introot=")`)82 check(t, str, `must_have_one_flag+=("--persistent-filename=")`)13 unmodified linesmodified:bash_completions.go+6 added-2 removed
101 unmodified lines102 103 if [[ ${#COMPREPLY[@]} -eq 0 ]]; then104 declare -F __custom_func >/dev/null && __custom_func105 fi106+107+ __ltrim_colon_completions "$cur"108 }109 110 # The arguments should be in the form "ext1|ext2|extn"111 __handle_filename_extension_flag()55 unmodified lines167 __debug "${FUNCNAME}: c is $c words[c] is ${words[c]}"168 169 local next_command170 if [[ -n ${last_command} ]]; then169- next_command="_${last_command}_${words[c]}"171+ next_command="_${last_command}_${words[c]//:/__}"172 else171- next_command="_${words[c]}"173+ next_command="_${words[c]//:/__}"174 fi175 c=$((c+1))176 __debug "${FUNCNAME}: looking for ${next_command}"177 declare -F $next_command >/dev/null && $next_command19 unmodified lines197 `)198 }199 200 func postscript(out *bytes.Buffer, name string) {201+ name = strings.Replace(name, ":", "__", -1)202 fmt.Fprintf(out, "__start_%s()\n", name)203 fmt.Fprintf(out, `{204 local cur prev words cword205 if declare -F _init_completion >/dev/null 2>&1; then151 unmodified lines357 gen(c, out)358 }359 commandName := cmd.CommandPath()360 commandName = strings.Replace(commandName, " ", "_", -1)361+ commandName = strings.Replace(commandName, ":", "__", -1)362 fmt.Fprintf(out, "_%s()\n{\n", commandName)363 fmt.Fprintf(out, " last_command=%q\n", commandName)364 writeCommands(cmd, out)365 writeFlags(cmd, out)64 unmodified linesmodified:cobra_test.go+6 added
141 unmodified lines142 versionUsed = 2143 },144 }145 146+var cmdColon = &Command{147+ Use: "cmd:colon",148+ Run: func(cmd *Command, args []string) {149+ },150+}151+152 func flagInit() {153 cmdEcho.ResetFlags()154 cmdPrint.ResetFlags()155 cmdTimes.ResetFlags()1014 unmodified lines