Fix #1086: exit with 0 code on help (#1093)

Make CommandLine not exit on error and intercept flag.ErrHelp to return
0 instead of default behavior of flag.Parse which is to exit with code 2
on error (flag.ErrHelp is raised when help is asked).
This commit is contained in:
Chris Laprun
2018-12-07 17:00:30 +01:00
committed by Charlie Drage
parent 5f09de01d3
commit 1045bdbab6

View File

@@ -12,6 +12,7 @@ import (
"github.com/redhat-developer/odo/pkg/odo/util/completion"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
"os"
)
func main() {
@@ -32,8 +33,14 @@ func main() {
_ = root.Help()
}
// parse the flags - both the program's flags and the completion flags
flag.Parse()
// parse the flags but hack around to avoid exiting with error code 2 on help
flag.CommandLine.Init(os.Args[0], flag.ContinueOnError)
args := os.Args[1:]
if err := flag.CommandLine.Parse(args); err != nil {
if err == flag.ErrHelp {
os.Exit(0)
}
}
// run the completion, in case that the completion was invoked
// and ran as a completion script or handled a flag that passed