Skip to content

Commit

Permalink
Fix:(issue_1838) Use primary name for required flag error
Browse files Browse the repository at this point in the history
  • Loading branch information
dearchap committed Dec 14, 2023
1 parent 5e65616 commit 581676f
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 3 deletions.
7 changes: 6 additions & 1 deletion command.go
Original file line number Diff line number Diff line change
Expand Up @@ -943,10 +943,15 @@ func (cmd *Command) checkRequiredFlag(f Flag) (bool, string) {
flagName := ""

for _, key := range f.Names() {
flagName = key
// use the first name to return since that is the
// primary flag name
if flagName == "" {
flagName = key
}

if cmd.IsSet(strings.TrimSpace(key)) {
flagPresent = true
break
}
}

Expand Down
2 changes: 1 addition & 1 deletion command_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3835,7 +3835,7 @@ func TestCheckRequiredFlags(t *testing.T) {
expectedAnError: true,
expectedErrorContents: []string{"Required flag \"names\" not set"},
flags: []Flag{
&StringSliceFlag{Name: "names, n", Required: true},
&StringSliceFlag{Name: "names", Aliases: []string{"n"}, Required: true},
},
},
{
Expand Down
2 changes: 1 addition & 1 deletion flag_bool_with_inverse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -309,7 +309,7 @@ func TestBoolWithInverseRequired(t *testing.T) {
{
toBeSet: false,
value: false,
err: fmt.Errorf(`Required flag "env" not set`),
err: fmt.Errorf(`Required flag "no-env" not set`),
},
{
args: []string{"--env", "--no-env"},
Expand Down

0 comments on commit 581676f

Please sign in to comment.