Skip to content

Commit

Permalink
Merge pull request #890 from urfave/issue-878
Browse files Browse the repository at this point in the history
Fix #878
  • Loading branch information
asahasrabuddhe committed Sep 11, 2019
2 parents 388c2dd + fa858dc commit 61f3ae3
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 3 deletions.
17 changes: 14 additions & 3 deletions context.go
Original file line number Diff line number Diff line change
Expand Up @@ -313,9 +313,20 @@ func checkRequiredFlags(flags []Flag, context *Context) requiredFlagsErr {
var missingFlags []string
for _, f := range flags {
if rf, ok := f.(RequiredFlag); ok && rf.IsRequired() {
key := strings.Split(f.GetName(), ",")[0]
if !context.IsSet(key) {
missingFlags = append(missingFlags, key)
var flagPresent bool
var flagName string
for _, key := range strings.Split(f.GetName(), ",") {
if len(key) > 1 {
flagName = key
}

if context.IsSet(strings.TrimSpace(key)) {
flagPresent = true
}
}

if !flagPresent && flagName != "" {
missingFlags = append(missingFlags, flagName)
}
}
}
Expand Down
14 changes: 14 additions & 0 deletions context_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -517,6 +517,20 @@ func TestCheckRequiredFlags(t *testing.T) {
},
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"},
},
{
testCase: "required_flag_with_multiple_short_names",
flags: []Flag{
StringSliceFlag{Name: "names, N, n", Required: true},
},
parseInput: []string{"-n", "asd", "-n", "qwe"},
},
}
for _, test := range tdata {
t.Run(test.testCase, func(t *testing.T) {
Expand Down

0 comments on commit 61f3ae3

Please sign in to comment.