diff --git a/docs.go b/docs.go index 0c352b0..adfc6ec 100644 --- a/docs.go +++ b/docs.go @@ -196,6 +196,9 @@ func prepareCommands(commands []*cli.Command, level int) []string { if command.Hidden { continue } + if level > 0 && command.Name == "help" { + continue + } usageText := prepareUsageText(command) @@ -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}, " ")), diff --git a/docs_test.go b/docs_test.go index e489751..ee958d5 100644 --- a/docs_test.go +++ b/docs_test.go @@ -7,6 +7,7 @@ import ( "io/fs" "net/mail" "os" + "strings" "testing" "github.com/stretchr/testify/require" @@ -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 })