Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow Ignoring Usage Errors #656

Closed
wants to merge 2 commits into from

Conversation

grubernaut
Copy link
Contributor

In some rare cases, it's handy to allow the application to handle
use-case errors in other ways, without requiring an error to be
returned. This allows the application written to handle the usage
error in other ways. Or, allow the application to pass the flags
on to subsequent child processes regardless of the host application's
flag parsing.

$ go test -run="TestApp_IgnoreOnUsageError"
PASS
ok      github.com/grubernaut/cli       0.001s

In some rare cases, it's handy to allow the application to handle
use-case errors in other ways, without requiring an error to be
returned. This allows the application written to handle the usage
error in other ways. Or, allow the application to pass the flags
on to subsequent child processes regardless of the host application's
flag parsing.

```
$ go test -run="TestApp_IgnoreOnUsageError"
PASS
ok      github.com/grubernaut/cli       0.001s
```
@meatballhat
Copy link
Member

@grubernaut IIRC this is part of the original intent behind the OnUsageError func, so that folks can choose to define it with a return nil to do whatever handling is desired. I'm generally OK with having this kind of usage be more explicit via a struct field like you've implemented, but I'm curious how the current feature(s) aren't meeting your needs 😃

@grubernaut
Copy link
Contributor Author

@meatballhat ah yea, I noticed that function. However, that returns the execution loop from the CLI app, without allowing for further interpolation from flags. It allows subcommands to pass through, but not incorrectly passed-in flags.
For example:

#fooApp/main.go
package main

import (
	"fmt"
	"os"

	cli "gopkg.in/urfave/cli.v2"
)

func main() {
	app := cli.App{}
	app.Name = "foo"
	app.Usage = "dont"
	app.OnUsageError = func(c *cli.Context, err error, isSubcommand bool) error {
		return nil
	}
	app.Action = func(c *cli.Context) error {
		fmt.Printf("It's working!!!")
		return nil
	}
	app.Run(os.Args)
}
$ ./fooApp
It's working!!!%
$ ./fooApp command
It's working!!!%
$ ./fooApp -f 
$

The -f flag threw the OnUsageError function, which ended application execution, and returned a nil error.

Albeit, this is a small use-case, but I'm currently working on a wrapper utility to run a conditional set of child programs via os.Exec(), and need to pass any argument flags to the wrapper binary that can be passed to any children.

This could have been changed by modifying how the error is returned here:https://github.com/urfave/cli/blob/v2/app.go#L193-L194, but I didn't want to cause any changing behavior for any consumers that were returning nil from the OnUsageError function, who actually wanted execution to stop on a usage error.

OT: but not quite sure how I broke the Travis build 😱

@coilysiren
Copy link
Member

Given that this PR is from years ago, I think I'm comfortable closing it 🙂 feel free to re-open / open a new PR if there's still interest here!

@coilysiren coilysiren closed this Aug 17, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants