Skip to content

Commit 2f492f5

Browse files
authored
Merge pull request #1839 from dearchap/issue_1838
Fix:(issue_1838) Use primary name for required flag error
2 parents 5e65616 + 581676f commit 2f492f5

File tree

3 files changed

+8
-3
lines changed

3 files changed

+8
-3
lines changed

command.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -943,10 +943,15 @@ func (cmd *Command) checkRequiredFlag(f Flag) (bool, string) {
943943
flagName := ""
944944

945945
for _, key := range f.Names() {
946-
flagName = key
946+
// use the first name to return since that is the
947+
// primary flag name
948+
if flagName == "" {
949+
flagName = key
950+
}
947951

948952
if cmd.IsSet(strings.TrimSpace(key)) {
949953
flagPresent = true
954+
break
950955
}
951956
}
952957

command_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3835,7 +3835,7 @@ func TestCheckRequiredFlags(t *testing.T) {
38353835
expectedAnError: true,
38363836
expectedErrorContents: []string{"Required flag \"names\" not set"},
38373837
flags: []Flag{
3838-
&StringSliceFlag{Name: "names, n", Required: true},
3838+
&StringSliceFlag{Name: "names", Aliases: []string{"n"}, Required: true},
38393839
},
38403840
},
38413841
{

flag_bool_with_inverse_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -309,7 +309,7 @@ func TestBoolWithInverseRequired(t *testing.T) {
309309
{
310310
toBeSet: false,
311311
value: false,
312-
err: fmt.Errorf(`Required flag "env" not set`),
312+
err: fmt.Errorf(`Required flag "no-env" not set`),
313313
},
314314
{
315315
args: []string{"--env", "--no-env"},

0 commit comments

Comments
 (0)