Skip to content

Commit

Permalink
Exit zero if help is explicitly requested
Browse files Browse the repository at this point in the history
We want a non-zero exit when the user invokes `temporal` with an unknown
command/subcommand, or when invoked with no arguments.  But if the user
explicitly asks for help, we should print it and return success.

Closes #495.
  • Loading branch information
josh-berry committed Apr 5, 2024
1 parent cb1d5b4 commit 7c9e180
Showing 1 changed file with 20 additions and 0 deletions.
20 changes: 20 additions & 0 deletions temporalcli/commands.go
Expand Up @@ -10,6 +10,7 @@ import (
"os"
"os/signal"
"path/filepath"
"slices"
"strings"
"syscall"
"time"
Expand Down Expand Up @@ -360,6 +361,25 @@ func (c *TemporalCommand) initCommand(cctx *CommandContext) {
c.Command.PersistentPostRun = func(*cobra.Command, []string) {
color.NoColor = origNoColor
}

// 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
inner_help := c.Command.HelpFunc()
c.Command.SetHelpFunc(func(c *cobra.Command, args []string) {
if slices.ContainsFunc(args, func(a string) bool {
return a == "--help" || a == "-h" || a == "help"
}) {
cctx.ActuallyRanCommand = true
}

inner_help(c, args)
})
}

func (c *TemporalCommand) preRun(cctx *CommandContext) error {
Expand Down

0 comments on commit 7c9e180

Please sign in to comment.