Skip to content

Commit

Permalink
fix(server/v2): return ErrHelp (#22399)
Browse files Browse the repository at this point in the history
  • Loading branch information
kocubinski authored Oct 30, 2024
1 parent 6b6e715 commit 470e085
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 1 deletion.
2 changes: 1 addition & 1 deletion server/v2/command_factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
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 470e085

Please sign in to comment.