Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix #878 #890

Merged
merged 8 commits into from
Sep 11, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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)) {
asahasrabuddhe marked this conversation as resolved.
Show resolved Hide resolved
flagPresent = true
}
asahasrabuddhe marked this conversation as resolved.
Show resolved Hide resolved
}

if !flagPresent && flagName != "" {
missingFlags = append(missingFlags, flagName)
asahasrabuddhe marked this conversation as resolved.
Show resolved Hide resolved
}
}
}
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