Skip to content
Myr

Don't complete --help flag when flag parsing disabled (#2061)

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

Fixes #2060 When a command sets `DisableFlagParsing = true` it requests the responsibility of doing all the flag parsing. Therefore even the `--help/-f/--version/-v` flags should not be automatically completed by Cobra in such a case. Without this change the `--help/-h/--version/-v` flags can end up being completed twice for plugins: one time from cobra and one time from the plugin (which has set `DisableFlagParsing = true`).

authored by Marc Khouzam · committed by GitHub · parent 8b1eba4 · Browse files · 2 files+55 added−6 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@gmail.com>

Diff stats

2 files+55 added−6 removed

  • other:+55 added−6 removed

2 files+55 added−6 removed

modified:completions_test.go+43 added-3 removed
2620 unmodified lines
26212622 expected := strings.Join([]string{2623 "--persistent",2624 "-p",2625 "--help",2626 "-h",2625 "--nonPersistent",2626 "-n",2627 "--flag",2628 "-f",
421 unmodified lines
3050 ValidArgsFunction: func(cmd *Command, args []string, toComplete string) ([]string, ShellCompDirective) {3051 return []string{"extra3"}, ShellCompDirectiveNoFileComp3052 },3053 }3054 childCmd4 := &Command{3055 Use: "child4",3056 Version: "1.1.1",3057 Run: emptyRun,3058 ValidArgsFunction: func(cmd *Command, args []string, toComplete string) ([]string, ShellCompDirective) {3059 return []string{"extra4"}, ShellCompDirectiveNoFileComp3060 },3061 DisableFlagParsing: true,3062 }3063 childCmd5 := &Command{3064 Use: "child5",3065 Version: "1.1.1",3066 Run: emptyRun,3067 ValidArgsFunction: func(cmd *Command, args []string, toComplete string) ([]string, ShellCompDirective) {3068 return []string{"extra5"}, ShellCompDirectiveNoFileComp3069 },3070 DisableFlagParsing: true,3071 }30723057 rootCmd.AddCommand(childCmd, childCmd2, childCmd3)3073 rootCmd.AddCommand(childCmd, childCmd2, childCmd3, childCmd4, childCmd5)30743075 _ = childCmd.Flags().Bool("bool", false, "A bool flag")3076 _ = childCmd.MarkFlagRequired("bool")3077
3 unmodified lines
30813082 // Have a command that only adds its own -v flag3083 _ = childCmd3.Flags().BoolP("verbose", "v", false, "Not a version flag")30843085 // Have a command that DisablesFlagParsing but that also adds its own help and version flags3086 _ = childCmd5.Flags().BoolP("help", "h", false, "My own help")3087 _ = childCmd5.Flags().BoolP("version", "v", false, "My own version")30883089 return rootCmd3090 }30913092 // Each test case uses a unique command from the function above.
122 unmodified lines
3215 "extra3",3216 ":4",3217 "Completion ended with directive: ShellCompDirectiveNoFileComp", ""}, "\n"),3218 },3219 {3220 desc: "no completion for --help/-h and --version/-v flags when DisableFlagParsing=true",3221 args: []string{"child4", "-"},3222 expectedOutput: strings.Join([]string{3223 "extra4",3224 ":4",3225 "Completion ended with directive: ShellCompDirectiveNoFileComp", ""}, "\n"),3226 },3227 {3228 desc: "completions for program-defined --help/-h and --version/-v flags even when DisableFlagParsing=true",3229 args: []string{"child5", "-"},3230 expectedOutput: strings.Join([]string{3231 "--help",3232 "-h",3233 "--version",3234 "-v",3235 "extra5",3236 ":4",3237 "Completion ended with directive: ShellCompDirectiveNoFileComp", ""}, "\n"),3238 },3239 }32403241 for _, tc := range testcases {3242 t.Run(tc.desc, func(t *testing.T) {
77 unmodified lines
modified:completions.go+12 added-3 removed
300 unmodified lines
301 finalCmd.ctx = c.ctx302303 // These flags are normally added when `execute()` is called on `finalCmd`,304 // however, when doing completion, we don't call `finalCmd.execute()`.305 // Let's add the --help and --version flag ourselves.306 finalCmd.InitDefaultHelpFlag()307 finalCmd.InitDefaultVersionFlag()305 // Let's add the --help and --version flag ourselves but only if the finalCmd306 // has not disabled flag parsing; if flag parsing is disabled, it is up to the307 // finalCmd itself to handle the completion of *all* flags.308 if !finalCmd.DisableFlagParsing {309 finalCmd.InitDefaultHelpFlag()310 finalCmd.InitDefaultVersionFlag()311 }312313 // Check if we are doing flag value completion before parsing the flags.314 // This is important because if we are completing a flag value, we need to also315 // remove the flag name argument from the list of finalArgs or else the parsing
95 unmodified lines
411 // non-inherited flags.412 finalCmd.InheritedFlags().VisitAll(func(flag *pflag.Flag) {413 doCompleteFlags(flag)414 })415 // Try to complete non-inherited flags even if DisableFlagParsing==true.416 // This allows programs to tell Cobra about flags for completion even417 // if the actual parsing of flags is not done by Cobra.418 // For instance, Helm uses this to provide flag name completion for419 // some of its plugins.420 finalCmd.NonInheritedFlags().VisitAll(func(flag *pflag.Flag) {421 doCompleteFlags(flag)422 })423 }
483 unmodified lines