Merge pull request #8 from awilliams/master
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Right aligns "Use" and "Additional help topics"
authored by Steve Francia · committed by Steve Francia · parents 6dd0aa1, 2e73a96 · Browse files · 2 files·+42 added−3 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+42 added−3 removed
- other:+42 added−3 removed
Provenance
- Commit
1e1ff9e- Tree
564de55- Author
Steve Francia <steve.francia@gmail.com>- Committer
Steve Francia <steve.francia@gmail.com>- Parent 1
6dd0aa1- Parent 2
2e73a96
2 files changed+42 added−3 removed
modified:cobra.go+7 added
15 unmodified lines16 17 package cobra18 19 import (20+ "fmt"21 "io"22 "reflect"23 "strconv"24 "strings"41 unmodified lines66 }67 return false68 }69 70+func rpad(s string, padding int) string {71+ template := fmt.Sprintf("%%-%ds", padding)72+ return fmt.Sprintf(template, s)73+}74+75 // tmpl executes the given template text on data, writing the result to w.76 func tmpl(w io.Writer, text string, data interface{}) error {77 t := template.New("top")78 t.Funcs(template.FuncMap{79 "trim": strings.TrimSpace,80+ "rpad": rpad,81 "gt": Gt,82 "eq": Eq,83 })84 template.Must(t.Parse(text))2 unmodified linesmodified:command.go+35 added-3 removed
47 unmodified lines48 // Commands is the list of commands supported by this program.49 commands []*Command50 // Parent Command for this command51 parent *Command52+ // max lengths of commands' string lengths for use in padding53+ commandsMaxUseLen int54+ commandsMaxCommandPathLen int55 56 flagErrorBuf *bytes.Buffer57 58 args []string96 unmodified lines155 }156 }157 }158 159+var minUsagePadding int = 25160+161+func (c *Command) UsagePadding() int {162+ if c.parent == nil || minUsagePadding > c.parent.commandsMaxUseLen {163+ return minUsagePadding164+ } else {165+ return c.parent.commandsMaxUseLen166+ }167+}168+169+var minCommandPathPadding int = 11170+171+func (c *Command) CommandPathPadding() int {172+ if c.parent == nil || minCommandPathPadding > c.parent.commandsMaxCommandPathLen {173+ return minCommandPathPadding174+ } else {175+ return c.parent.commandsMaxCommandPathLen176+ }177+}178+179 func (c *Command) UsageTemplate() string {180 if c.usageTemplate != "" {181 return c.usageTemplate182 }6 unmodified lines189 {{.UseLine}}{{if .HasFlags}} [flags]{{end}}{{end}}{{if .HasSubCommands}}190 {{ .CommandPath}} [command]{{end}}191 {{ if .HasSubCommands}}192 Available Commands: {{range .Commands}}{{if .Runnable}}170- {{.Use | printf "%-15s"}} :: {{.Short}}{{end}}{{end}}193+ {{rpad .Use .UsagePadding }} :: {{.Short}}{{end}}{{end}}194 {{end}}195 {{ if .HasFlags}} Available Flags:196 {{.Flags.FlagUsages}}{{end}}{{if .HasParent}}{{if and (gt .Commands 0) (gt .Parent.Commands 1) }}174-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}}175- {{.CommandPath | printf "%-11s"}} :: {{.Short}}{{end}}{{end}}{{end}}{{end}}197+Additional help topics: {{if gt .Commands 0 }}{{range .Commands}}{{if not .Runnable}} {{rpad .CommandPath .CommandPathPadding}} :: {{.Short}}{{end}}{{end}}{{end}}{{if gt .Parent.Commands 1 }}{{range .Parent.Commands}}{{if .Runnable}}{{if not (eq .Name $cmd.Name) }}{{end}}198+ {{rpad .CommandPath .CommandPathPadding}} :: {{.Short}}{{end}}{{end}}{{end}}{{end}}199 {{end}}200 Use "{{.Root.Name}} help [command]" for more information about that command.201 `202 }181 unmodified lines384 if cmds[i] == c {385 panic("Command can't be a child of itself")386 }387 cmds[i].parent = c388+ // update max lengths389+ usageLen := len(x.Use)390+ if usageLen > c.commandsMaxUseLen {391+ c.commandsMaxUseLen = usageLen392+ }393+ commandPathLen := len(x.CommandPath())394+ if commandPathLen > c.commandsMaxCommandPathLen {395+ c.commandsMaxCommandPathLen = commandPathLen396+ }397 c.commands = append(c.commands, x)398 }399 }400 243 unmodified lines