diff --git a/command_run.go b/command_run.go index 32bd509199..e5cfff8a57 100644 --- a/command_run.go +++ b/command_run.go @@ -141,8 +141,8 @@ func (cmd *Command) run(ctx context.Context, osArgs []string) (_ context.Context var rargs Args = &stringSliceArgs{v: osArgs} var args Args = &stringSliceArgs{rargs.Tail()} - if cmd.isCompletionCommand { - tracef("completion command detected, skipping pre-parse (cmd=%[1]q)", cmd.Name) + if cmd.isCompletionCommand || cmd.Name == helpName { + tracef("special command detected, skipping pre-parse (cmd=%[1]q)", cmd.Name) cmd.parsedArgs = args return ctx, cmd.Action(ctx, cmd) } diff --git a/completion_test.go b/completion_test.go index cf47381a87..17112d02fa 100644 --- a/completion_test.go +++ b/completion_test.go @@ -21,7 +21,7 @@ func TestCompletionEnable(t *testing.T) { cmd := &Command{ EnableShellCompletion: true, Flags: []Flag{ - &BoolFlag{ + &StringFlag{ Name: "goo", Required: true, }, diff --git a/help_test.go b/help_test.go index 0f7f5d5981..fad4242128 100644 --- a/help_test.go +++ b/help_test.go @@ -108,7 +108,7 @@ func TestShowSubcommandHelpAndExit(t *testing.T) { require.Equal(t, 42, lastExitCode) } -func Test_Help_RequiredFlagsNoDefault(t *testing.T) { +func Test_HelpFlag_RequiredFlagsNoDefault(t *testing.T) { output := new(bytes.Buffer) cmd := &Command{ @@ -136,6 +136,34 @@ GLOBAL OPTIONS: "expected output to include usage text") } +func Test_HelpCommand_RequiredFlagsNoDefault(t *testing.T) { + output := new(bytes.Buffer) + + cmd := &Command{ + Flags: []Flag{ + &Int64Flag{Name: "foo", Aliases: []string{"f"}, Required: true}, + }, + Arguments: AnyArguments, + Writer: output, + } + + _ = cmd.Run(buildTestContext(t), []string{"test", "help"}) + + expected := `NAME: + test - A new cli application + +USAGE: + test [global options] [arguments...] + +GLOBAL OPTIONS: + --foo int, -f int + --help, -h show help +` + + assert.Contains(t, output.String(), expected, + "expected output to include usage text") +} + func Test_Help_Custom_Flags(t *testing.T) { oldFlag := HelpFlag defer func() {