Improve Code and Add Test Case

main
Ajitem Sahasrabuddhe 5 years ago
parent 7d6a604106
commit cbb9e015b8
No known key found for this signature in database
GPG Key ID: 782DEBC01D3967A5

@ -314,14 +314,16 @@ func checkRequiredFlags(flags []Flag, context *Context) requiredFlagsErr {
for _, f := range flags { for _, f := range flags {
if rf, ok := f.(RequiredFlag); ok && rf.IsRequired() { if rf, ok := f.(RequiredFlag); ok && rf.IsRequired() {
key := strings.Split(f.GetName(), ",") key := strings.Split(f.GetName(), ",")
shortName := strings.TrimSpace(key[0])
if len(key) > 1 { if len(key) > 1 {
// has short name // has short name
if !context.IsSet(strings.TrimSpace(key[0])) && !context.IsSet(strings.TrimSpace(key[1])) { longName := strings.TrimSpace(key[1])
if !context.IsSet(shortName) && !context.IsSet(longName) {
missingFlags = append(missingFlags, key[0]) missingFlags = append(missingFlags, key[0])
} }
} else { } else {
// does not have short name // does not have short name
if !context.IsSet(strings.TrimSpace(key[0])) { if !context.IsSet(shortName) {
missingFlags = append(missingFlags, key[0]) missingFlags = append(missingFlags, key[0])
} }
} }

@ -517,6 +517,13 @@ func TestCheckRequiredFlags(t *testing.T) {
}, },
parseInput: []string{"--requiredFlag", "myinput", "--requiredFlagTwo", "myinput"}, parseInput: []string{"--requiredFlag", "myinput", "--requiredFlagTwo", "myinput"},
}, },
{
testCase: "required_flag_with_short_name",
flags: []Flag{
StringSliceFlag{Name: "names, N", Required: true},
},
parseInput: []string{"-N", "asd", "-N", "qwe"},
},
} }
for _, test := range tdata { for _, test := range tdata {
t.Run(test.testCase, func(t *testing.T) { t.Run(test.testCase, func(t *testing.T) {

Loading…
Cancel
Save