diff --git a/command.go b/command.go index adb9465e00..be5fa1d88f 100644 --- a/command.go +++ b/command.go @@ -943,10 +943,15 @@ func (cmd *Command) checkRequiredFlag(f Flag) (bool, string) { flagName := "" for _, key := range f.Names() { - flagName = key + // use the first name to return since that is the + // primary flag name + if flagName == "" { + flagName = key + } if cmd.IsSet(strings.TrimSpace(key)) { flagPresent = true + break } } diff --git a/command_test.go b/command_test.go index 13e51c75ad..9c62daad97 100644 --- a/command_test.go +++ b/command_test.go @@ -3835,7 +3835,7 @@ func TestCheckRequiredFlags(t *testing.T) { expectedAnError: true, expectedErrorContents: []string{"Required flag \"names\" not set"}, flags: []Flag{ - &StringSliceFlag{Name: "names, n", Required: true}, + &StringSliceFlag{Name: "names", Aliases: []string{"n"}, Required: true}, }, }, { diff --git a/flag_bool_with_inverse_test.go b/flag_bool_with_inverse_test.go index 8f6b94af1f..ae18023ca0 100644 --- a/flag_bool_with_inverse_test.go +++ b/flag_bool_with_inverse_test.go @@ -309,7 +309,7 @@ func TestBoolWithInverseRequired(t *testing.T) { { toBeSet: false, value: false, - err: fmt.Errorf(`Required flag "env" not set`), + err: fmt.Errorf(`Required flag "no-env" not set`), }, { args: []string{"--env", "--no-env"},