Skip to content

Commit

Permalink
Help returns exit 0 code (#389)
Browse files Browse the repository at this point in the history
  • Loading branch information
pahatz authored May 28, 2024
2 parents fb55634 + db980ed commit 433b631
Showing 1 changed file with 12 additions and 8 deletions.
20 changes: 12 additions & 8 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,6 @@ 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)
Expand All @@ -96,7 +95,7 @@ func ParseArgs() (string, []string) {
case "version", "-v", "-version", "--version":
if len(os.Args) != 2 {
_ = Help("version")
os.Exit(1)
os.Exit(0)
}

return "version", os.Args
Expand All @@ -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)

}

Expand Down Expand Up @@ -162,10 +162,14 @@ func Help(command string) error {
fmt.Fprint(os.Stderr, subcommandUsage)
}
fmt.Fprintf(os.Stderr,
"Use '%s help <command>' to get help with subcommand flags.\n",
"use '%s help <command>' 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
Expand Down

0 comments on commit 433b631

Please sign in to comment.