@@ -43,13 +43,15 @@ func TestValidateFlagGroups(t *testing.T) {
43
43
44
44
// Each test case uses a unique command from the function above.
45
45
testcases := []struct {
46
- desc string
47
- flagGroupsRequired []string
48
- flagGroupsExclusive []string
49
- subCmdFlagGroupsRequired []string
50
- subCmdFlagGroupsExclusive []string
51
- args []string
52
- expectErr string
46
+ desc string
47
+ flagGroupsRequired []string
48
+ flagGroupsOneRequired []string
49
+ flagGroupsExclusive []string
50
+ subCmdFlagGroupsRequired []string
51
+ subCmdFlagGroupsOneRequired []string
52
+ subCmdFlagGroupsExclusive []string
53
+ args []string
54
+ expectErr string
53
55
}{
54
56
{
55
57
desc : "No flags no problem" ,
@@ -62,6 +64,11 @@ func TestValidateFlagGroups(t *testing.T) {
62
64
flagGroupsRequired : []string {"a b c" },
63
65
args : []string {"--a=foo" },
64
66
expectErr : "if any flags in the group [a b c] are set they must all be set; missing [b c]" ,
67
+ }, {
68
+ desc : "One-required flag group not satisfied" ,
69
+ flagGroupsOneRequired : []string {"a b" },
70
+ args : []string {"--c=foo" },
71
+ expectErr : "at least one of the flags in the group [a b] is required" ,
65
72
}, {
66
73
desc : "Exclusive flag group not satisfied" ,
67
74
flagGroupsExclusive : []string {"a b c" },
@@ -72,6 +79,11 @@ func TestValidateFlagGroups(t *testing.T) {
72
79
flagGroupsRequired : []string {"a b c" , "a d" },
73
80
args : []string {"--c=foo" , "--d=foo" },
74
81
expectErr : `if any flags in the group [a b c] are set they must all be set; missing [a b]` ,
82
+ }, {
83
+ desc : "Multiple one-required flag group not satisfied returns first error" ,
84
+ flagGroupsOneRequired : []string {"a b" , "d e" },
85
+ args : []string {"--c=foo" , "--f=foo" },
86
+ expectErr : `at least one of the flags in the group [a b] is required` ,
75
87
}, {
76
88
desc : "Multiple exclusive flag group not satisfied returns first error" ,
77
89
flagGroupsExclusive : []string {"a b c" , "a d" },
@@ -82,32 +94,57 @@ func TestValidateFlagGroups(t *testing.T) {
82
94
flagGroupsRequired : []string {"a d" , "a b" , "a c" },
83
95
args : []string {"--a=foo" },
84
96
expectErr : `if any flags in the group [a b] are set they must all be set; missing [b]` ,
97
+ }, {
98
+ desc : "Validation of one-required groups occurs on groups in sorted order" ,
99
+ flagGroupsOneRequired : []string {"d e" , "a b" , "f g" },
100
+ args : []string {"--c=foo" },
101
+ expectErr : `at least one of the flags in the group [a b] is required` ,
85
102
}, {
86
103
desc : "Validation of exclusive groups occurs on groups in sorted order" ,
87
104
flagGroupsExclusive : []string {"a d" , "a b" , "a c" },
88
105
args : []string {"--a=foo" , "--b=foo" , "--c=foo" },
89
106
expectErr : `if any flags in the group [a b] are set none of the others can be; [a b] were all set` ,
90
107
}, {
91
- desc : "Persistent flags utilize both features and can fail required groups" ,
108
+ desc : "Persistent flags utilize required and exclusive groups and can fail required groups" ,
92
109
flagGroupsRequired : []string {"a e" , "e f" },
93
110
flagGroupsExclusive : []string {"f g" },
94
111
args : []string {"--a=foo" , "--f=foo" , "--g=foo" },
95
112
expectErr : `if any flags in the group [a e] are set they must all be set; missing [e]` ,
96
113
}, {
97
- desc : "Persistent flags utilize both features and can fail mutually exclusive groups" ,
114
+ desc : "Persistent flags utilize one-required and exclusive groups and can fail one-required groups" ,
115
+ flagGroupsOneRequired : []string {"a b" , "e f" },
116
+ flagGroupsExclusive : []string {"e f" },
117
+ args : []string {"--e=foo" },
118
+ expectErr : `at least one of the flags in the group [a b] is required` ,
119
+ }, {
120
+ desc : "Persistent flags utilize required and exclusive groups and can fail mutually exclusive groups" ,
98
121
flagGroupsRequired : []string {"a e" , "e f" },
99
122
flagGroupsExclusive : []string {"f g" },
100
123
args : []string {"--a=foo" , "--e=foo" , "--f=foo" , "--g=foo" },
101
124
expectErr : `if any flags in the group [f g] are set none of the others can be; [f g] were all set` ,
102
125
}, {
103
- desc : "Persistent flags utilize both features and can pass" ,
126
+ desc : "Persistent flags utilize required and exclusive groups and can pass" ,
104
127
flagGroupsRequired : []string {"a e" , "e f" },
105
128
flagGroupsExclusive : []string {"f g" },
106
129
args : []string {"--a=foo" , "--e=foo" , "--f=foo" },
130
+ }, {
131
+ desc : "Persistent flags utilize one-required and exclusive groups and can pass" ,
132
+ flagGroupsOneRequired : []string {"a e" , "e f" },
133
+ flagGroupsExclusive : []string {"f g" },
134
+ args : []string {"--a=foo" , "--e=foo" , "--f=foo" },
107
135
}, {
108
136
desc : "Subcmds can use required groups using inherited flags" ,
109
137
subCmdFlagGroupsRequired : []string {"e subonly" },
110
138
args : []string {"subcmd" , "--e=foo" , "--subonly=foo" },
139
+ }, {
140
+ desc : "Subcmds can use one-required groups using inherited flags" ,
141
+ subCmdFlagGroupsOneRequired : []string {"e subonly" },
142
+ args : []string {"subcmd" , "--e=foo" , "--subonly=foo" },
143
+ }, {
144
+ desc : "Subcmds can use one-required groups using inherited flags and fail one-required groups" ,
145
+ subCmdFlagGroupsOneRequired : []string {"e subonly" },
146
+ args : []string {"subcmd" },
147
+ expectErr : "at least one of the flags in the group [e subonly] is required" ,
111
148
}, {
112
149
desc : "Subcmds can use exclusive groups using inherited flags" ,
113
150
subCmdFlagGroupsExclusive : []string {"e subonly" },
@@ -130,12 +167,18 @@ func TestValidateFlagGroups(t *testing.T) {
130
167
for _ , flagGroup := range tc .flagGroupsRequired {
131
168
c .MarkFlagsRequiredTogether (strings .Split (flagGroup , " " )... )
132
169
}
170
+ for _ , flagGroup := range tc .flagGroupsOneRequired {
171
+ c .MarkFlagsOneRequired (strings .Split (flagGroup , " " )... )
172
+ }
133
173
for _ , flagGroup := range tc .flagGroupsExclusive {
134
174
c .MarkFlagsMutuallyExclusive (strings .Split (flagGroup , " " )... )
135
175
}
136
176
for _ , flagGroup := range tc .subCmdFlagGroupsRequired {
137
177
sub .MarkFlagsRequiredTogether (strings .Split (flagGroup , " " )... )
138
178
}
179
+ for _ , flagGroup := range tc .subCmdFlagGroupsOneRequired {
180
+ sub .MarkFlagsOneRequired (strings .Split (flagGroup , " " )... )
181
+ }
139
182
for _ , flagGroup := range tc .subCmdFlagGroupsExclusive {
140
183
sub .MarkFlagsMutuallyExclusive (strings .Split (flagGroup , " " )... )
141
184
}
0 commit comments