alecthomas / kingpin

CONTRIBUTIONS ONLY: A Go (golang) command line and flag parser
MIT License
3.5k stars 273 forks source link

usage: fix formatCmdUsage #280

Closed balupton closed 5 years ago

balupton commented 5 years ago

This fixes the behaviour of formatCmdUsage

Template file to test:

{{define "FormatCommands"}}\
{{range .FlattenedCommands}}
## {{formatCmdUsage $ .}}
{{template "FormatUsage" .}}\
{{end}}\
{{end}}\
\
{{define "FormatUsage"}}
{{if .Help}}\
{{.Help|Wrap 0}}\
{{end}}\
{{end}}\
\
{{if not .Context.SelectedCommand}}\
# <command> [<args> ...]
{{template "FormatUsage" .App}}
{{end}}

{{if .Context.Flags}}
Flags:
{{.Context.Flags|FlagsToTwoColumns|FormatTwoColumns}}
{{end}}

{{if .Context.Args}}
Args:
{{.Context.Args|ArgsToTwoColumns|FormatTwoColumns}}
{{end}}

{{if .Context.SelectedCommand}}
{{if len .Context.SelectedCommand.Commands}}
Subcommands:
{{template "FormatCommands" .Context.SelectedCommand}}
{{end}}\
{{else if .App.Commands}}
Commands:
{{template "FormatCommands" .App}}
{{end}}