From a967b93e2448f4ca452f46feaf4fb6fafc6fd160 Mon Sep 17 00:00:00 2001 From: "mergify[bot]" <37929162+mergify[bot]@users.noreply.github.com> Date: Thu, 31 Oct 2024 09:20:35 +0100 Subject: [PATCH] fix(server/v2): return ErrHelp (backport #22399) (#22400) Co-authored-by: Matt Kocubinski Co-authored-by: Julien Robert --- simapp/v2/simdv2/cmd/root_di.go | 6 ++++++ simapp/v2/simdv2/cmd/root_test.go | 23 +++++++++++++++++++++++ 2 files changed, 29 insertions(+) diff --git a/simapp/v2/simdv2/cmd/root_di.go b/simapp/v2/simdv2/cmd/root_di.go index 0bbae702becb..d1e3f62c5e12 100644 --- a/simapp/v2/simdv2/cmd/root_di.go +++ b/simapp/v2/simdv2/cmd/root_di.go @@ -1,7 +1,10 @@ package cmd import ( + "errors" + "github.com/spf13/cobra" + "github.com/spf13/pflag" autocliv1 "cosmossdk.io/api/cosmos/autocli/v1" "cosmossdk.io/client/v2/autocli" @@ -38,6 +41,9 @@ func NewRootCmd[T transaction.Tx]( subCommand, configMap, logger, err := factory.ParseCommand(rootCommand, args) if err != nil { + if errors.Is(err, pflag.ErrHelp) { + return rootCommand, nil + } return nil, err } diff --git a/simapp/v2/simdv2/cmd/root_test.go b/simapp/v2/simdv2/cmd/root_test.go index 7f7d6a07d95a..687e8aa510f0 100644 --- a/simapp/v2/simdv2/cmd/root_test.go +++ b/simapp/v2/simdv2/cmd/root_test.go @@ -1,6 +1,7 @@ package cmd_test import ( + "bytes" "fmt" "testing" @@ -42,3 +43,25 @@ func TestHomeFlagRegistration(t *testing.T) { require.NoError(t, err) require.Equal(t, result, homeDir) } + +func TestHelpRequested(t *testing.T) { + argz := [][]string{ + {"query", "--help"}, + {"query", "tx", "-h"}, + {"--help"}, + {"start", "-h"}, + } + + for _, args := range argz { + rootCmd, err := cmd.NewRootCmd[transaction.Tx](args...) + require.NoError(t, err) + + var out bytes.Buffer + rootCmd.SetArgs(args) + rootCmd.SetOut(&out) + require.NoError(t, rootCmd.Execute()) + require.Contains(t, out.String(), args[0]) + require.Contains(t, out.String(), "--help") + require.Contains(t, out.String(), "Usage:") + } +}