diff --git a/Godeps/_workspace/src/github.com/spf13/cobra/command.go b/Godeps/_workspace/src/github.com/spf13/cobra/command.go index b65f59b016a2..82299b9b81e6 100644 --- a/Godeps/_workspace/src/github.com/spf13/cobra/command.go +++ b/Godeps/_workspace/src/github.com/spf13/cobra/command.go @@ -308,6 +308,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])) } } } @@ -344,6 +347,7 @@ func (c *Command) Root() *Command { func (c *Command) findAndExecute(args []string) (err error) { cmd, a, e := c.Find(args) + if e != nil { return e } @@ -425,6 +429,8 @@ func (c *Command) Execute() (err error) { args = c.args } + c.assureHelpFlag() + if len(args) == 0 { // Only the executable is called and the root is runnable, run it if c.Runnable() { @@ -563,6 +569,7 @@ func (c *Command) Usage() error { // Used when a user calls help [command] // by the default HelpFunc in the commander func (c *Command) Help() error { + c.mergePersistentFlags() err := tmpl(c.Out(), c.HelpTemplate(), c) return err } @@ -675,7 +682,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 } // Determine if the command is a child command @@ -691,11 +703,18 @@ 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 } +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()) + } + c.mergePersistentFlags() +} + // Get the local FlagSet specifically set in the current command func (c *Command) LocalFlags() *flag.FlagSet { c.mergePersistentFlags() diff --git a/hack/test-cmd.sh b/hack/test-cmd.sh index 5e67cc35ba27..cf6d6ba19001 100755 --- a/hack/test-cmd.sh +++ b/hack/test-cmd.sh @@ -103,6 +103,39 @@ export OPENSHIFT_PROFILE="${CLI_PROFILE-}" [ "$(openshift kubectl)" ] [ "$(openshift kube 2>&1)" ] +# help for root commands must be consistent +[ "$(openshift | grep 'OpenShift for Admins')" ] +[ "$(osc | grep 'OpenShift Client')" ] +[ "$(openshift cli | grep 'OpenShift Client')" ] +[ "$(openshift kubectl | grep 'OpenShift Client')" ] + +# help for root commands with --help flag must be consistent +[ "$(openshift --help 2>&1 | grep 'OpenShift for Admins')" ] +[ "$(osc --help 2>&1 | grep 'OpenShift Client')" ] +[ "$(openshift cli --help 2>&1 | grep 'OpenShift Client')" ] +[ "$(openshift kubectl --help 2>&1 | grep 'OpenShift Client')" ] + +# help for root commands through help command must be consistent +[ "$(openshift help cli 2>&1 | grep 'OpenShift Client')" ] +[ "$(openshift help kubectl 2>&1 | grep 'OpenShift Client')" ] + +# help for given command with --help flag must be consistent +[ "$(osc get --help 2>&1 | grep 'Display one or many resources')" ] +[ "$(openshift cli get --help 2>&1 | grep 'Display one or many resources')" ] +[ "$(openshift kubectl get --help 2>&1 | grep 'Display one or many resources')" ] +[ "$(openshift start --help 2>&1 | grep 'Start an OpenShift server')" ] + +# help for given command through help command must be consistent +[ "$(osc help get 2>&1 | grep 'Display one or many resources')" ] +[ "$(openshift cli help get 2>&1 | grep 'Display one or many resources')" ] +[ "$(openshift kubectl help get 2>&1 | grep 'Display one or many resources')" ] +[ "$(openshift help start 2>&1 | grep 'Start an OpenShift server')" ] + +# runnable commands with required flags must error consistently +[ "$(osc get 2>&1 | grep 'you must provide one or more resources')" ] +[ "$(openshift cli get 2>&1 | grep 'you must provide one or more resources')" ] +[ "$(openshift kubectl get 2>&1 | grep 'you must provide one or more resources')" ] + osc get pods --match-server-version osc create -f examples/hello-openshift/hello-pod.json osc delete pods hello-openshift