Skip to content

Commit

Permalink
Merge pull request #2000 from dearchap/issue_1921_fix
Browse files Browse the repository at this point in the history
Fix:(issue_1921) Dont display [command] in help
  • Loading branch information
dearchap authored Nov 2, 2024
2 parents 7ec374f + 68f9c2c commit 30c4df2
Show file tree
Hide file tree
Showing 7 changed files with 14 additions and 37 deletions.
5 changes: 3 additions & 2 deletions command.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
1 change: 0 additions & 1 deletion command_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
2 changes: 1 addition & 1 deletion docs/migrate-v2-to-v3.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`
Expand Down
4 changes: 3 additions & 1 deletion docs/v3/examples/flags.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions docs/v3/examples/full-api-example.md
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
10 changes: 2 additions & 8 deletions examples_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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
Expand Down
25 changes: 3 additions & 22 deletions help_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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")
},
},
Expand Down Expand Up @@ -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
`,
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 30c4df2

Please sign in to comment.