diff --git a/context.go b/context.go index dbf50e495f..cf5e58fe77 100644 --- a/context.go +++ b/context.go @@ -204,9 +204,10 @@ func (cCtx *Context) checkRequiredFlags(flags []Flag) requiredFlagsErr { var flagPresent bool var flagName string - for _, key := range f.Names() { - flagName = key + flagNames := f.Names() + flagName = flagNames[0] + for _, key := range flagNames { if cCtx.IsSet(strings.TrimSpace(key)) { flagPresent = true } diff --git a/context_test.go b/context_test.go index 246590d339..cdb208c2c4 100644 --- a/context_test.go +++ b/context_test.go @@ -602,6 +602,21 @@ func TestCheckRequiredFlags(t *testing.T) { &StringFlag{Name: "n", Required: true}, }, }, + { + testCase: "required_flag_with_alias_errors_with_actual_name", + expectedAnError: true, + expectedErrorContents: []string{"Required flag \"collection\" not set"}, + flags: []Flag{ + &StringFlag{Name: "collection", Required: true, Aliases: []string{"c"}}, + }, + }, + { + testCase: "required_flag_without_name_or_aliases_doesnt_error", + expectedAnError: false, + flags: []Flag{ + &StringFlag{Required: true}, + }, + }, } for _, test := range tdata {