Skip to content

Commit

Permalink
(#340) Improve required flag missing error, now show all missing flags
Browse files Browse the repository at this point in the history
Signed-off-by: R.I.Pienaar <[email protected]>
  • Loading branch information
ripienaar committed Oct 1, 2023
1 parent 5c565c1 commit be84374
Showing 1 changed file with 9 additions and 1 deletion.
10 changes: 9 additions & 1 deletion app.go
Original file line number Diff line number Diff line change
Expand Up @@ -715,15 +715,23 @@ func (a *Application) validateRequired(context *ParseContext) error {
}

// Check required flags and set defaults.
var neededFlags []string
for _, flag := range context.flags.long {
if flagElements[flag.name] == nil {
// Check required flags were provided.
if flag.needsValue() {
return fmt.Errorf("%w --%s not provided", ErrRequiredFlag, flag.name)
neededFlags = append(neededFlags, fmt.Sprintf("--%s", flag.name))
}
}
}

switch {
case len(neededFlags) == 1:
return fmt.Errorf("%w %s not provided", ErrRequiredFlag, neededFlags[0])
case len(neededFlags) > 1:
return fmt.Errorf("%ws %s not provided", ErrRequiredFlag, strings.Join(neededFlags, ", "))
}

for _, arg := range context.arguments.args {
if argElements[arg.name] == nil {
if arg.needsValue() {
Expand Down

0 comments on commit be84374

Please sign in to comment.