Skip to content
New issue

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

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

Already on GitHub? # to your account

Exit zero if help/version is explicitly requested #525

Merged
merged 2 commits into from
Apr 8, 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
18 changes: 17 additions & 1 deletion temporalcli/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"os"
"os/signal"
"path/filepath"
"slices"
"strings"
"syscall"
"time"
Expand Down Expand Up @@ -321,8 +322,23 @@ func Execute(ctx context.Context, options CommandOptions) {
if err != nil {
cctx.Options.Fail(err)
}
// If no command ever actually got run, exit nonzero

// If no command ever actually got run, exit nonzero with an error. This is
// an ugly hack to make sure that iff the user explicitly asked for help, we
// exit with a zero error code. (The other situation in which help is
// printed is when the user invokes an unknown command--we still want a
// non-zero exit in that case.) We should revisit this if/when the
// following Cobra issues get fixed:
//
// - https://github.com/spf13/cobra/issues/1156
// - https://github.com/spf13/cobra/issues/706
if !cctx.ActuallyRanCommand {
zeroExitArgs := []string{"--help", "-h", "--version", "-v", "help"}
if slices.ContainsFunc(cctx.Options.Args, func(a string) bool {
return slices.Contains(zeroExitArgs, a)
}) {
return
}
cctx.Options.Fail(fmt.Errorf("unknown command"))
}
}
Expand Down
Loading