MCPcopy
hub / github.com/spf13/cobra / TestCompletionForMutuallyExclusiveFlags

Function TestCompletionForMutuallyExclusiveFlags

completions_test.go:3255–3347  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

3253}
3254
3255func TestCompletionForMutuallyExclusiveFlags(t *testing.T) {
3256 getCmd := func() *Command {
3257 rootCmd := &Command{
3258 Use: "root",
3259 Run: emptyRun,
3260 }
3261 childCmd := &Command{
3262 Use: "child",
3263 ValidArgsFunction: func(cmd *Command, args []string, toComplete string) ([]string, ShellCompDirective) {
3264 return []string{"subArg"}, ShellCompDirectiveNoFileComp
3265 },
3266 Run: emptyRun,
3267 }
3268 rootCmd.AddCommand(childCmd)
3269
3270 rootCmd.PersistentFlags().IntSlice("ingroup1", []int{1}, "ingroup1")
3271 rootCmd.PersistentFlags().String("ingroup2", "", "ingroup2")
3272
3273 childCmd.Flags().Bool("ingroup3", false, "ingroup3")
3274 childCmd.Flags().Bool("nogroup", false, "nogroup")
3275
3276 // Add flags to a group
3277 childCmd.MarkFlagsMutuallyExclusive("ingroup1", "ingroup2", "ingroup3")
3278
3279 return rootCmd
3280 }
3281
3282 // Each test case uses a unique command from the function above.
3283 testcases := []struct {
3284 desc string
3285 args []string
3286 expectedOutput string
3287 }{
3288 {
3289 desc: "flags in mutually exclusive group not suggested without the - prefix",
3290 args: []string{"child", ""},
3291 expectedOutput: strings.Join([]string{
3292 "subArg",
3293 ":4",
3294 "Completion ended with directive: ShellCompDirectiveNoFileComp", ""}, "\n"),
3295 },
3296 {
3297 desc: "flags in mutually exclusive group suggested with the - prefix",
3298 args: []string{"child", "-"},
3299 expectedOutput: strings.Join([]string{
3300 "--ingroup1",
3301 "--ingroup2",
3302 "--help",
3303 "-h",
3304 "--ingroup3",
3305 "--nogroup",
3306 ":4",
3307 "Completion ended with directive: ShellCompDirectiveNoFileComp", ""}, "\n"),
3308 },
3309 {
3310 desc: "when flag in mutually exclusive group present, other flags in group not suggested even with the - prefix",
3311 args: []string{"child", "--ingroup1", "8", "-"},
3312 expectedOutput: strings.Join([]string{

Callers

nothing calls this directly

Calls 6

AddCommandMethod · 0.95
PersistentFlagsMethod · 0.95
FlagsMethod · 0.95
executeCommandFunction · 0.85
StringMethod · 0.80

Tested by

no test coverage detected