diff --git a/command.go b/command.go index 91f238f2a5..3970f1c355 100644 --- a/command.go +++ b/command.go @@ -900,9 +900,10 @@ func (cmd *Command) VisibleCategories() []CommandCategory { func (cmd *Command) VisibleCommands() []*Command { var ret []*Command for _, command := range cmd.Commands { - if !command.Hidden { - ret = append(ret, command) + if command.Hidden || command.Name == helpName { + continue } + ret = append(ret, command) } return ret } diff --git a/command_test.go b/command_test.go index 5946264abe..d9bc3605da 100644 --- a/command_test.go +++ b/command_test.go @@ -1026,7 +1026,6 @@ func TestCommand_VisibleCommands(t *testing.T) { cmd.setupDefaults([]string{"cli.test"}) expected := []*Command{ cmd.Commands[0], - cmd.Commands[2], // help } actual := cmd.VisibleCommands() assert.Len(t, actual, len(expected)) diff --git a/docs/migrate-v2-to-v3.md b/docs/migrate-v2-to-v3.md index b5ef6a4b02..aa2ab96b58 100644 --- a/docs/migrate-v2-to-v3.md +++ b/docs/migrate-v2-to-v3.md @@ -123,7 +123,7 @@ in addition to other specific args. This allows handler functions to utilize con blocking/time-specific operations and so on * OLD: `type BeforeFunc func(*Context) error` -* NEW: `type BeforeFunc func(context.Context, *cli.Command) error` +* NEW: `type BeforeFunc func(context.Context, *cli.Command) (context.Context, error)` * OLD: `type AfterFunc func(*Context) error` * NEW: `type AfterFunc func(context.Context, *cli.Command) error` diff --git a/docs/v3/examples/flags.md b/docs/v3/examples/flags.md index 04d1aefb44..b311e6a897 100644 --- a/docs/v3/examples/flags.md +++ b/docs/v3/examples/flags.md @@ -501,7 +501,9 @@ getting data from a yaml file below. ```go // --- >8 --- - command.Before = altsrc.InitInputSourceWithContext(command.Flags, NewYamlSourceFromFlagFunc("load")) + command.Before = func(ctx context.Context, cmd *Command) (context.Context, error) { + return ctx, altsrc.InitInputSourceWithContext(command.Flags, NewYamlSourceFromFlagFunc("load")) + } ``` The code above will use the "load" string as a flag name to get the file name of diff --git a/docs/v3/examples/full-api-example.md b/docs/v3/examples/full-api-example.md index a468be857a..edf97c58ec 100644 --- a/docs/v3/examples/full-api-example.md +++ b/docs/v3/examples/full-api-example.md @@ -153,9 +153,9 @@ func main() { ShellComplete: func(ctx context.Context, cmd *cli.Command) { fmt.Fprintf(cmd.Root().Writer, "lipstick\nkiss\nme\nlipstick\nringo\n") }, - Before: func(ctx context.Context, cmd *cli.Command) error { + Before: func(ctx context.Context, cmd *cli.Command) (context.Context, error) { fmt.Fprintf(cmd.Root().Writer, "HEEEERE GOES\n") - return nil + return nil, nil }, After: func(ctx context.Context, cmd *cli.Command) error { fmt.Fprintf(cmd.Root().Writer, "Phew!\n") diff --git a/examples_test.go b/examples_test.go index dfb49e177b..4bc4654253 100644 --- a/examples_test.go +++ b/examples_test.go @@ -181,14 +181,11 @@ func ExampleCommand_Run_commandHelp() { // greet describeit - use it to see a description // // USAGE: - // greet describeit [command [command options]] [arguments...] + // greet describeit [arguments...] // // DESCRIPTION: // This is how we describe describeit the function // - // COMMANDS: - // help, h Shows a list of commands or help for one command - // // OPTIONS: // --help, -h show help } @@ -205,10 +202,7 @@ func ExampleCommand_Run_noAction() { // greet - A new cli application // // USAGE: - // greet [global options] [command [command options]] [arguments...] - // - // COMMANDS: - // help, h Shows a list of commands or help for one command + // greet [global options] [arguments...] // // GLOBAL OPTIONS: // --help, -h show help diff --git a/help_test.go b/help_test.go index e9db4383df..a85587ba10 100644 --- a/help_test.go +++ b/help_test.go @@ -82,10 +82,7 @@ func Test_Help_RequiredFlagsNoDefault(t *testing.T) { test - A new cli application USAGE: - test [global options] [command [command options]] [arguments...] - -COMMANDS: - help, h Shows a list of commands or help for one command + test [global options] [arguments...] GLOBAL OPTIONS: --foo value, -f value @@ -198,7 +195,7 @@ func Test_helpCommand_InHelpOutput(t *testing.T) { s := output.String() require.NotContains(t, s, "\nCOMMANDS:\nGLOBAL OPTIONS:\n", "empty COMMANDS section detected") - require.Contains(t, s, "help, h", "missing \"help, h\"") + require.Contains(t, s, "--help, -h", "missing \"--help, --h\"") } func TestHelpCommand_FullName(t *testing.T) { @@ -361,7 +358,6 @@ func TestShowCommandHelp_AppendHelp(t *testing.T) { args: []string{"app", "cmd", "help"}, verify: func(t *testing.T, outString string) { r := require.New(t) - r.Contains(outString, "help, h Shows a list of commands or help for one command") r.Contains(outString, "--help, -h show help") }, }, @@ -1512,11 +1508,6 @@ DESCRIPTION: enough to wrap in this test case -COMMANDS: - help, h Shows a list of - commands or help - for one command - OPTIONS: --help, -h show help `, @@ -1655,11 +1646,6 @@ USAGE: this is long enough to wrap even more -COMMANDS: - help, h Shows a list of - commands or help - for one command - OPTIONS: --test-f value my test usage @@ -1733,12 +1719,7 @@ func TestCategorizedHelp(t *testing.T) { application USAGE: - cli.test [global options] [command [command options]] [arguments...] - -COMMANDS: - help, h Shows a list of - commands or help - for one command + cli.test [global options] [arguments...] GLOBAL OPTIONS: --help, -h show help