Skip to content

Commit

Permalink
update help and option suggestions on cmds acting as root cmd
Browse files Browse the repository at this point in the history
  • Loading branch information
juanvallejo committed Aug 5, 2016
1 parent 8e93729 commit 16e318e
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 7 deletions.
24 changes: 18 additions & 6 deletions pkg/cmd/templates/templater.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ type FlagExposer interface {
ExposeFlags(cmd *cobra.Command, flags ...string) FlagExposer
}

// ActsAsRootCommand displays command usage using a root command usage template
func ActsAsRootCommand(cmd *cobra.Command, filters []string, groups ...CommandGroup) FlagExposer {
if cmd == nil {
panic("nil root command")
Expand Down Expand Up @@ -192,14 +193,25 @@ func (t *templater) optionsCmdFor(c *cobra.Command) string {
if !c.Runnable() {
return ""
}
rootCmdStructure := t.parents(c)
for i := len(rootCmdStructure) - 1; i >= 0; i-- {
cmd := rootCmdStructure[i]
if _, _, err := cmd.Find([]string{"options"}); err == nil {
return cmd.CommandPath() + " options"

parentCmdHasOptsArg := false
currentCmdHasOptsArg := false

if t.RootCmd.HasParent() {
if _, _, err := t.RootCmd.Parent().Find([]string{"options"}); err == nil {
parentCmdHasOptsArg = true
}
}
return ""

if _, _, err := t.RootCmd.Find([]string{"options"}); err == nil {
currentCmdHasOptsArg = true
}

if (parentCmdHasOptsArg && currentCmdHasOptsArg) || !t.RootCmd.HasParent() {
return t.RootCmd.CommandPath() + " options"
}

return t.RootCmd.Parent().CommandPath() + " options"
}

func (t *templater) usageLine(c *cobra.Command) string {
Expand Down
2 changes: 1 addition & 1 deletion pkg/cmd/templates/templates.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ Examples:
{{end}}{{end}}{{ if or $visibleFlags.HasFlags $explicitlyExposedFlags.HasFlags}}
Options:
{{ if $visibleFlags.HasFlags}}{{flagsUsages $visibleFlags}}{{end}}{{ if $explicitlyExposedFlags.HasFlags}}{{flagsUsages $explicitlyExposedFlags}}{{end}}{{end}}{{ if .HasSubCommands }}
Use "{{$rootCmd}} help <command>" for more information about a given command.{{end}}{{ if $optionsCmdFor}}
Use "{{$rootCmd}} <command> --help" for more information about a given command.{{end}}{{ if $optionsCmdFor}}
Use "{{$optionsCmdFor}}" for a list of global command-line options (applies to all commands).{{end}}`

optionsHelpTemplate = ``
Expand Down

0 comments on commit 16e318e

Please sign in to comment.