From 451c2fdbdcb254de7a089cef3785be90876edd00 Mon Sep 17 00:00:00 2001 From: Panos Chatzopoulos Date: Fri, 24 May 2024 15:50:53 +0200 Subject: [PATCH 1/2] Help returns exit 0 code --- main.go | 24 ++++++++++++++---------- 1 file changed, 14 insertions(+), 10 deletions(-) diff --git a/main.go b/main.go index 7a51f222..b0aa65fb 100644 --- a/main.go +++ b/main.go @@ -86,17 +86,16 @@ func main() { // of the arguments for the subcommand. func ParseArgs() (string, []string) { // Print usage if no arguments are provided. - // Terminate with non-zero exit status. if len(os.Args) < 2 { _ = Help("help") - os.Exit(1) + os.Exit(0) } switch os.Args[1] { case "version", "-v", "-version", "--version": if len(os.Args) != 2 { _ = Help("version") - os.Exit(1) + os.Exit(0) } return "version", os.Args @@ -119,11 +118,12 @@ func ParseArgs() (string, []string) { } else { subcommand = "help" } - - if Help(subcommand) == nil { - os.Exit(0) + // If the subcommand is not recognized, we exit with status 1 + err := Help(subcommand) + if err != nil { + os.Exit(1) } - os.Exit(1) + os.Exit(0) } @@ -138,7 +138,7 @@ func ParseArgs() (string, []string) { // non-zero exit status. if len(os.Args) == 1 { _ = Help(command) - os.Exit(1) + os.Exit(0) } return command, os.Args @@ -162,10 +162,14 @@ func Help(command string) error { fmt.Fprint(os.Stderr, subcommandUsage) } fmt.Fprintf(os.Stderr, - "Use '%s help ' to get help with subcommand flags.\n", + "use '%s help ' to get help with subcommand flags.\n", os.Args[0]) - return fmt.Errorf("Unknown command: %s", command) + if command == "help" { + return nil + } + + return fmt.Errorf("unknown command: %s", command) } // print subcommand help From db980ed30895377fdf7f2c3a6eb88b69d3bb6c7c Mon Sep 17 00:00:00 2001 From: Panos Chatzopoulos Date: Mon, 27 May 2024 15:12:56 +0200 Subject: [PATCH 2/2] Review fixes --- main.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/main.go b/main.go index b0aa65fb..d09fb7af 100644 --- a/main.go +++ b/main.go @@ -88,7 +88,7 @@ func ParseArgs() (string, []string) { // Print usage if no arguments are provided. if len(os.Args) < 2 { _ = Help("help") - os.Exit(0) + os.Exit(1) } switch os.Args[1] { @@ -138,7 +138,7 @@ func ParseArgs() (string, []string) { // non-zero exit status. if len(os.Args) == 1 { _ = Help(command) - os.Exit(0) + os.Exit(1) } return command, os.Args