Help improvements

This commit is contained in:
spf13 2013-09-29 02:02:49 -04:00
parent 6b3d2f2e9a
commit 2d02c310f3
2 changed files with 27 additions and 14 deletions

View file

@ -201,10 +201,20 @@ func (c *Command) Printf(format string, i ...interface{}) {
// Used when a user provides invalid input // Used when a user provides invalid input
// Can be defined by user by overriding Commander.UsageFunc // Can be defined by user by overriding Commander.UsageFunc
func (c *Command) Usage() error { func (c *Command) Usage() error {
c.mergePersistentFlags()
err := c.Commander().UsageFunc(c) err := c.Commander().UsageFunc(c)
return err return err
} }
// Output the help for the command
// Used when a user calls help [command]
// by the default HelpFunc in the commander
func (c *Command) Help() error {
c.mergePersistentFlags()
err := tmpl(c.Commander().Out(), c.Commander().HelpTemplate, c)
return err
}
func (c *Command) UsageString() string { func (c *Command) UsageString() string {
tmpOutput := c.Commander().cmdr.output tmpOutput := c.Commander().cmdr.output
bb := new(bytes.Buffer) bb := new(bytes.Buffer)

View file

@ -33,7 +33,8 @@ type Commander struct {
UsageTemplate string // Can be defined by Application UsageTemplate string // Can be defined by Application
HelpTemplate string // Can be defined by Application HelpTemplate string // Can be defined by Application
HelpFunc func(*Command, []string) // Help can be defined by application HelpFunc func(*Command, []string) // Help can be defined by application
HelpCommand *Command HelpCommand *Command // The help command
cmd *Command // The command version of itself
} }
// Provide the user with a new commander. // Provide the user with a new commander.
@ -49,7 +50,7 @@ func NewCommander() (c *Commander) {
func (c *Commander) initHelp() { func (c *Commander) initHelp() {
if c.HelpCommand == nil { if c.HelpCommand == nil {
c.HelpCommand = &Command{ c.HelpCommand = &Command{
Use: "help [command to learn about]", Use: "help [command]",
Short: "Help about any command", Short: "Help about any command",
Long: `Help provides help for any command in the application. Long: `Help provides help for any command in the application.
Simply type ` + c.Name() + ` help [path to command] for full details.`, Simply type ` + c.Name() + ` help [path to command] for full details.`,
@ -98,15 +99,19 @@ func (cmdr *Commander) defaultUsage(c *Command) error {
} }
func (cmdr *Commander) defaultHelp(c *Command, args []string) { func (cmdr *Commander) defaultHelp(c *Command, args []string) {
cmd, _, e := c.Root().Find(args) if len(args) == 0 {
if cmd == nil { // Help called without any topic, calling on root
cmdr.Printf("Unknown help topic %#q. Run '%v help'.\n", args, cmdr.Name()) c.Root().Help()
return return
} }
if e != nil {
cmdr.Printf("Unknown help topic %#q. Run '%v help'.\n", args, cmdr.Name()) cmd, _, e := c.Root().Find(args)
if cmd == nil || e != nil {
cmdr.Printf("Unknown help topic %#q.", args)
c.Root().Usage()
} else { } else {
err := tmpl(cmdr.out(), cmdr.HelpTemplate, cmd) err := cmd.Help()
if err != nil { if err != nil {
c.Println(err) c.Println(err)
} }
@ -131,14 +136,12 @@ Usage: {{if .Runnable}}
{{ .CommandPath}} [command]{{end}} {{ .CommandPath}} [command]{{end}}
{{ if .HasSubCommands}} {{ if .HasSubCommands}}
Available Commands: {{range .Commands}}{{if .Runnable}} Available Commands: {{range .Commands}}{{if .Runnable}}
{{.Use | printf "%-11s"}} :: {{.Short}}{{end}}{{end}} {{.Use | printf "%-15s"}} :: {{.Short}}{{end}}{{end}}
{{end}} {{end}}
{{ if .HasFlags}} Available Flags: {{ if .HasFlags}} Available Flags:
{{.Flags.FlagUsages}}{{end}}{{if and (gt .Commands 0) (gt .Parent.Commands 1) }} {{.Flags.FlagUsages}}{{end}}{{if .HasParent}}{{if and (gt .Commands 0) (gt .Parent.Commands 1) }}
Additional help topics: {{if gt .Commands 0 }}{{range .Commands}}{{if not .Runnable}} Additional help topics: {{if gt .Commands 0 }}{{range .Commands}}{{if not .Runnable}} {{.CommandPath | printf "%-11s"}} :: {{.Short}}{{end}}{{end}}{{end}}{{if gt .Parent.Commands 1 }}{{range .Parent.Commands}}{{if .Runnable}}{{if not (eq .Name $cmd.Name) }}{{end}}
{{.CommandPath | printf "%-11s"}} :: {{.Short}}{{end}}{{end}}{{end}}{{if gt .Parent.Commands 1 }}{{range .Parent.Commands}}{{if .Runnable}}{{if not (eq .Name $cmd.Name) }}{{end}} {{.CommandPath | printf "%-11s"}} :: {{.Short}}{{end}}{{end}}{{end}}{{end}}
{{.CommandPath | printf "%-11s"}} :: {{.Short}}{{end}}{{end}}{{end}}
{{end}} {{end}}
Use "{{.Commander.Name}} help [command]" for more information about that command. Use "{{.Commander.Name}} help [command]" for more information about that command.
` `