From b98c059269904304ad1b8542a2800cccc3171b30 Mon Sep 17 00:00:00 2001 From: Gerrard-YNWA Date: Mon, 29 Aug 2022 19:25:55 +0800 Subject: [PATCH] fix: allow required flag with one character (#1449) * fix: allow required flag with one character * fix: update test case --- context.go | 4 +--- context_test.go | 8 ++++++++ 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/context.go b/context.go index 6b497ed20d..3a78c267d8 100644 --- a/context.go +++ b/context.go @@ -170,9 +170,7 @@ func (cCtx *Context) checkRequiredFlags(flags []Flag) requiredFlagsErr { var flagName string for _, key := range f.Names() { - if len(key) > 1 { - flagName = key - } + flagName = key if cCtx.IsSet(strings.TrimSpace(key)) { flagPresent = true diff --git a/context_test.go b/context_test.go index 84757063eb..55a9ead891 100644 --- a/context_test.go +++ b/context_test.go @@ -556,6 +556,14 @@ func TestCheckRequiredFlags(t *testing.T) { &StringSliceFlag{Name: "names, n", Required: true}, }, }, + { + testCase: "required_flag_with_one_character", + expectedAnError: true, + expectedErrorContents: []string{"Required flag \"n\" not set"}, + flags: []Flag{ + &StringFlag{Name: "n", Required: true}, + }, + }, } for _, test := range tdata {