Help improvements
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by spf13 · committed by spf13 · parent 6b3d2f2 · Browse files · 2 files·+27 added−14 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
2 files+27 added−14 removed
- other:+27 added−14 removed
Provenance
- Commit
2d02c31- Tree
e18c3e0- Author
spf13 <steve.francia@gmail.com>- Committer
spf13 <steve.francia@gmail.com>- Parent
6b3d2f2
2 files changed+27 added−14 removed
modified:command.go+10 added
199 unmodified lines200 // Output the usage for the command201 // Used when a user provides invalid input202 // Can be defined by user by overriding Commander.UsageFunc203 func (c *Command) Usage() error {204+ c.mergePersistentFlags()205 err := c.Commander().UsageFunc(c)206 return err207 }208 209+// Output the help for the command210+// Used when a user calls help [command]211+// by the default HelpFunc in the commander212+func (c *Command) Help() error {213+ c.mergePersistentFlags()214+ err := tmpl(c.Commander().Out(), c.Commander().HelpTemplate, c)215+ return err216+}217+218 func (c *Command) UsageString() string {219 tmpOutput := c.Commander().cmdr.output220 bb := new(bytes.Buffer)221 c.Commander().SetOutput(bb)204 unmodified linesmodified:commander.go+17 added-14 removed
31 unmodified lines32 UsageFunc func(*Command) error // Usage can be defined by application33 UsageTemplate string // Can be defined by Application34 HelpTemplate string // Can be defined by Application35 HelpFunc func(*Command, []string) // Help can be defined by application36- HelpCommand *Command36+ HelpCommand *Command // The help command37+ cmd *Command // The command version of itself38 }39 40 // Provide the user with a new commander.41 func NewCommander() (c *Commander) {7 unmodified lines49 50 func (c *Commander) initHelp() {51 if c.HelpCommand == nil {52 c.HelpCommand = &Command{52- Use: "help [command to learn about]",53+ Use: "help [command]",54 Short: "Help about any command",55 Long: `Help provides help for any command in the application.56 Simply type ` + c.Name() + ` help [path to command] for full details.`,57 Run: c.HelpFunc,40 unmodified lines98 return err99 }100 101 func (cmdr *Commander) defaultHelp(c *Command, args []string) {101- cmd, _, e := c.Root().Find(args)102- if cmd == nil {103- cmdr.Printf("Unknown help topic %#q. Run '%v help'.\n", args, cmdr.Name())102+ if len(args) == 0 {103+ // Help called without any topic, calling on root104+ c.Root().Help()105 return106 }106- if e != nil {107- cmdr.Printf("Unknown help topic %#q. Run '%v help'.\n", args, cmdr.Name())107+108+ cmd, _, e := c.Root().Find(args)109+ if cmd == nil || e != nil {110+ cmdr.Printf("Unknown help topic %#q.", args)111+112+ c.Root().Usage()113 } else {109- err := tmpl(cmdr.out(), cmdr.HelpTemplate, cmd)114+ err := cmd.Help()115 if err != nil {116 c.Println(err)117 }118 }16 unmodified lines135 {{.UseLine}}{{if .HasFlags}} [flags]{{end}}{{end}}{{if .HasSubCommands}}136 {{ .CommandPath}} [command]{{end}}137 {{ if .HasSubCommands}}138 Available Commands: {{range .Commands}}{{if .Runnable}}134- {{.Use | printf "%-11s"}} :: {{.Short}}{{end}}{{end}}139+ {{.Use | printf "%-15s"}} :: {{.Short}}{{end}}{{end}}140 {{end}}141 {{ if .HasFlags}} Available Flags:137-{{.Flags.FlagUsages}}{{end}}{{if and (gt .Commands 0) (gt .Parent.Commands 1) }}138-Additional help topics: {{if gt .Commands 0 }}{{range .Commands}}{{if not .Runnable}}139- {{.CommandPath | printf "%-11s"}} :: {{.Short}}{{end}}{{end}}{{end}}{{if gt .Parent.Commands 1 }}{{range .Parent.Commands}}{{if .Runnable}}{{if not (eq .Name $cmd.Name) }}{{end}}140- {{.CommandPath | printf "%-11s"}} :: {{.Short}}{{end}}{{end}}{{end}}141-142+{{.Flags.FlagUsages}}{{end}}{{if .HasParent}}{{if and (gt .Commands 0) (gt .Parent.Commands 1) }}143+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}}144+ {{.CommandPath | printf "%-11s"}} :: {{.Short}}{{end}}{{end}}{{end}}{{end}}145 {{end}}146 Use "{{.Commander.Name}} help [command]" for more information about that command.147 `148 4 unmodified lines