google / subcommands

Go subcommand library.
Apache License 2.0
749 stars 48 forks source link

proposal: Execute() (ExitStatus, error) #8

Closed runningmaster closed 6 years ago

runningmaster commented 7 years ago

I think that it will be useful for many users of this superb package.

func main() {
    // ...
    flag.Parse()
    ctx := context.Background()
    code, err := subcommands.Execute(ctx)
    if err != nil {
        fmt.Fprintln(os.Stderr, "Error: ", err)
    }
    os.Exit(int(code))
}
slewiskelly commented 6 years ago

What is stopping you from printing the error in Execute() and exiting as shown in the example?

os.Exit(int(subcommands.Execute(ctx)))

Keep in mind, that this kind of change would break the build of all tools that use this package.

adjackura commented 6 years ago

Sorry, I think I thought I had commented on this but I guess I never did. We can't take changes the break the interface of this package. As slewiskelly pointed out, you should print any error in the subcommands Execute().