From 7ed567fbad1e16b1d349ee8e6343dc58907b26da Mon Sep 17 00:00:00 2001 From: Eric Coissac Date: Tue, 21 Mar 2023 22:01:20 +0700 Subject: [PATCH] Make the `--help` or `-h` options working when mandatory options are declared Former-commit-id: db502ff81dcf20449d126978fcebf890edb814ae --- pkg/obioptions/options.go | 27 +++++++++++++++------------ 1 file changed, 15 insertions(+), 12 deletions(-) diff --git a/pkg/obioptions/options.go b/pkg/obioptions/options.go index 8212bb7..1ba683b 100644 --- a/pkg/obioptions/options.go +++ b/pkg/obioptions/options.go @@ -41,8 +41,22 @@ func GenerateOptionParser(optionset ...func(*getoptions.GetOpt)) ArgumentParser remaining, err := options.Parse(args[1:]) + if options.Called("help") { + fmt.Fprint(os.Stderr, options.Help()) + os.Exit(1) + } + + log.SetLevel(log.InfoLevel) + if options.Called("debug") { + log.SetLevel(log.DebugLevel) + log.Debugln("Switch to debug level logging") + } + + // Handle user errors if err != nil { - log.Fatalf("Error on the commande line : %v",err) + fmt.Fprintf(os.Stderr, "ERROR: %s\n\n", err) + fmt.Fprint(os.Stderr, options.Help(getoptions.HelpSynopsis)) + os.Exit(1) } // Setup the maximum number of CPU usable by the program @@ -59,17 +73,6 @@ func GenerateOptionParser(optionset ...func(*getoptions.GetOpt)) ArgumentParser log.Printf("No singleton option set") } - if options.Called("help") { - fmt.Fprint(os.Stderr, options.Help()) - os.Exit(1) - } - - log.SetLevel(log.InfoLevel) - if options.Called("debug") { - log.SetLevel(log.DebugLevel) - log.Debugln("Switch to debug level logging") - } - return options, remaining } }