Rename BashCompDirectives to ShellCompDirectives (#1082)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Since the completion directives will be used for all shells, and that these names will be consumed by users, this is a more appropriate name.
authored by Marc Khouzam · committed by GitHub · parent b80aeb1 · Browse files · 5 files·+93 added−93 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Trailers
- Signed-off-by: Marc Khouzam <marc.khouzam@montreal.ca>
Diff stats
5 files+93 added−93 removed
- other:+93 added−93 removed
Provenance
- Commit
b84ef40- Tree
ea99060- Author
Marc Khouzam <marc.khouzam@montreal.ca>- Committer
GitHub <noreply@github.com>- Parent
b80aeb1
5 files changed+93 added−93 removed
modified:bash_completions.go+1 added-1 removed
342 unmodified lines343 fi344 __%[1]s_handle_word345 }346 347-`, name, CompRequestCmd, BashCompDirectiveError, BashCompDirectiveNoSpace, BashCompDirectiveNoFileComp))347+`, name, ShellCompRequestCmd, ShellCompDirectiveError, ShellCompDirectiveNoSpace, ShellCompDirectiveNoFileComp))348 }349 350 func writePostscript(buf *bytes.Buffer, name string) {351 name = strings.Replace(name, ":", "__", -1)287 unmodified linesmodified:bash_completions.md+14 added-14 removed
126 unmodified lines127 Long: status_long,128 RunE: func(cmd *cobra.Command, args []string) {129 RunGet(args[0])130 },131- ValidArgsFunction: func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.BashCompDirective) {131+ ValidArgsFunction: func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {132 if len(args) != 0 {133- return nil, cobra.BashCompDirectiveNoFileComp133+ return nil, cobra.ShellCompDirectiveNoFileComp134 }135- return getReleasesFromCluster(toComplete), cobra.BashCompDirectiveNoFileComp135+ return getReleasesFromCluster(toComplete), cobra.ShellCompDirectiveNoFileComp136 },137 }138 ```139 Where `getReleasesFromCluster()` is a Go function that obtains the list of current Helm releases running on the Kubernetes cluster.2 unmodified lines142 ```bash143 # helm status [tab][tab]144 harbor notary rook thanos145 ```146-You may have noticed the use of `cobra.BashCompDirective`. These directives are bit fields allowing to control some shell completion behaviors for your particular completion. You can combine them with the bit-or operator such as `cobra.BashCompDirectiveNoSpace | cobra.BashCompDirectiveNoFileComp`146+You may have noticed the use of `cobra.ShellCompDirective`. These directives are bit fields allowing to control some shell completion behaviors for your particular completion. You can combine them with the bit-or operator such as `cobra.ShellCompDirectiveNoSpace | cobra.ShellCompDirectiveNoFileComp`147 ```go148 // Indicates an error occurred and completions should be ignored.149-BashCompDirectiveError149+ShellCompDirectiveError150 // Indicates that the shell should not add a space after the completion,151 // even if there is a single completion provided.152-BashCompDirectiveNoSpace152+ShellCompDirectiveNoSpace153 // Indicates that the shell should not provide file completion even when154 // no completion is provided.155 // This currently does not work for zsh or bash < 4156-BashCompDirectiveNoFileComp156+ShellCompDirectiveNoFileComp157 // Indicates that the shell will perform its default behavior after completions158-// have been provided (this implies !BashCompDirectiveNoSpace && !BashCompDirectiveNoFileComp).159-BashCompDirectiveDefault158+// have been provided (this implies !ShellCompDirectiveNoSpace && !ShellCompDirectiveNoFileComp).159+ShellCompDirectiveDefault160 ```161 162 When using the `ValidArgsFunction`, Cobra will call your registered function after having parsed all flags and arguments provided in the command-line. You therefore don't need to do this parsing yourself. For example, when a user calls `helm status --namespace my-rook-ns [tab][tab]`, Cobra will call your registered `ValidArgsFunction` after having parsed the `--namespace` flag, as it would have done when calling the `RunE` function.163 3 unmodified lines167 ```bash168 # helm __complete status har<ENTER>169 harbor170 :4171-Completion ended with directive: BashCompDirectiveNoFileComp # This is on stderr171+Completion ended with directive: ShellCompDirectiveNoFileComp # This is on stderr172 ```173 ***Important:*** If the noun to complete is empty, you must pass an empty parameter to the `__complete` command:174 ```bash175 # helm __complete status ""<ENTER>1 unmodified line177 notary178 rook179 thanos180 :4181-Completion ended with directive: BashCompDirectiveNoFileComp # This is on stderr181+Completion ended with directive: ShellCompDirectiveNoFileComp # This is on stderr182 ```183 Calling the `__complete` command directly allows you to run the Go debugger to troubleshoot your code. You can also add printouts to your code; Cobra provides the following functions to use for printouts in Go completion code:184 ```go185 // Prints to the completion script debug file (if BASH_COMP_DEBUG_FILE120 unmodified lines306 To provide a Go function that Cobra will execute when it needs the list of completion choices for a flag, you must register the function in the following manner:307 308 ```go309 flagName := "output"310-cmd.RegisterFlagCompletionFunc(flagName, func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.BashCompDirective) {311- return []string{"json", "table", "yaml"}, cobra.BashCompDirectiveDefault310+cmd.RegisterFlagCompletionFunc(flagName, func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {311+ return []string{"json", "table", "yaml"}, cobra.ShellCompDirectiveDefault312 })313 ```314 Notice that calling `RegisterFlagCompletionFunc()` is done through the `command` with which the flag is associated. In our example this dynamic completion will give results like so:315 10 unmodified lines326 json327 table328 yaml329 :4330-Completion ended with directive: BashCompDirectiveNoFileComp # This is on stderr330+Completion ended with directive: ShellCompDirectiveNoFileComp # This is on stderr331 ```332 ***Important:*** You should **not** leave traces that print to stdout in your completion code as they will be interpreted as completion choices by the completion script. Instead, use the cobra-provided debugging traces functions mentioned in the above section.333 334 ## 2. Custom completions of flags written in Bash45 unmodified linesmodified:command.go+1 added-1 removed
58 unmodified lines59 ValidArgs []string60 // ValidArgsFunction is an optional function that provides valid non-flag arguments for bash completion.61 // It is a dynamic version of using ValidArgs.62 // Only one of ValidArgs and ValidArgsFunction can be used for a command.63- ValidArgsFunction func(cmd *Command, args []string, toComplete string) ([]string, BashCompDirective)63+ ValidArgsFunction func(cmd *Command, args []string, toComplete string) ([]string, ShellCompDirective)64 65 // Expected arguments66 Args PositionalArgs67 1567 unmodified linesmodified:custom_completions_test.go+42 added-42 removed
4 unmodified lines5 "strings"6 "testing"7 )8 9-func validArgsFunc(cmd *Command, args []string, toComplete string) ([]string, BashCompDirective) {9+func validArgsFunc(cmd *Command, args []string, toComplete string) ([]string, ShellCompDirective) {10 if len(args) != 0 {11- return nil, BashCompDirectiveNoFileComp11+ return nil, ShellCompDirectiveNoFileComp12 }13 14 var completions []string15 for _, comp := range []string{"one", "two"} {16 if strings.HasPrefix(comp, toComplete) {17 completions = append(completions, comp)18 }19 }20- return completions, BashCompDirectiveDefault20+ return completions, ShellCompDirectiveDefault21 }22 23-func validArgsFunc2(cmd *Command, args []string, toComplete string) ([]string, BashCompDirective) {23+func validArgsFunc2(cmd *Command, args []string, toComplete string) ([]string, ShellCompDirective) {24 if len(args) != 0 {25- return nil, BashCompDirectiveNoFileComp25+ return nil, ShellCompDirectiveNoFileComp26 }27 28 var completions []string29 for _, comp := range []string{"three", "four"} {30 if strings.HasPrefix(comp, toComplete) {31 completions = append(completions, comp)32 }33 }34- return completions, BashCompDirectiveDefault34+ return completions, ShellCompDirectiveDefault35 }36 37 func TestValidArgsFuncSingleCmd(t *testing.T) {38 rootCmd := &Command{2 unmodified lines41 Run: emptyRun,42 }43 44 // Test completing an empty string45- output, err := executeCommand(rootCmd, CompRequestCmd, "")45+ output, err := executeCommand(rootCmd, ShellCompRequestCmd, "")46 if err != nil {47 t.Errorf("Unexpected error: %v", err)48 }49 50 expected := strings.Join([]string{51 "one",52 "two",53 ":0",54- "Completion ended with directive: BashCompDirectiveDefault", ""}, "\n")54+ "Completion ended with directive: ShellCompDirectiveDefault", ""}, "\n")55 56 if output != expected {57 t.Errorf("expected: %q, got: %q", expected, output)58 }59 60 // Check completing with a prefix61- output, err = executeCommand(rootCmd, CompRequestCmd, "t")61+ output, err = executeCommand(rootCmd, ShellCompRequestCmd, "t")62 if err != nil {63 t.Errorf("Unexpected error: %v", err)64 }65 66 expected = strings.Join([]string{67 "two",68 ":0",69- "Completion ended with directive: BashCompDirectiveDefault", ""}, "\n")69+ "Completion ended with directive: ShellCompDirectiveDefault", ""}, "\n")70 71 if output != expected {72 t.Errorf("expected: %q, got: %q", expected, output)73 }11 unmodified lines85 Run: emptyRun,86 }87 88 // Check completing with wrong number of args89- output, err := executeCommand(rootCmd, CompRequestCmd, "unexpectedArg", "t")89+ output, err := executeCommand(rootCmd, ShellCompRequestCmd, "unexpectedArg", "t")90 if err != nil {91 t.Errorf("Unexpected error: %v", err)92 }93 94 expected := strings.Join([]string{95 ":4",96- "Completion ended with directive: BashCompDirectiveNoFileComp", ""}, "\n")96+ "Completion ended with directive: ShellCompDirectiveNoFileComp", ""}, "\n")97 98 if output != expected {99 t.Errorf("expected: %q, got: %q", expected, output)100 }13 unmodified lines114 }115 rootCmd.AddCommand(child1Cmd, child2Cmd)116 117 // Test completion of first sub-command with empty argument118- output, err := executeCommand(rootCmd, CompRequestCmd, "child1", "")118+ output, err := executeCommand(rootCmd, ShellCompRequestCmd, "child1", "")119 if err != nil {120 t.Errorf("Unexpected error: %v", err)121 }122 123 expected := strings.Join([]string{124 "one",125 "two",126 ":0",127- "Completion ended with directive: BashCompDirectiveDefault", ""}, "\n")127+ "Completion ended with directive: ShellCompDirectiveDefault", ""}, "\n")128 129 if output != expected {130 t.Errorf("expected: %q, got: %q", expected, output)131 }132 133 // Test completion of first sub-command with a prefix to complete134- output, err = executeCommand(rootCmd, CompRequestCmd, "child1", "t")134+ output, err = executeCommand(rootCmd, ShellCompRequestCmd, "child1", "t")135 if err != nil {136 t.Errorf("Unexpected error: %v", err)137 }138 139 expected = strings.Join([]string{140 "two",141 ":0",142- "Completion ended with directive: BashCompDirectiveDefault", ""}, "\n")142+ "Completion ended with directive: ShellCompDirectiveDefault", ""}, "\n")143 144 if output != expected {145 t.Errorf("expected: %q, got: %q", expected, output)146 }147 148 // Check completing with wrong number of args149- output, err = executeCommand(rootCmd, CompRequestCmd, "child1", "unexpectedArg", "t")149+ output, err = executeCommand(rootCmd, ShellCompRequestCmd, "child1", "unexpectedArg", "t")150 if err != nil {151 t.Errorf("Unexpected error: %v", err)152 }153 154 expected = strings.Join([]string{155 ":4",156- "Completion ended with directive: BashCompDirectiveNoFileComp", ""}, "\n")156+ "Completion ended with directive: ShellCompDirectiveNoFileComp", ""}, "\n")157 158 if output != expected {159 t.Errorf("expected: %q, got: %q", expected, output)160 }161 162 // Test completion of second sub-command with empty argument163- output, err = executeCommand(rootCmd, CompRequestCmd, "child2", "")163+ output, err = executeCommand(rootCmd, ShellCompRequestCmd, "child2", "")164 if err != nil {165 t.Errorf("Unexpected error: %v", err)166 }167 168 expected = strings.Join([]string{169 "three",170 "four",171 ":0",172- "Completion ended with directive: BashCompDirectiveDefault", ""}, "\n")172+ "Completion ended with directive: ShellCompDirectiveDefault", ""}, "\n")173 174 if output != expected {175 t.Errorf("expected: %q, got: %q", expected, output)176 }177 178- output, err = executeCommand(rootCmd, CompRequestCmd, "child2", "t")178+ output, err = executeCommand(rootCmd, ShellCompRequestCmd, "child2", "t")179 if err != nil {180 t.Errorf("Unexpected error: %v", err)181 }182 183 expected = strings.Join([]string{184 "three",185 ":0",186- "Completion ended with directive: BashCompDirectiveDefault", ""}, "\n")186+ "Completion ended with directive: ShellCompDirectiveDefault", ""}, "\n")187 188 if output != expected {189 t.Errorf("expected: %q, got: %q", expected, output)190 }191 192 // Check completing with wrong number of args193- output, err = executeCommand(rootCmd, CompRequestCmd, "child2", "unexpectedArg", "t")193+ output, err = executeCommand(rootCmd, ShellCompRequestCmd, "child2", "unexpectedArg", "t")194 if err != nil {195 t.Errorf("Unexpected error: %v", err)196 }197 198 expected = strings.Join([]string{199 ":4",200- "Completion ended with directive: BashCompDirectiveNoFileComp", ""}, "\n")200+ "Completion ended with directive: ShellCompDirectiveNoFileComp", ""}, "\n")201 202 if output != expected {203 t.Errorf("expected: %q, got: %q", expected, output)204 }9 unmodified lines214 }215 rootCmd.AddCommand(child)216 217 // Test completion of first sub-command with empty argument218- output, err := executeCommand(rootCmd, CompRequestCmd, "son", "")218+ output, err := executeCommand(rootCmd, ShellCompRequestCmd, "son", "")219 if err != nil {220 t.Errorf("Unexpected error: %v", err)221 }222 223 expected := strings.Join([]string{224 "one",225 "two",226 ":0",227- "Completion ended with directive: BashCompDirectiveDefault", ""}, "\n")227+ "Completion ended with directive: ShellCompDirectiveDefault", ""}, "\n")228 229 if output != expected {230 t.Errorf("expected: %q, got: %q", expected, output)231 }232 233 // Test completion of first sub-command with a prefix to complete234- output, err = executeCommand(rootCmd, CompRequestCmd, "daughter", "t")234+ output, err = executeCommand(rootCmd, ShellCompRequestCmd, "daughter", "t")235 if err != nil {236 t.Errorf("Unexpected error: %v", err)237 }238 239 expected = strings.Join([]string{240 "two",241 ":0",242- "Completion ended with directive: BashCompDirectiveDefault", ""}, "\n")242+ "Completion ended with directive: ShellCompDirectiveDefault", ""}, "\n")243 244 if output != expected {245 t.Errorf("expected: %q, got: %q", expected, output)246 }247 248 // Check completing with wrong number of args249- output, err = executeCommand(rootCmd, CompRequestCmd, "son", "unexpectedArg", "t")249+ output, err = executeCommand(rootCmd, ShellCompRequestCmd, "son", "unexpectedArg", "t")250 if err != nil {251 t.Errorf("Unexpected error: %v", err)252 }253 254 expected = strings.Join([]string{255 ":4",256- "Completion ended with directive: BashCompDirectiveNoFileComp", ""}, "\n")256+ "Completion ended with directive: ShellCompDirectiveNoFileComp", ""}, "\n")257 258 if output != expected {259 t.Errorf("expected: %q, got: %q", expected, output)260 }35 unmodified lines296 Use: "root",297 Run: emptyRun,298 }299 rootCmd.Flags().IntP("introot", "i", -1, "help message for flag introot")300- rootCmd.RegisterFlagCompletionFunc("introot", func(cmd *Command, args []string, toComplete string) ([]string, BashCompDirective) {300+ rootCmd.RegisterFlagCompletionFunc("introot", func(cmd *Command, args []string, toComplete string) ([]string, ShellCompDirective) {301 completions := []string{}302 for _, comp := range []string{"1", "2", "10"} {303 if strings.HasPrefix(comp, toComplete) {304 completions = append(completions, comp)305 }306 }307- return completions, BashCompDirectiveDefault307+ return completions, ShellCompDirectiveDefault308 })309 rootCmd.Flags().String("filename", "", "Enter a filename")310- rootCmd.RegisterFlagCompletionFunc("filename", func(cmd *Command, args []string, toComplete string) ([]string, BashCompDirective) {310+ rootCmd.RegisterFlagCompletionFunc("filename", func(cmd *Command, args []string, toComplete string) ([]string, ShellCompDirective) {311 completions := []string{}312 for _, comp := range []string{"file.yaml", "myfile.json", "file.xml"} {313 if strings.HasPrefix(comp, toComplete) {314 completions = append(completions, comp)315 }316 }317- return completions, BashCompDirectiveNoSpace | BashCompDirectiveNoFileComp317+ return completions, ShellCompDirectiveNoSpace | ShellCompDirectiveNoFileComp318 })319 320 // Test completing an empty string321- output, err := executeCommand(rootCmd, CompRequestCmd, "--introot", "")321+ output, err := executeCommand(rootCmd, ShellCompRequestCmd, "--introot", "")322 if err != nil {323 t.Errorf("Unexpected error: %v", err)324 }325 1 unmodified line327 "1",328 "2",329 "10",330 ":0",331- "Completion ended with directive: BashCompDirectiveDefault", ""}, "\n")331+ "Completion ended with directive: ShellCompDirectiveDefault", ""}, "\n")332 333 if output != expected {334 t.Errorf("expected: %q, got: %q", expected, output)335 }336 337 // Check completing with a prefix338- output, err = executeCommand(rootCmd, CompRequestCmd, "--introot", "1")338+ output, err = executeCommand(rootCmd, ShellCompRequestCmd, "--introot", "1")339 if err != nil {340 t.Errorf("Unexpected error: %v", err)341 }342 343 expected = strings.Join([]string{344 "1",345 "10",346 ":0",347- "Completion ended with directive: BashCompDirectiveDefault", ""}, "\n")347+ "Completion ended with directive: ShellCompDirectiveDefault", ""}, "\n")348 349 if output != expected {350 t.Errorf("expected: %q, got: %q", expected, output)351 }352 353 // Test completing an empty string354- output, err = executeCommand(rootCmd, CompRequestCmd, "--filename", "")354+ output, err = executeCommand(rootCmd, ShellCompRequestCmd, "--filename", "")355 if err != nil {356 t.Errorf("Unexpected error: %v", err)357 }358 1 unmodified line360 "file.yaml",361 "myfile.json",362 "file.xml",363 ":6",364- "Completion ended with directive: BashCompDirectiveNoSpace, BashCompDirectiveNoFileComp", ""}, "\n")364+ "Completion ended with directive: ShellCompDirectiveNoSpace, ShellCompDirectiveNoFileComp", ""}, "\n")365 366 if output != expected {367 t.Errorf("expected: %q, got: %q", expected, output)368 }369 370 // Check completing with a prefix371- output, err = executeCommand(rootCmd, CompRequestCmd, "--filename", "f")371+ output, err = executeCommand(rootCmd, ShellCompRequestCmd, "--filename", "f")372 if err != nil {373 t.Errorf("Unexpected error: %v", err)374 }375 376 expected = strings.Join([]string{377 "file.yaml",378 "file.xml",379 ":6",380- "Completion ended with directive: BashCompDirectiveNoSpace, BashCompDirectiveNoFileComp", ""}, "\n")380+ "Completion ended with directive: ShellCompDirectiveNoSpace, ShellCompDirectiveNoFileComp", ""}, "\n")381 382 if output != expected {383 t.Errorf("expected: %q, got: %q", expected, output)384 }1 unmodified linemodified:custom_completions.go+35 added-35 removed
7 unmodified lines8 9 "github.com/spf13/pflag"10 )11 12-// CompRequestCmd is the name of the hidden command that is used to request12+// ShellCompRequestCmd is the name of the hidden command that is used to request13 // completion results from the program. It is used by the shell completion script.14-const CompRequestCmd = "__complete"14+const ShellCompRequestCmd = "__complete"15 16 // Global map of flag completion functions.17-var flagCompletionFunctions = map[*pflag.Flag]func(cmd *Command, args []string, toComplete string) ([]string, BashCompDirective){}17+var flagCompletionFunctions = map[*pflag.Flag]func(cmd *Command, args []string, toComplete string) ([]string, ShellCompDirective){}18 19-// BashCompDirective is a bit map representing the different behaviors the shell19+// ShellCompDirective is a bit map representing the different behaviors the shell20 // can be instructed to have once completions have been provided.21-type BashCompDirective int21+type ShellCompDirective int22 23 const (24- // BashCompDirectiveError indicates an error occurred and completions should be ignored.25- BashCompDirectiveError BashCompDirective = 1 << iota24+ // ShellCompDirectiveError indicates an error occurred and completions should be ignored.25+ ShellCompDirectiveError ShellCompDirective = 1 << iota26 27- // BashCompDirectiveNoSpace indicates that the shell should not add a space27+ // ShellCompDirectiveNoSpace indicates that the shell should not add a space28 // after the completion even if there is a single completion provided.29- BashCompDirectiveNoSpace29+ ShellCompDirectiveNoSpace30 31- // BashCompDirectiveNoFileComp indicates that the shell should not provide31+ // ShellCompDirectiveNoFileComp indicates that the shell should not provide32 // file completion even when no completion is provided.33 // This currently does not work for zsh or bash < 434- BashCompDirectiveNoFileComp34+ ShellCompDirectiveNoFileComp35 36- // BashCompDirectiveDefault indicates to let the shell perform its default36+ // ShellCompDirectiveDefault indicates to let the shell perform its default37 // behavior after completions have been provided.38- BashCompDirectiveDefault BashCompDirective = 038+ ShellCompDirectiveDefault ShellCompDirective = 039 )40 41 // RegisterFlagCompletionFunc should be called to register a function to provide completion for a flag.42-func (c *Command) RegisterFlagCompletionFunc(flagName string, f func(cmd *Command, args []string, toComplete string) ([]string, BashCompDirective)) error {42+func (c *Command) RegisterFlagCompletionFunc(flagName string, f func(cmd *Command, args []string, toComplete string) ([]string, ShellCompDirective)) error {43 flag := c.Flag(flagName)44 if flag == nil {45 return fmt.Errorf("RegisterFlagCompletionFunc: flag '%s' does not exist", flagName)46 }4 unmodified lines51 return nil52 }53 54 // Returns a string listing the different directive enabled in the specified parameter55-func (d BashCompDirective) string() string {55+func (d ShellCompDirective) string() string {56 var directives []string57- if d&BashCompDirectiveError != 0 {58- directives = append(directives, "BashCompDirectiveError")57+ if d&ShellCompDirectiveError != 0 {58+ directives = append(directives, "ShellCompDirectiveError")59 }60- if d&BashCompDirectiveNoSpace != 0 {61- directives = append(directives, "BashCompDirectiveNoSpace")60+ if d&ShellCompDirectiveNoSpace != 0 {61+ directives = append(directives, "ShellCompDirectiveNoSpace")62 }63- if d&BashCompDirectiveNoFileComp != 0 {64- directives = append(directives, "BashCompDirectiveNoFileComp")63+ if d&ShellCompDirectiveNoFileComp != 0 {64+ directives = append(directives, "ShellCompDirectiveNoFileComp")65 }66 if len(directives) == 0 {67- directives = append(directives, "BashCompDirectiveDefault")67+ directives = append(directives, "ShellCompDirectiveDefault")68 }69 70- if d > BashCompDirectiveError+BashCompDirectiveNoSpace+BashCompDirectiveNoFileComp {71- return fmt.Sprintf("ERROR: unexpected BashCompDirective value: %d", d)70+ if d > ShellCompDirectiveError+ShellCompDirectiveNoSpace+ShellCompDirectiveNoFileComp {71+ return fmt.Sprintf("ERROR: unexpected ShellCompDirective value: %d", d)72 }73 return strings.Join(directives, ", ")74 }75 76 // Adds a special hidden command that can be used to request custom completions.77 func (c *Command) initCompleteCmd(args []string) {78 completeCmd := &Command{79- Use: fmt.Sprintf("%s [command-line]", CompRequestCmd),79+ Use: fmt.Sprintf("%s [command-line]", ShellCompRequestCmd),80 DisableFlagsInUseLine: true,81 Hidden: true,82 DisableFlagParsing: true,83 Args: MinimumNArgs(1),84 Short: "Request shell completion choices for the specified command-line",85 Long: fmt.Sprintf("%[2]s is a special command that is used by the shell completion logic\n%[1]s",86- "to request completion choices for the specified command-line.", CompRequestCmd),86+ "to request completion choices for the specified command-line.", ShellCompRequestCmd),87 Run: func(cmd *Command, args []string) {88 finalCmd, completions, directive, err := cmd.getCompletions(args)89 if err != nil {90 CompErrorln(err.Error())6 unmodified lines97 // Print each possible completion to stdout for the completion script to consume.98 fmt.Fprintln(finalCmd.OutOrStdout(), comp)99 }100 101- if directive > BashCompDirectiveError+BashCompDirectiveNoSpace+BashCompDirectiveNoFileComp {102- directive = BashCompDirectiveDefault101+ if directive > ShellCompDirectiveError+ShellCompDirectiveNoSpace+ShellCompDirectiveNoFileComp {102+ directive = ShellCompDirectiveDefault103 }104 105 // As the last printout, print the completion directive for the completion script to parse.106 // The directive integer must be that last character following a single colon (:).6 unmodified lines113 },114 }115 c.AddCommand(completeCmd)116 subCmd, _, err := c.Find(args)117- if err != nil || subCmd.Name() != CompRequestCmd {117+ if err != nil || subCmd.Name() != ShellCompRequestCmd {118 // Only create this special command if it is actually being called.119 // This reduces possible side-effects of creating such a command;120 // for example, having this command would cause problems to a121 // cobra program that only consists of the root command, since this1 unmodified line123 c.RemoveCommand(completeCmd)124 }125 }126 127-func (c *Command) getCompletions(args []string) (*Command, []string, BashCompDirective, error) {127+func (c *Command) getCompletions(args []string) (*Command, []string, ShellCompDirective, error) {128 var completions []string129 130 // The last argument, which is not completely typed by the user,131 // should not be part of the list of arguments3 unmodified lines135 // Find the real command for which completion must be performed136 finalCmd, finalArgs, err := c.Root().Find(trimmedArgs)137 if err != nil {138 // Unable to find the real command. E.g., <program> someInvalidCmd <TAB>139- return c, completions, BashCompDirectiveDefault, fmt.Errorf("Unable to find a command for arguments: %v", trimmedArgs)139+ return c, completions, ShellCompDirectiveDefault, fmt.Errorf("Unable to find a command for arguments: %v", trimmedArgs)140 }141 142 var flag *pflag.Flag143 if !finalCmd.DisableFlagParsing {1 unmodified line145 // This is important for commands which have requested to do their own flag completion.146 flag, finalArgs, toComplete, err = checkIfFlagCompletion(finalCmd, finalArgs, toComplete)147 if err != nil {148 // Error while attempting to parse flags149- return finalCmd, completions, BashCompDirectiveDefault, err149+ return finalCmd, completions, ShellCompDirectiveDefault, err150 }151 }152 153 // Parse the flags and extract the arguments to prepare for calling the completion function154 if err = finalCmd.ParseFlags(finalArgs); err != nil {155- return finalCmd, completions, BashCompDirectiveDefault, fmt.Errorf("Error while parsing flags from args %v: %s", finalArgs, err.Error())155+ return finalCmd, completions, ShellCompDirectiveDefault, fmt.Errorf("Error while parsing flags from args %v: %s", finalArgs, err.Error())156 }157 158 // We only remove the flags from the arguments if DisableFlagParsing is not set.159 // This is important for commands which have requested to do their own flag completion.1 unmodified line161 finalArgs = finalCmd.Flags().Args()162 }163 164 // Find the completion function for the flag or command165- var completionFn func(cmd *Command, args []string, toComplete string) ([]string, BashCompDirective)165+ var completionFn func(cmd *Command, args []string, toComplete string) ([]string, ShellCompDirective)166 if flag != nil {167 completionFn = flagCompletionFunctions[flag]168 } else {169 completionFn = finalCmd.ValidArgsFunction170 }171 if completionFn == nil {172 // Go custom completion not supported/needed for this flag or command173- return finalCmd, completions, BashCompDirectiveDefault, nil173+ return finalCmd, completions, ShellCompDirectiveDefault, nil174 }175 176 // Call the registered completion function to get the completions177 comps, directive := completionFn(finalCmd, finalArgs, toComplete)122 unmodified lines