From 7d6a604106e44732edc0a76f4a4800c8c27ddfbe Mon Sep 17 00:00:00 2001 From: Ajitem Sahasrabuddhe Date: Wed, 11 Sep 2019 08:59:51 +0530 Subject: [PATCH 1/6] Fix #878 --- context.go | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/context.go b/context.go index db7cd69..485f529 100644 --- a/context.go +++ b/context.go @@ -313,9 +313,17 @@ func checkRequiredFlags(flags []Flag, context *Context) requiredFlagsErr { var missingFlags []string for _, f := range flags { if rf, ok := f.(RequiredFlag); ok && rf.IsRequired() { - key := strings.Split(f.GetName(), ",")[0] - if !context.IsSet(key) { - missingFlags = append(missingFlags, key) + key := strings.Split(f.GetName(), ",") + if len(key) > 1 { + // has short name + if !context.IsSet(strings.TrimSpace(key[0])) && !context.IsSet(strings.TrimSpace(key[1])) { + missingFlags = append(missingFlags, key[0]) + } + } else { + // does not have short name + if !context.IsSet(strings.TrimSpace(key[0])) { + missingFlags = append(missingFlags, key[0]) + } } } } From cbb9e015b89225aa090c41085bdb0933f6290d96 Mon Sep 17 00:00:00 2001 From: Ajitem Sahasrabuddhe Date: Wed, 11 Sep 2019 09:21:45 +0530 Subject: [PATCH 2/6] Improve Code and Add Test Case --- context.go | 6 ++++-- context_test.go | 7 +++++++ 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/context.go b/context.go index 485f529..8f1dcd8 100644 --- a/context.go +++ b/context.go @@ -314,14 +314,16 @@ func checkRequiredFlags(flags []Flag, context *Context) requiredFlagsErr { for _, f := range flags { if rf, ok := f.(RequiredFlag); ok && rf.IsRequired() { key := strings.Split(f.GetName(), ",") + shortName := strings.TrimSpace(key[0]) if len(key) > 1 { // has short name - if !context.IsSet(strings.TrimSpace(key[0])) && !context.IsSet(strings.TrimSpace(key[1])) { + longName := strings.TrimSpace(key[1]) + if !context.IsSet(shortName) && !context.IsSet(longName) { missingFlags = append(missingFlags, key[0]) } } else { // does not have short name - if !context.IsSet(strings.TrimSpace(key[0])) { + if !context.IsSet(shortName) { missingFlags = append(missingFlags, key[0]) } } diff --git a/context_test.go b/context_test.go index 9e594dd..1d52921 100644 --- a/context_test.go +++ b/context_test.go @@ -517,6 +517,13 @@ func TestCheckRequiredFlags(t *testing.T) { }, parseInput: []string{"--requiredFlag", "myinput", "--requiredFlagTwo", "myinput"}, }, + { + testCase: "required_flag_with_short_name", + flags: []Flag{ + StringSliceFlag{Name: "names, N", Required: true}, + }, + parseInput: []string{"-N", "asd", "-N", "qwe"}, + }, } for _, test := range tdata { t.Run(test.testCase, func(t *testing.T) { From 1547ac2f6a3d3d39fe4d49570c0d1c2401a8f20e Mon Sep 17 00:00:00 2001 From: Ajitem Sahasrabuddhe Date: Wed, 11 Sep 2019 14:15:20 +0530 Subject: [PATCH 3/6] Modify variable names --- context.go | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/context.go b/context.go index 8f1dcd8..485f529 100644 --- a/context.go +++ b/context.go @@ -314,16 +314,14 @@ func checkRequiredFlags(flags []Flag, context *Context) requiredFlagsErr { for _, f := range flags { if rf, ok := f.(RequiredFlag); ok && rf.IsRequired() { key := strings.Split(f.GetName(), ",") - shortName := strings.TrimSpace(key[0]) if len(key) > 1 { // has short name - longName := strings.TrimSpace(key[1]) - if !context.IsSet(shortName) && !context.IsSet(longName) { + if !context.IsSet(strings.TrimSpace(key[0])) && !context.IsSet(strings.TrimSpace(key[1])) { missingFlags = append(missingFlags, key[0]) } } else { // does not have short name - if !context.IsSet(shortName) { + if !context.IsSet(strings.TrimSpace(key[0])) { missingFlags = append(missingFlags, key[0]) } } From c6ee3b4904ed76d34f277c315c2097ae7b22d38f Mon Sep 17 00:00:00 2001 From: Ajitem Sahasrabuddhe Date: Wed, 11 Sep 2019 14:34:41 +0530 Subject: [PATCH 4/6] Use iterative logic to determine missing flag --- context.go | 21 ++++++++++++--------- context_test.go | 7 +++++++ 2 files changed, 19 insertions(+), 9 deletions(-) diff --git a/context.go b/context.go index 485f529..2f18f3f 100644 --- a/context.go +++ b/context.go @@ -313,18 +313,21 @@ func checkRequiredFlags(flags []Flag, context *Context) requiredFlagsErr { var missingFlags []string for _, f := range flags { if rf, ok := f.(RequiredFlag); ok && rf.IsRequired() { - key := strings.Split(f.GetName(), ",") - if len(key) > 1 { - // has short name - if !context.IsSet(strings.TrimSpace(key[0])) && !context.IsSet(strings.TrimSpace(key[1])) { - missingFlags = append(missingFlags, key[0]) + var flagPresent bool + var flagName string + for _, key := range strings.Split(f.GetName(), ",") { + if len(key) > 1 { + flagName = key } - } else { - // does not have short name - if !context.IsSet(strings.TrimSpace(key[0])) { - missingFlags = append(missingFlags, key[0]) + + if context.IsSet(strings.TrimSpace(key)) { + flagPresent = true } } + + if !flagPresent { + missingFlags = append(missingFlags, flagName) + } } } diff --git a/context_test.go b/context_test.go index 1d52921..13c3701 100644 --- a/context_test.go +++ b/context_test.go @@ -524,6 +524,13 @@ func TestCheckRequiredFlags(t *testing.T) { }, parseInput: []string{"-N", "asd", "-N", "qwe"}, }, + { + testCase: "required_flag_with_short_name", + flags: []Flag{ + StringSliceFlag{Name: "names, N, n", Required: true}, + }, + parseInput: []string{"-n", "asd", "-n", "qwe"}, + }, } for _, test := range tdata { t.Run(test.testCase, func(t *testing.T) { From f8bb66ae7d679973cf9b3f6f8c3dc6933404a31a Mon Sep 17 00:00:00 2001 From: Ajitem Sahasrabuddhe Date: Wed, 11 Sep 2019 14:42:38 +0530 Subject: [PATCH 5/6] Fix Typo --- context_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/context_test.go b/context_test.go index 13c3701..28f5e08 100644 --- a/context_test.go +++ b/context_test.go @@ -525,7 +525,7 @@ func TestCheckRequiredFlags(t *testing.T) { parseInput: []string{"-N", "asd", "-N", "qwe"}, }, { - testCase: "required_flag_with_short_name", + testCase: "required_flag_with_multiple_short_names", flags: []Flag{ StringSliceFlag{Name: "names, N, n", Required: true}, }, From fa858dcc260fb07c25aab13650d9fa0e64f851c7 Mon Sep 17 00:00:00 2001 From: Ajitem Sahasrabuddhe Date: Wed, 11 Sep 2019 15:10:14 +0530 Subject: [PATCH 6/6] Ensure flag is not blank --- context.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/context.go b/context.go index 2f18f3f..ecfc032 100644 --- a/context.go +++ b/context.go @@ -325,7 +325,7 @@ func checkRequiredFlags(flags []Flag, context *Context) requiredFlagsErr { } } - if !flagPresent { + if !flagPresent && flagName != "" { missingFlags = append(missingFlags, flagName) } }