Skip to content

Commit

Permalink
Several 'help' fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
fabianofranz committed Apr 7, 2015
1 parent f576d29 commit 37b839e
Showing 1 changed file with 19 additions and 4 deletions.
23 changes: 19 additions & 4 deletions command.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,14 @@ package cobra
import (
"bytes"
"fmt"
"github.com/inconshreveable/mousetrap"
flag "github.com/spf13/pflag"
"io"
"os"
"runtime"
"strings"
"time"

"github.com/inconshreveable/mousetrap"
flag "github.com/spf13/pflag"
)

// Command is just that, a command for your application.
Expand Down Expand Up @@ -370,6 +371,9 @@ func (c *Command) Find(arrs []string) (*Command, []string, error) {
// only accept a single prefix match - multiple matches would be ambiguous
if len(matches) == 1 {
return innerfind(matches[0], argsMinusX(args, argsWOflags[0]))
} else if len(matches) == 0 && len(args) > 0 && args[0] == "help" {
// special case help command
return innerfind(c, argsMinusX(append(args, "--help"), argsWOflags[0]))
}
}
}
Expand Down Expand Up @@ -751,7 +755,12 @@ func (c *Command) Runnable() bool {

// Determine if the command has children commands
func (c *Command) HasSubCommands() bool {
return len(c.commands) > 0
for _, sub := range c.commands {
if sub.Runnable() {
return true
}
}
return false
}

func (c *Command) HasRunnableSiblings() bool {
Expand Down Expand Up @@ -790,6 +799,12 @@ func (c *Command) HasParent() bool {
return c.parent != nil
}

func (c *Command) assureHelpFlag() {
if c.Flags().Lookup("help") == nil && c.PersistentFlags().Lookup("help") == nil {
c.PersistentFlags().BoolVarP(&c.helpFlagVal, "help", "h", false, "help for "+c.Name())
}
}

// Get the complete FlagSet that applies to this command (local and persistent declared here and by all parents)
func (c *Command) Flags() *flag.FlagSet {
if c.flags == nil {
Expand All @@ -798,7 +813,7 @@ func (c *Command) Flags() *flag.FlagSet {
c.flagErrorBuf = new(bytes.Buffer)
}
c.flags.SetOutput(c.flagErrorBuf)
c.PersistentFlags().BoolVarP(&c.helpFlagVal, "help", "h", false, "help for "+c.Name())
c.assureHelpFlag()
}
return c.flags
}
Expand Down

0 comments on commit 37b839e

Please sign in to comment.