From a7e3459dba8413c3c5e61a4c98af8f669f394931 Mon Sep 17 00:00:00 2001 From: Nir Haas Date: Mon, 20 Mar 2023 15:12:43 +0200 Subject: [PATCH] fix: missing required flag error uses flag name and not alias --- context.go | 5 +++-- context_test.go | 8 ++++++++ 2 files changed, 11 insertions(+), 2 deletions(-) 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..4dc5b6d377 100644 --- a/context_test.go +++ b/context_test.go @@ -602,6 +602,14 @@ 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"}}, + }, + }, } for _, test := range tdata {