Merge pull request #146 from eparis/is-available-rework

Update IsAvailableCommand checks
This commit is contained in:
Eric Paris 2015-09-09 14:20:07 -05:00
commit 4b86c66ef2
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) { func writeCommands(cmd *Command, out *bytes.Buffer) {
fmt.Fprintf(out, " commands=()\n") fmt.Fprintf(out, " commands=()\n")
for _, c := range cmd.Commands() { for _, c := range cmd.Commands() {
if len(c.Deprecated) > 0 || c == cmd.helpCommand { if !c.IsAvailableCommand() || c == cmd.helpCommand {
continue continue
} }
fmt.Fprintf(out, " commands+=(%q)\n", c.Name()) 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) { func gen(cmd *Command, out *bytes.Buffer) {
for _, c := range cmd.Commands() { for _, c := range cmd.Commands() {
if len(c.Deprecated) > 0 || c == cmd.helpCommand { if !c.IsAvailableCommand() || c == cmd.helpCommand {
continue continue
} }
gen(c, out) 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 // IsAvailableCommand determines if a command is available as a non-help command
// (this includes all non deprecated/hidden commands) // (this includes all non deprecated/hidden commands)
func (c *Command) IsAvailableCommand() bool { 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 if c.Runnable() || c.HasAvailableSubCommands() {
return c.Runnable() && len(c.Deprecated) == 0 && !c.Hidden return true
}
return false
} }
// IsHelpCommand determines if a command is a 'help' command; a help command is // IsHelpCommand determines if a command is a 'help' command; a help command is

View file

@ -13,8 +13,6 @@
package cobra package cobra
import ()
// Test to see if we have a reason to print See Also information in docs // 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 // Basically this is a test for a parent commend or a subcommand which is
// both not deprecated and not the autogenerated help command. // both not deprecated and not the autogenerated help command.
@ -27,7 +25,7 @@ func (cmd *Command) hasSeeAlso() bool {
return false return false
} }
for _, c := range children { for _, c := range children {
if len(c.Deprecated) != 0 || c == cmd.helpCommand { if !c.IsAvailableCommand() || c == cmd.helpCommand {
continue continue
} }
return true return true

View file

@ -40,7 +40,7 @@ func (cmd *Command) GenManTree(header *GenManHeader, dir string) {
header = &GenManHeader{} header = &GenManHeader{}
} }
for _, c := range cmd.Commands() { for _, c := range cmd.Commands() {
if len(c.Deprecated) != 0 || c == cmd.helpCommand { if !c.IsAvailableCommand() || c == cmd.helpCommand {
continue continue
} }
GenManTree(c, header, dir) 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) { func manPrintFlags(out *bytes.Buffer, flags *pflag.FlagSet) {
flags.VisitAll(func(flag *pflag.Flag) { flags.VisitAll(func(flag *pflag.Flag) {
if len(flag.Deprecated) > 0 { if len(flag.Deprecated) > 0 || flag.Hidden {
return return
} }
format := "" format := ""
@ -200,7 +200,7 @@ func genMarkdown(cmd *Command, header *GenManHeader) []byte {
children := cmd.Commands() children := cmd.Commands()
sort.Sort(byName(children)) sort.Sort(byName(children))
for _, c := range children { for _, c := range children {
if len(c.Deprecated) != 0 || c == cmd.helpCommand { if !c.IsAvailableCommand() || c == cmd.helpCommand {
continue continue
} }
fmt.Fprintf(buf, "**%s-%s(%s)**, ", dashCommandName, c.Name(), header.Section) 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)) sort.Sort(byName(children))
for _, child := range children { for _, child := range children {
if len(child.Deprecated) > 0 || child == cmd.helpCommand { if !child.IsAvailableCommand() || child == cmd.helpCommand {
continue continue
} }
cname := name + " " + child.Name() 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) { func (cmd *Command) GenMarkdownTreeCustom(dir string, filePrepender func(string) string, linkHandler func(string) string) {
for _, c := range cmd.Commands() { for _, c := range cmd.Commands() {
if len(c.Deprecated) != 0 || c == cmd.helpCommand { if !c.IsAvailableCommand() || c == cmd.helpCommand {
continue continue
} }
c.GenMarkdownTreeCustom(dir, filePrepender, linkHandler) c.GenMarkdownTreeCustom(dir, filePrepender, linkHandler)