diff --git a/server/v2/command_factory.go b/server/v2/command_factory.go index a9f544a2f3b8..999f3565c22e 100644 --- a/server/v2/command_factory.go +++ b/server/v2/command_factory.go @@ -149,7 +149,7 @@ func (f *CommandFactory) ParseCommand( if err = cmd.ParseFlags(args); err != nil { // help requested, return the command early if errors.Is(err, pflag.ErrHelp) { - return cmd, nil, nil, nil + return cmd, nil, nil, err } return nil, nil, nil, err } 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:") + } +}