Any chance I could register a callback to handle os.Exit myself when the user stuffs up on flag usage? I need more control over the exit status than your hard-coded os.Exit(-1) permits.
Hang on… that'd be PersistentPreRunE, perhaps. Or do I need to hook all the E functions?
I did a clean-up of Hugo in this area some time ago, have a look at
https://github.com/spf13/hugo/blob/master/commands/hugo.go#L126
https://github.com/spf13/hugo/blob/master/commands/hugo.go#L100
To propagate errors from the commands, use the RunE.
The err result from RootCmd.Execute gets RunE and command line problems e.g. unknown options, which is nice… aah, and the undocumented SilenceUsage suppresses the usage text. Awesome.
TL;DR:
// control your exit status with Execute, by default in cmd/root.go:
func Execute() {
if err := RootCmd.Execute(); err != nil {
// log it, then
os.Exit(23)
}
}
Most helpful comment
The
errresult fromRootCmd.ExecutegetsRunEand command line problems e.g. unknown options, which is nice… aah, and the undocumentedSilenceUsagesuppresses the usage text. Awesome.TL;DR: