Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Help returns exit 0 code #389

Merged
merged 2 commits into from
May 28, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading