From 3eac1434ef2164a44e3705c6e0d63329b5ec3398 Mon Sep 17 00:00:00 2001 From: Eric Paris Date: Tue, 8 Sep 2015 22:43:49 -0400 Subject: [PATCH] Update IsAvailableCommand checks This slightly changes IsAvailableCommand in that a non-runnable command with a runnable subcommand is now 'Available' We also use IsAvailableCommand in the rest of the codebase instead of half kinda sorta doing it incorrectly other places. --- bash_completions.go | 4 ++-- command.go | 10 ++++++++-- doc_util.go | 4 +--- man_docs.go | 6 +++--- md_docs.go | 4 ++-- 5 files changed, 16 insertions(+), 12 deletions(-) diff --git a/bash_completions.go b/bash_completions.go index 66efd71..25c69ec 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 2996fd5..df5a455 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 9c20bca..e248216 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 e5edbfd..e61fff1 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 4e5b23a..3c27f7a 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)