diff --git a/command.go b/command.go index b032239153..991894ea82 100644 --- a/command.go +++ b/command.go @@ -527,6 +527,10 @@ func (cmd *Command) Run(ctx context.Context, osArgs []string) (deferErr error) { func (cmd *Command) checkHelp() bool { tracef("checking if help is wanted (cmd=%[1]q)", cmd.Name) + if HelpFlag == nil { + return false + } + for _, name := range HelpFlag.Names() { if cmd.Bool(name) { return true diff --git a/help_test.go b/help_test.go index d3bee346e0..1b387ea768 100644 --- a/help_test.go +++ b/help_test.go @@ -95,6 +95,24 @@ func Test_Help_Custom_Flags(t *testing.T) { require.Len(t, out.String(), 0) } +func Test_Help_Nil_Flags(t *testing.T) { + oldFlag := HelpFlag + defer func() { + HelpFlag = oldFlag + }() + HelpFlag = nil + + cmd := &Command{ + Action: func(_ context.Context, cmd *Command) error { + return nil + }, + } + out := new(bytes.Buffer) + cmd.Writer = out + _ = cmd.Run(buildTestContext(t), []string{"test"}) + require.Len(t, out.String(), 0) +} + func Test_Version_Custom_Flags(t *testing.T) { oldFlag := VersionFlag defer func() { diff --git a/suggestions.go b/suggestions.go index b078d8781a..607de09deb 100644 --- a/suggestions.go +++ b/suggestions.go @@ -31,7 +31,7 @@ func suggestFlag(flags []Flag, provided string, hideHelp bool) string { for _, flag := range flags { flagNames := flag.Names() - if !hideHelp { + if !hideHelp && HelpFlag != nil { flagNames = append(flagNames, HelpFlag.Names()...) } for _, name := range flagNames {