fix(server/v2): return ErrHelp (#22399)

This commit is contained in:
Matt Kocubinski 2024-10-30 10:35:07 -05:00 committed by GitHub
parent 6b6e71594a
commit 470e085946
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 30 additions and 1 deletions

View File

@ -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
}

View File

@ -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
}

View File

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