Skip to content
Open
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
7 changes: 7 additions & 0 deletions docs.go
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,9 @@ func prepareCommands(commands []*cli.Command, level int) []string {
if command.Hidden {
continue
}
if level > 0 && command.Name == "help" {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm wondering if we need to compare this with helpCommand.Name instead ?

continue
}

usageText := prepareUsageText(command)

Expand Down Expand Up @@ -369,6 +372,10 @@ func (tt tabularTemplate) PrepareCommands(commands []*cli.Command, appPath, pare
result := make([]cliTabularCommandTemplate, 0, len(commands))

for _, cmd := range commands {
if level > 0 && cmd.Name == "help" {
continue
}

command := cliTabularCommandTemplate{
AppPath: appPath,
Name: strings.TrimSpace(strings.Join([]string{parentCommandName, cmd.Name}, " ")),
Expand Down
44 changes: 44 additions & 0 deletions docs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"io/fs"
"net/mail"
"os"
"strings"
"testing"

"github.com/stretchr/testify/require"
Expand Down Expand Up @@ -185,6 +186,49 @@ func TestToTabularMarkdown(t *testing.T) {
})
}

func TestPrepareCommandsSkipsNestedHelp(t *testing.T) {
tmpl := tabularTemplate{}
commands := []*cli.Command{
{Name: "help"},
{
Name: "config",
Commands: []*cli.Command{
{Name: "help"},
{Name: "sub"},
},
},
}

result := tmpl.PrepareCommands(commands, "app", "", 0)
require.Len(t, result, 2)
require.Equal(t, "help", result[0].Name)
require.Len(t, result[1].SubCommands, 1)
require.Equal(t, "config sub", result[1].SubCommands[0].Name)
require.Equal(t, uint(0), result[0].Level)
require.Equal(t, uint(0), result[1].Level)
require.Equal(t, uint(1), result[1].SubCommands[0].Level)
}

func TestPrepareCommandsMarkdownSkipsNestedHelp(t *testing.T) {
commands := []*cli.Command{
{Name: "help"},
{
Name: "config",
Commands: []*cli.Command{
{Name: "help"},
{Name: "sub"},
},
},
}

sections := prepareCommands(commands, 0)
joined := strings.Join(sections, "\n")
require.Contains(t, joined, "## help\n")
require.Contains(t, joined, "## config\n")
require.Contains(t, joined, "### sub\n")
require.NotContains(t, joined, "### help")
}

func TestToTabularMarkdownFailed(t *testing.T) {
tpl := MarkdownTabularDocTemplate
t.Cleanup(func() { MarkdownTabularDocTemplate = tpl })
Expand Down
Loading