Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion command.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package cobra
import (
"bytes"
"context"
"errors"
"fmt"
"io"
"os"
Expand Down Expand Up @@ -990,7 +991,7 @@ func (c *Command) ExecuteC() (cmd *Command, err error) {
if err != nil {
// Always show help if requested, even if SilenceErrors is in
// effect
if err == flag.ErrHelp {
if errors.Is(err, flag.ErrHelp) {
cmd.HelpFunc()(cmd, args)
return cmd, nil
}
Expand Down
32 changes: 32 additions & 0 deletions command_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1723,6 +1723,38 @@ func TestFlagErrorFunc(t *testing.T) {
}
}

func TestFlagErrorFuncHelp(t *testing.T) {
c := &Command{Use: "c", Run: emptyRun}
c.PersistentFlags().Bool("help", false, "help for c")
c.SetFlagErrorFunc(func(_ *Command, err error) error {
return fmt.Errorf("wrap error: %w", err)
})

out, err := executeCommand(c, "--help")
if err != nil {
t.Errorf("--help should not fail: %v", err)
}

expected := `Usage:
c [flags]

Flags:
--help help for c
`
if out != expected {
t.Errorf("Expected: %v, got: %v", expected, out)
}

out, err = executeCommand(c, "-h")
if err != nil {
t.Errorf("-h should not fail: %v", err)
}

if out != expected {
t.Errorf("Expected: %v, got: %v", expected, out)
}
}

// TestSortedFlags checks,
// if cmd.LocalFlags() is unsorted when cmd.Flags().SortFlags set to false.
// Related to https://github.com/spf13/cobra/issues/404.
Expand Down