Skip to content
Myr

Right aligns "Use" and "Additional help topics" even if they are longer than the min padding

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by awilliams · committed by awilliams · parent 6dd0aa1 · 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

2 files+42 added−3 removed

modified:cobra.go+7 added
15 unmodified lines
1617package cobra1819import (20 "fmt"21 "io"22 "reflect"23 "strconv"24 "strings"
41 unmodified lines
66 }67 return false68}6970func rpad(s string, padding int) string {71 template := fmt.Sprintf("%%-%ds", padding)72 return fmt.Sprintf(template, s)73}7475// tmpl executes the given template text on data, writing the result to w.76func 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 lines
modified:command.go+35 added-3 removed
47 unmodified lines
48 // 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 int5556 flagErrorBuf *bytes.Buffer5758 args []string
96 unmodified lines
155 }156 }157}158159var minUsagePadding int = 25160161func (c *Command) UsagePadding() int {162 if c.parent == nil || minUsagePadding > c.parent.commandsMaxUseLen {163 return minUsagePadding164 } else {165 return c.parent.commandsMaxUseLen166 }167}168169var minCommandPathPadding int = 11170171func (c *Command) CommandPathPadding() int {172 if c.parent == nil || minCommandPathPadding > c.parent.commandsMaxCommandPathLen {173 return minCommandPathPadding174 } else {175 return c.parent.commandsMaxCommandPathLen176 }177}178179func (c *Command) UsageTemplate() string {180 if c.usageTemplate != "" {181 return c.usageTemplate182 }
6 unmodified lines
189 {{.UseLine}}{{if .HasFlags}} [flags]{{end}}{{end}}{{if .HasSubCommands}}190 {{ .CommandPath}} [command]{{end}}191{{ if .HasSubCommands}}192Available 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) }}174Additional 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}}197Additional 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}}200Use "{{.Root.Name}} help [command]" for more information about that command.201`202 }
181 unmodified lines
384 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