Skip to content

Commit

Permalink
fix: don't print help for non-validation errors. Fixes argoproj#13826
Browse files Browse the repository at this point in the history
argoproj#13656 changed all CLI
commands to use `RunE` instead of `Run` and [cobra
validators](https://cobra.dev/#positional-and-custom-arguments). The
default behavior with Cobra is to print the help text for all errors,
which can be tedious to scroll through.

This changes the CLI to only print the help text for argument validation
errors, which should match the previous behavior.

Signed-off-by: Mason Malone <[email protected]>
  • Loading branch information
MasonM committed Oct 29, 2024
1 parent eb23eb6 commit 0827ea5
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 0 deletions.
8 changes: 8 additions & 0 deletions cmd/argo/commands/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,14 @@ If your server is behind an ingress with a path (running "argo server --base-hre
cli.SetLogLevel(logLevel)
cmdutil.SetGLogLevel(glogLevel)
log.WithField("version", argo.GetVersion()).Debug("CLI version")

// Disable printing of usage string on errors, except for argument validation errors
// (i.e. when the "Args" function returns an error).
//
// This is set here instead of directly in "command" because Cobra
// executes PersistentPreRun after performing argument validation:
// https://github.com/spf13/cobra/blob/3a5efaede9d389703a792e2f7bfe3a64bc82ced9/command.go#L939-L957
cmd.SilenceUsage = true
}
command.PersistentFlags().StringVar(&logLevel, "loglevel", "info", "Set the logging level. One of: debug|info|warn|error")
command.PersistentFlags().IntVar(&glogLevel, "gloglevel", 0, "Set the glog logging level")
Expand Down
2 changes: 2 additions & 0 deletions test/e2e/cli_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -757,6 +757,7 @@ func (s *CLISuite) TestWorkflowLint() {
RunCli([]string{"lint", "--kinds", "wf", "testdata/workflow-template-nested-template.yaml"}, func(t *testing.T, output string, err error) {
require.Error(t, err)
assert.Contains(t, output, "found nothing to lint in the specified paths, failing...")
assert.NotContains(t, output, "Usage:")
})
})
s.Run("All Kinds", func() {
Expand Down Expand Up @@ -1095,6 +1096,7 @@ func (s *CLISuite) TestTemplateCommands() {
s.Run("LintWithoutArgs", func() {
s.Given().RunCli([]string{"template", "lint"}, func(t *testing.T, output string, err error) {
require.Error(t, err)
assert.Contains(t, output, "Error: requires at least 1 arg(s), only received 0")
assert.Contains(t, output, "Usage:")
})
})
Expand Down

0 comments on commit 0827ea5

Please sign in to comment.