From f842187ebb32ee7d5109783d02fe9902b68ee54e Mon Sep 17 00:00:00 2001 From: Ole Petter Date: Sat, 2 May 2020 19:45:17 +0200 Subject: [PATCH 1/2] test(context): Added regression test for requiredFlagsError This adds a test verifying that the requiredFlagsError does contain the long option of the missing flag, instead of the short option and a space, which was the old behaviour. Signed-off-by: Ole Petter --- context_test.go | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/context_test.go b/context_test.go index a9f9312328..74f655a8c0 100644 --- a/context_test.go +++ b/context_test.go @@ -576,6 +576,14 @@ func TestCheckRequiredFlags(t *testing.T) { }, parseInput: []string{"-n", "asd", "-n", "qwe"}, }, + { + testCase: "required_flag_with_short_alias_not_printed_on_error", + expectedAnError: true, + expectedErrorContents: []string{"Required flag \"names\" not set"}, + flags: []Flag{ + StringSliceFlag{Name: "names, n", Required: true}, + }, + }, } for _, test := range tdata { t.Run(test.testCase, func(t *testing.T) { From ee2fae6dcecc3f768919d04688c2c92a37f8d1ab Mon Sep 17 00:00:00 2001 From: Ole Petter Date: Fri, 1 May 2020 17:43:49 +0200 Subject: [PATCH 2/2] fix: Print the long options of the required flags missing The error message collected by 'checkRequiredFlags()' now sets the name of the flags to the long option given in the 'Name: ' field. This change is introduced to fix up an error where printing the error in the case of a missing required flag looks like: ``` Required flags " n, t" not set ``` Whilst the expected behaviour is: ``` Required flags ", " not set ``` Signed-off-by: Ole Petter --- context.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/context.go b/context.go index 957f39e0f4..3adf37e7b2 100644 --- a/context.go +++ b/context.go @@ -324,11 +324,12 @@ func checkRequiredFlags(flags []Flag, context *Context) requiredFlagsErr { var flagPresent bool var flagName string for _, key := range strings.Split(f.GetName(), ",") { + key = strings.TrimSpace(key) if len(key) > 1 { flagName = key } - if context.IsSet(strings.TrimSpace(key)) { + if context.IsSet(key) { flagPresent = true } }