Skip to content

Commit

Permalink
fix(server/v2): return ErrHelp (backport #22399) (#22400)
Browse files Browse the repository at this point in the history
Co-authored-by: Matt Kocubinski <mkocubinski@gmail.com>
Co-authored-by: Julien Robert <julien@rbrt.fr>
  • Loading branch information
3 people authored Oct 31, 2024
1 parent ea770de commit a967b93
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 0 deletions.
6 changes: 6 additions & 0 deletions simapp/v2/simdv2/cmd/root_di.go
Original file line number Diff line number Diff line change
@@ -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"
Expand Down Expand Up @@ -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
}

Expand Down
23 changes: 23 additions & 0 deletions simapp/v2/simdv2/cmd/root_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package cmd_test

import (
"bytes"
"fmt"
"testing"

Expand Down Expand Up @@ -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:")
}
}

0 comments on commit a967b93

Please sign in to comment.