Skip to content

Inconsistent error handling between App.OnUsageError and App.Before during App.Run #707

@diliop

Description

@diliop

When OnUsageError is set for a cli.App instance, one has full control of what is printed to stderr when an error is encountered. That is not the case when setting Before.

Looking at how OnUsageError is handled in App.Run in app.go [c6eb2a0]:

if a.OnUsageError != nil {
	err := a.OnUsageError(context, err, false)
	a.handleExitCoder(context, err)
	return err
}

and for Before:

if a.Before != nil {
	beforeErr := a.Before(context)
	if beforeErr != nil {
		fmt.Fprintf(a.Writer, "%v\n\n", beforeErr)
		ShowAppHelp(context)
		a.handleExitCoder(context, beforeErr)
		err = beforeErr
		return err
	}
}

The issue I'm having is with the call to ShowAppHelp(..) when Before is set. Essentially the experience to the user is different when a usage error is encountered and I want to print custom output versus when I am checking for lets say flag value validity before running any subcommands. Ideally I would prefer the 2 if statements above to be identical and the default case to call ShowAppHelper(..) as is currently the case i.e. for Before:

if a.Before != nil {
	beforeErr := a.Before(context)
	if beforeErr != nil {
		a.handleExitCoder(context, beforeErr)
		err = beforeErr
		return err
	}
}

I've looked around to see if there is another way to modify this behavior but nothing obvious stands out.

Metadata

Metadata

Assignees

No one assigned

    Labels

    area/v2relates to / is being considered for v2help wantedplease help if you can!kind/bugdescribes or fixes a bugstatus/confirmedconfirmed to be valid, but work has yet to start

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions