Skip to content

Commit

Permalink
Help returns exit 0 code
Browse files Browse the repository at this point in the history
  • Loading branch information
pahatz committed May 24, 2024
1 parent 19233e3 commit 451c2fd
Showing 1 changed file with 14 additions and 10 deletions.
24 changes: 14 additions & 10 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
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 All @@ -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
Expand All @@ -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 451c2fd

Please sign in to comment.