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.
This commit is contained in:
Eric Paris 2015-09-08 22:43:49 -04:00
parent 75d96d75c3
commit 3eac1434ef
5 changed files with 16 additions and 12 deletions

View file

@ -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)

View file

@ -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

View file

@ -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

View file

@ -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)

View file

@ -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)