diff --git a/bash_completions.go b/bash_completions.go index 66efd719..25c69ec2 100644 --- a/bash_completions.go +++ b/bash_completions.go @@ -223,7 +223,7 @@ func postscript(out *bytes.Buffer, name string) { func writeCommands(cmd *Command, out *bytes.Buffer) { fmt.Fprintf(out, " commands=()\n") for _, c := range cmd.Commands() { - if len(c.Deprecated) > 0 || c == cmd.helpCommand { + if !c.IsAvailableCommand() || c == cmd.helpCommand { continue } fmt.Fprintf(out, " commands+=(%q)\n", c.Name()) @@ -332,7 +332,7 @@ func writeRequiredNoun(cmd *Command, out *bytes.Buffer) { func gen(cmd *Command, out *bytes.Buffer) { for _, c := range cmd.Commands() { - if len(c.Deprecated) > 0 || c == cmd.helpCommand { + if !c.IsAvailableCommand() || c == cmd.helpCommand { continue } gen(c, out) diff --git a/command.go b/command.go index 2996fd5f..df5a4554 100644 --- a/command.go +++ b/command.go @@ -855,9 +855,15 @@ func (c *Command) HasSubCommands() bool { // IsAvailableCommand determines if a command is available as a non-help command // (this includes all non deprecated/hidden commands) func (c *Command) IsAvailableCommand() bool { + if len(c.Deprecated) != 0 || c.Hidden { + return false + } - // a command is 'available' if it is runnable and is not deprecated/hidden - return c.Runnable() && len(c.Deprecated) == 0 && !c.Hidden + if c.Runnable() || c.HasAvailableSubCommands() { + return true + } + + return false } // IsHelpCommand determines if a command is a 'help' command; a help command is diff --git a/doc_util.go b/doc_util.go index 9c20bca8..e2482162 100644 --- a/doc_util.go +++ b/doc_util.go @@ -13,8 +13,6 @@ package cobra -import () - // Test to see if we have a reason to print See Also information in docs // Basically this is a test for a parent commend or a subcommand which is // both not deprecated and not the autogenerated help command. @@ -27,7 +25,7 @@ func (cmd *Command) hasSeeAlso() bool { return false } for _, c := range children { - if len(c.Deprecated) != 0 || c == cmd.helpCommand { + if !c.IsAvailableCommand() || c == cmd.helpCommand { continue } return true diff --git a/man_docs.go b/man_docs.go index e5edbfdd..e61fff18 100644 --- a/man_docs.go +++ b/man_docs.go @@ -40,7 +40,7 @@ func (cmd *Command) GenManTree(header *GenManHeader, dir string) { header = &GenManHeader{} } for _, c := range cmd.Commands() { - if len(c.Deprecated) != 0 || c == cmd.helpCommand { + if !c.IsAvailableCommand() || c == cmd.helpCommand { continue } GenManTree(c, header, dir) @@ -125,7 +125,7 @@ func manPreamble(out *bytes.Buffer, header *GenManHeader, name, short, long stri func manPrintFlags(out *bytes.Buffer, flags *pflag.FlagSet) { flags.VisitAll(func(flag *pflag.Flag) { - if len(flag.Deprecated) > 0 { + if len(flag.Deprecated) > 0 || flag.Hidden { return } format := "" @@ -200,7 +200,7 @@ func genMarkdown(cmd *Command, header *GenManHeader) []byte { children := cmd.Commands() sort.Sort(byName(children)) for _, c := range children { - if len(c.Deprecated) != 0 || c == cmd.helpCommand { + if !c.IsAvailableCommand() || c == cmd.helpCommand { continue } fmt.Fprintf(buf, "**%s-%s(%s)**, ", dashCommandName, c.Name(), header.Section) diff --git a/md_docs.go b/md_docs.go index 4e5b23a1..3c27f7ae 100644 --- a/md_docs.go +++ b/md_docs.go @@ -97,7 +97,7 @@ func (cmd *Command) GenMarkdownCustom(out *bytes.Buffer, linkHandler func(string sort.Sort(byName(children)) for _, child := range children { - if len(child.Deprecated) > 0 || child == cmd.helpCommand { + if !child.IsAvailableCommand() || child == cmd.helpCommand { continue } cname := name + " " + child.Name() @@ -127,7 +127,7 @@ func GenMarkdownTreeCustom(cmd *Command, dir string, filePrepender func(string) func (cmd *Command) GenMarkdownTreeCustom(dir string, filePrepender func(string) string, linkHandler func(string) string) { for _, c := range cmd.Commands() { - if len(c.Deprecated) != 0 || c == cmd.helpCommand { + if !c.IsAvailableCommand() || c == cmd.helpCommand { continue } c.GenMarkdownTreeCustom(dir, filePrepender, linkHandler)