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

Function TestCompletionForGroupedFlags

completions_test.go:3057–3155  ·  completions_test.go::TestCompletionForGroupedFlags
(t *testing.T)

Source from the content-addressed store, hash-verified

3055}
3056
3057func TestCompletionForGroupedFlags(t *testing.T) {
3058 getCmd := func() *Command {
3059 rootCmd := &Command{
3060 Use: "root",
3061 Run: emptyRun,
3062 }
3063 childCmd := &Command{
3064 Use: "child",
3065 ValidArgsFunction: func(cmd *Command, args []string, toComplete string) ([]string, ShellCompDirective) {
3066 return []string{"subArg"}, ShellCompDirectiveNoFileComp
3067 },
3068 Run: emptyRun,
3069 }
3070 rootCmd.AddCommand(childCmd)
3071
3072 rootCmd.PersistentFlags().Int("ingroup1", -1, "ingroup1")
3073 rootCmd.PersistentFlags().String("ingroup2", "", "ingroup2")
3074
3075 childCmd.Flags().Bool("ingroup3", false, "ingroup3")
3076 childCmd.Flags().Bool("nogroup", false, "nogroup")
3077
3078 // Add flags to a group
3079 childCmd.MarkFlagsRequiredTogether("ingroup1", "ingroup2", "ingroup3")
3080
3081 return rootCmd
3082 }
3083
3084 // Each test case uses a unique command from the function above.
3085 testcases := []struct {
3086 desc string
3087 args []string
3088 expectedOutput string
3089 }{
3090 {
3091 desc: "flags in group not suggested without - prefix",
3092 args: []string{"child", ""},
3093 expectedOutput: strings.Join([]string{
3094 "subArg",
3095 ":4",
3096 "Completion ended with directive: ShellCompDirectiveNoFileComp", ""}, "\n"),
3097 },
3098 {
3099 desc: "flags in group suggested with - prefix",
3100 args: []string{"child", "-"},
3101 expectedOutput: strings.Join([]string{
3102 "--ingroup1",
3103 "--ingroup2",
3104 "--help",
3105 "-h",
3106 "--ingroup3",
3107 "--nogroup",
3108 ":4",
3109 "Completion ended with directive: ShellCompDirectiveNoFileComp", ""}, "\n"),
3110 },
3111 {
3112 desc: "when flag in group present, other flags in group suggested even without - prefix",
3113 args: []string{"child", "--ingroup2", "value", ""},
3114 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