No newline after Flags in usage

The flags usage template from pflags has a trailing \n. We need to
include a newline in case there are no flags in our template. This will
trim the newline from the end of the flags from pflag and we can do it
right outselves.
This commit is contained in:
Eric Paris 2015-09-11 18:44:03 -04:00
parent 42498ec777
commit 8af2b2b89f
2 changed files with 15 additions and 6 deletions

View File

@ -23,10 +23,12 @@ import (
"strconv" "strconv"
"strings" "strings"
"text/template" "text/template"
"unicode"
) )
var templateFuncs template.FuncMap = template.FuncMap{ var templateFuncs template.FuncMap = template.FuncMap{
"trim": strings.TrimSpace, "trim": strings.TrimSpace,
"trimRightSpace": trimRightSpace,
"rpad": rpad, "rpad": rpad,
"gt": Gt, "gt": Gt,
"eq": Eq, "eq": Eq,
@ -113,6 +115,10 @@ func Eq(a interface{}, b interface{}) bool {
return false return false
} }
func trimRightSpace(s string) string {
return strings.TrimRightFunc(s, unicode.IsSpace)
}
//rpad adds padding to the right of a string //rpad adds padding to the right of a string
func rpad(s string, padding int) string { func rpad(s string, padding int) string {
template := fmt.Sprintf("%%-%ds", padding) template := fmt.Sprintf("%%-%ds", padding)

View File

@ -186,6 +186,9 @@ func (c *Command) UsageFunc() (f func(*Command) error) {
} else { } else {
return func(c *Command) error { return func(c *Command) error {
err := tmpl(c.Out(), c.UsageTemplate(), c) err := tmpl(c.Out(), c.UsageTemplate(), c)
if err != nil {
fmt.Print(err)
}
return err return err
} }
} }
@ -263,10 +266,10 @@ Available Commands:{{range .Commands}}{{if .IsAvailableCommand}}
{{rpad .Name .NamePadding }} {{.Short}}{{end}}{{end}}{{end}}{{ if .HasLocalFlags}} {{rpad .Name .NamePadding }} {{.Short}}{{end}}{{end}}{{end}}{{ if .HasLocalFlags}}
Flags: Flags:
{{.LocalFlags.FlagUsages}}{{end}}{{ if .HasInheritedFlags}} {{.LocalFlags.FlagUsages | trimRightSpace}}{{end}}{{ if .HasInheritedFlags}}
Global Flags: Global Flags:
{{.InheritedFlags.FlagUsages}}{{end}}{{if .HasHelpSubCommands}} {{.InheritedFlags.FlagUsages | trimRightSpace}}{{end}}{{if .HasHelpSubCommands}}
Additional help topics:{{range .Commands}}{{if .IsHelpCommand}} Additional help topics:{{range .Commands}}{{if .IsHelpCommand}}
{{rpad .CommandPath .CommandPathPadding}} {{.Short}}{{end}}{{end}}{{end}}{{ if .HasSubCommands }} {{rpad .CommandPath .CommandPathPadding}} {{.Short}}{{end}}{{end}}{{end}}{{ if .HasSubCommands }}