Skip to content

Commit

Permalink
Merge pull request #1709 from nirhaas/nhaas/required_flag_with_alias_…
Browse files Browse the repository at this point in the history
…errors_with_actual_name

Fix missing required flag error uses flag name and not alias
  • Loading branch information
dearchap authored Mar 28, 2023
2 parents 0436998 + 69df7bf commit 31b7abb
Show file tree
Hide file tree
Showing 2 changed files with 18 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
15 changes: 15 additions & 0 deletions context_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -602,6 +602,21 @@ 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"}},
},
},
{
testCase: "required_flag_without_name_or_aliases_doesnt_error",
expectedAnError: false,
flags: []Flag{
&StringFlag{Required: true},
},
},
}

for _, test := range tdata {
Expand Down

0 comments on commit 31b7abb

Please sign in to comment.