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
Provenance
- Commit
6dec1ae- Tree
8ee5091- Author
Harald Albers <albers@users.noreply.github.com>- Committer
GitHub <noreply@github.com>- Parent
c8289c1
3 files changed+102 added−0 removed
modified:site/content/completions/_index.md+30 added
303 unmodified lines304 $ helm status --output [tab][tab]305 json table yaml306 ```307 308+#### Change the default ShellCompDirective309+310+When no completion function is registered for a leaf command or for a flag, Cobra will311+automatically use `ShellCompDirectiveDefault`, which will invoke the shell's filename completion.312+This implies that when file completion does not apply to a leaf command or to a flag (the command313+or flag does not operate on a filename), turning off file completion requires you to register a314+completion function for that command/flag.315+For example:316+317+```go318+cmd.RegisterFlagCompletionFunc("flag-name", cobra.NoFileCompletions)319+```320+321+If you find that there are more situations where file completion should be turned off than322+when it is applicable, you can recursively change the default `ShellCompDirective` for a command323+and its subcommands to `ShellCompDirectiveNoFileComp`:324+325+```go326+cmd.CompletionOptions.SetDefaultShellCompDirective(ShellCompDirectiveNoFileComp)327+```328+329+If doing so, keep in mind that you should instead register a completion function for leaf commands or330+flags where file completion is applicable. For example:331+332+```go333+cmd.RegisterFlagCompletionFunc("flag-name", cobra.FixedCompletions(nil, ShellCompDirectiveDefault))334+```335+336+To change the default directive for the entire program, set the DefaultShellCompDirective on the root command.337+338 #### Debugging339 340 You can also easily debug your Go completion code for flags:341 ```bash275 unmodified linesmodified:completions_test.go+57 added
4013 unmodified lines4014 }4015 })4016 }4017 }4018+4019+func 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)4024+4025+ // 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)4030+4031+ rootCmd.AddCommand(childCmd1, childCmd2)4032+4033+ 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+ }4054+4055+ for _, tc := range testCases {4056+ t.Run(tc.desc, func(t *testing.T) {4057+ args := []string{ShellCompNoDescRequestCmd}4058+ args = append(args, tc.args...)4059+4060+ output, err := executeCommand(rootCmd, args...)4061+4062+ if err != nil {4063+ t.Errorf("Unexpected error: %v", err)4064+ }4065+4066+ outputWords := strings.Split(strings.TrimSpace(output), " ")4067+ directive := outputWords[len(outputWords)-1]4068+4069+ 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 lines114 // 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 }122 123+func (receiver *CompletionOptions) SetDefaultShellCompDirective(directive ShellCompDirective) {124+ receiver.DefaultShellCompDirective = &directive125+}126+127 // Completion is a string that can be used for completions128 //129 // two formats are supported:130 // - the completion choice355 unmodified lines486 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+ }497+498 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 command519 unmodified lines