Skip to content

Commit

Permalink
fix: missing required flag error uses flag name and not alias
Browse files Browse the repository at this point in the history
  • Loading branch information
nirhaas committed Mar 20, 2023
1 parent 560c87b commit a7e3459
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 2 deletions.
5 changes: 3 additions & 2 deletions context.go
Original file line number Diff line number Diff line change
Expand Up @@ -204,9 +204,10 @@ func (cCtx *Context) checkRequiredFlags(flags []Flag) requiredFlagsErr {
var flagPresent bool
var flagName string

for _, key := range f.Names() {
flagName = key
flagNames := f.Names()
flagName = flagNames[0]

for _, key := range flagNames {
if cCtx.IsSet(strings.TrimSpace(key)) {
flagPresent = true
}
Expand Down
8 changes: 8 additions & 0 deletions context_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -602,6 +602,14 @@ func TestCheckRequiredFlags(t *testing.T) {
&StringFlag{Name: "n", Required: true},
},
},
{
testCase: "required_flag_with_alias_errors_with_actual_name",
expectedAnError: true,
expectedErrorContents: []string{"Required flag \"collection\" not set"},
flags: []Flag{
&StringFlag{Name: "collection", Required: true, Aliases: []string{"c"}},
},
},
}

for _, test := range tdata {
Expand Down

0 comments on commit a7e3459

Please sign in to comment.