Skip to content
Myr

The default ShellCompDirective can be customized for a command and its subcommands (#2238)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by Harald Albers · committed by GitHub · parent c8289c1 · Browse files · 3 files+102 added−0 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+102 added−0 removed

  • other:+102 added−0 removed

3 files+102 added−0 removed

modified:site/content/completions/_index.md+30 added
303 unmodified lines
304$ helm status --output [tab][tab]305json table yaml306```307308#### Change the default ShellCompDirective309310When no completion function is registered for a leaf command or for a flag, Cobra will311automatically use `ShellCompDirectiveDefault`, which will invoke the shell's filename completion.312This implies that when file completion does not apply to a leaf command or to a flag (the command313or flag does not operate on a filename), turning off file completion requires you to register a314completion function for that command/flag.315For example:316317```go318cmd.RegisterFlagCompletionFunc("flag-name", cobra.NoFileCompletions)319```320321If you find that there are more situations where file completion should be turned off than322when it is applicable, you can recursively change the default `ShellCompDirective` for a command323and its subcommands to `ShellCompDirectiveNoFileComp`:324325```go326cmd.CompletionOptions.SetDefaultShellCompDirective(ShellCompDirectiveNoFileComp)327```328329If doing so, keep in mind that you should instead register a completion function for leaf commands or330flags where file completion is applicable. For example:331332```go333cmd.RegisterFlagCompletionFunc("flag-name", cobra.FixedCompletions(nil, ShellCompDirectiveDefault))334```335336To change the default directive for the entire program, set the DefaultShellCompDirective on the root command.337338#### Debugging339340You can also easily debug your Go completion code for flags:341```bash
275 unmodified lines
modified:completions_test.go+57 added
4013 unmodified lines
4014 }4015 })4016 }4017}40184019func TestCustomDefaultShellCompDirective(t *testing.T) {4020 rootCmd := &Command{Use: "root", Run: emptyRun}4021 rootCmd.PersistentFlags().String("string", "", "test string flag")4022 // use ShellCompDirectiveNoFileComp instead of the default, which is ShellCompDirectiveDefault.4023 rootCmd.CompletionOptions.SetDefaultShellCompDirective(ShellCompDirectiveNoFileComp)40244025 // child1 inherits the custom DefaultShellCompDirective.4026 childCmd1 := &Command{Use: "child1", Run: emptyRun}4027 // child2 resets the custom DefaultShellCompDirective to the default value.4028 childCmd2 := &Command{Use: "child2", Run: emptyRun}4029 childCmd2.CompletionOptions.SetDefaultShellCompDirective(ShellCompDirectiveDefault)40304031 rootCmd.AddCommand(childCmd1, childCmd2)40324033 testCases := []struct {4034 desc string4035 args []string4036 expectedDirective string4037 }{4038 {4039 "flag completion on root command with custom DefaultShellCompDirective",4040 []string{"--string", ""},4041 "ShellCompDirectiveNoFileComp",4042 },4043 {4044 "flag completion on subcommand with inherited custom DefaultShellCompDirective",4045 []string{"child1", "--string", ""},4046 "ShellCompDirectiveNoFileComp",4047 },4048 {4049 "flag completion on subcommand with reset DefaultShellCompDirective",4050 []string{"child2", "--string", ""},4051 "ShellCompDirectiveDefault",4052 },4053 }40544055 for _, tc := range testCases {4056 t.Run(tc.desc, func(t *testing.T) {4057 args := []string{ShellCompNoDescRequestCmd}4058 args = append(args, tc.args...)40594060 output, err := executeCommand(rootCmd, args...)40614062 if err != nil {4063 t.Errorf("Unexpected error: %v", err)4064 }40654066 outputWords := strings.Split(strings.TrimSpace(output), " ")4067 directive := outputWords[len(outputWords)-1]40684069 if directive != tc.expectedDirective {4070 t.Errorf("expected: %q, got: %q", tc.expectedDirective, directive)4071 }4072 })4073 }4074}
modified:completions.go+15 added
113 unmodified lines
114 // that support them115 DisableDescriptions bool116 // HiddenDefaultCmd makes the default 'completion' command hidden117 HiddenDefaultCmd bool118 // DefaultShellCompDirective sets the ShellCompDirective that is returned119 // if no special directive can be determined120 DefaultShellCompDirective *ShellCompDirective121}122123func (receiver *CompletionOptions) SetDefaultShellCompDirective(directive ShellCompDirective) {124 receiver.DefaultShellCompDirective = &directive125}126127// Completion is a string that can be used for completions128//129// two formats are supported:130// - the completion choice
355 unmodified lines
486 return finalCmd, completions, directive, nil487 }488 } else {489 directive = ShellCompDirectiveDefault490 // check current and parent commands for a custom DefaultShellCompDirective491 for cmd := finalCmd; cmd != nil; cmd = cmd.parent {492 if cmd.CompletionOptions.DefaultShellCompDirective != nil {493 directive = *cmd.CompletionOptions.DefaultShellCompDirective494 break495 }496 }497498 if flag == nil {499 foundLocalNonPersistentFlag := false500 // If TraverseChildren is true on the root command we don't check for501 // local flags because we can use a local flag on a parent command
519 unmodified lines