Skip to content
Myr

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

2 files+27 added−14 removed

modified:command.go+10 added
199 unmodified lines
200// Output the usage for the command201// Used when a user provides invalid input202// Can be defined by user by overriding Commander.UsageFunc203func (c *Command) Usage() error {204 c.mergePersistentFlags()205 err := c.Commander().UsageFunc(c)206 return err207}208209// Output the help for the command210// Used when a user calls help [command]211// by the default HelpFunc in the commander212func (c *Command) Help() error {213 c.mergePersistentFlags()214 err := tmpl(c.Commander().Out(), c.Commander().HelpTemplate, c)215 return err216}217218func (c *Command) UsageString() string {219 tmpOutput := c.Commander().cmdr.output220 bb := new(bytes.Buffer)221 c.Commander().SetOutput(bb)
204 unmodified lines
modified:commander.go+17 added-14 removed
31 unmodified lines
32 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}3940// Provide the user with a new commander.41func NewCommander() (c *Commander) {
7 unmodified lines
4950func (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 lines
98 return err99}100101func (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())107108 cmd, _, e := c.Root().Find(args)109 if cmd == nil || e != nil {110 cmdr.Printf("Unknown help topic %#q.", args)111112 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 lines
135 {{.UseLine}}{{if .HasFlags}} [flags]{{end}}{{end}}{{if .HasSubCommands}}136 {{ .CommandPath}} [command]{{end}}137{{ if .HasSubCommands}}138Available 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) }}138Additional 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}}141142{{.Flags.FlagUsages}}{{end}}{{if .HasParent}}{{if and (gt .Commands 0) (gt .Parent.Commands 1) }}143Additional 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}}146Use "{{.Commander.Name}} help [command]" for more information about that command.147`148
4 unmodified lines