Skip to content
Myr

Fix additional help topics template

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

The additional help topics were really hard to ever get to show. The required conditionals were difficult to meet and did not seem to really be logical. Problems I see: 1) the top level command could never have additional topics. 2) you must have at least one sibling command AND one subcommand 3) we had the AND above, but then test both conditionals a second time 4) if the sub command was runnable we wouldn't print anything 5) if the sibling commands were not runnable we wouldn't print anything 4+5) it's possible that we printed "Additional help topics:" with nothing following it 6) We always printed ourselves as a sibling in the additional info Whew, I think I fixed all of those! Again, using https://github.com/eparis/readable-golang-template I'm actually able to visualize the template and see this craziness. The conditionals BEFORE this change: {{if .HasParent}} {{if and (gt .Commands 0) (gt .Parent.Commands 1) }} 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}} {{rpad .CommandPath .CommandPathPadding}} {{.Short}} {{end}} {{end}} {{end}} {{end}} {{end}} The conditionals AFTER this change: {{if or (.HasHelpSubCommands) (.HasRunnableSiblings)}} Additional help topics: {{if .HasHelpSubCommands}} {{range .Commands}} {{if not .Runnable}} {{rpad .CommandPath .CommandPathPadding}} {{.Short}} {{end}} {{end}} {{end}} {{if .HasRunnableSiblings }} {{range .Parent.Commands}} {{if .Runnable}} {{if not (eq .Name $cmd.Name) }} {{rpad .CommandPath .CommandPathPadding}} {{.Short}} {{end}} {{end}} {{end}} {{end}} {{end}}

authored by Eric Paris · committed by Eric Paris · parent c64442a · Browse files · 1 file+35 added−4 removed
This commit

State

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

Diff stats

1 file+35 added−4 removed

  • other:+35 added−4 removed

1 file+35 added−4 removed

modified:command.go+35 added-4 removed
221 unmodified lines
222{{end}}{{if .HasExample}}223224Examples:225{{ .Example }}226{{end}}{{ if .HasSubCommands}}226{{end}}{{ if .HasRunnableSubCommands}}227228Available Commands: {{range .Commands}}{{if .Runnable}}229 {{rpad .Name .NamePadding }} {{.Short}}{{end}}{{end}}230{{end}}231{{ if .HasLocalFlags}}Flags:232{{.LocalFlags.FlagUsages}}{{end}}233{{ if .HasInheritedFlags}}Global Flags:234{{.InheritedFlags.FlagUsages}}{{end}}{{if .HasParent}}{{if and (gt .Commands 0) (gt .Parent.Commands 1) }}235Additional 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) }}236 {{rpad .CommandPath .CommandPathPadding}} {{.Short}}{{end}}{{end}}{{end}}{{end}}{{end}}234{{.InheritedFlags.FlagUsages}}{{end}}{{if or (.HasHelpSubCommands) (.HasRunnableSiblings)}}235Additional help topics: {{if .HasHelpSubCommands}}{{range .Commands}}{{if not .Runnable}} {{rpad .CommandPath .CommandPathPadding}} {{.Short}}{{end}}{{end}}{{end}}{{if .HasRunnableSiblings }}{{range .Parent.Commands}}{{if .Runnable}}{{if not (eq .Name $cmd.Name) }}236 {{rpad .CommandPath .CommandPathPadding}} {{.Short}}{{end}}{{end}}{{end}}{{end}}237{{end}}{{ if .HasSubCommands }}238Use "{{.Root.Name}} help [command]" for more information about a command.239{{end}}`240 }
545 unmodified lines
786func (c *Command) HasSubCommands() bool {787 return len(c.commands) > 0788}789790func (c *Command) HasRunnableSiblings() bool {791 if !c.HasParent() {792 return false793 }794 for _, sub := range c.parent.commands {795 if sub.Runnable() {796 return true797 }798 }799 return false800}801802func (c *Command) HasHelpSubCommands() bool {803 for _, sub := range c.commands {804 if !sub.Runnable() {805 return true806 }807 }808 return false809}810811// Determine if the command has runnable children commands812func (c *Command) HasRunnableSubCommands() bool {813 for _, sub := range c.commands {814 if sub.Runnable() {815 return true816 }817 }818 return false819}820821// Determine if the command is a child command822func (c *Command) HasParent() bool {823 return c.parent != nil824}
165 unmodified lines