@@ -64,15 +64,24 @@ func DefaultShortHelp(c *CommandInfo) string {
6464 return strings .Compare (nameToCmpA , nameToCmpB )
6565 })
6666
67- var nonCondensedOpts bool
68- for i := range c .Opts {
69- if l := len (c .Opts [i ].optUsgNameAndArg ()); l >= helpShortMsgMaxFirstColLen {
70- nonCondensedOpts = true
71- break
67+ // First we need to determine the length of the longest left padded 'name(s) + value
68+ // name' for the options (meaing which `-s, --long <arg>` is the longest when spacing
69+ // is added for any absent short names). This will determine if we ouptut 'condensed'
70+ // option data or not, and (if we do condensed) what the right padding should be for
71+ // the option name columns that are shorter than the longest one.
72+ optLeftPaddedNames := make ([]string , len (opts ))
73+ optNameColWidth := 0
74+ for i := range opts {
75+ optLeftPaddedNames [i ] = opts [i ].leftPaddedNames ()
76+ if l := len (optLeftPaddedNames [i ]); l > optNameColWidth {
77+ optNameColWidth = l
7278 }
7379 }
7480
75- if nonCondensedOpts {
81+ // If the name column width would be longer than the (arbitrary) max width, then we'll
82+ // output 'non-condensed' lines of option data so it won't all look awkwardly crammed
83+ // off to the right.
84+ if optNameColWidth > helpShortMsgMaxFirstColLen {
7685 for _ , o := range opts {
7786 desc := o .HelpBlurb
7887 if o .IsRequired {
@@ -106,14 +115,7 @@ func DefaultShortHelp(c *CommandInfo) string {
106115 u .WriteByte ('\n' )
107116 }
108117 } else {
109- var optNameColWidth int
110- for i := range c .Opts {
111- if l := len (c .Opts [i ].optUsgNameAndArg ()); l > optNameColWidth && l <= helpShortMsgMaxFirstColLen {
112- optNameColWidth = l
113- }
114- }
115- for _ , o := range opts {
116- paddedNameAndArg := fmt .Sprintf (" %-*s" , optNameColWidth , o .optUsgNameAndArg ())
118+ for i , o := range opts {
117119 desc := o .HelpBlurb
118120 if o .IsRequired {
119121 desc += " (required)"
@@ -124,13 +126,8 @@ func DefaultShortHelp(c *CommandInfo) string {
124126 if o .EnvVar != "" {
125127 desc += " [$" + o .EnvVar + "]"
126128 }
127- content := paddedNameAndArg
128- if len (paddedNameAndArg ) > helpShortMsgMaxFirstColLen {
129- content += "\n " + strings .Repeat (" " , optNameColWidth + 5 )
130- } else {
131- content += " "
132- }
133- content += wrapBlurb (desc , len (paddedNameAndArg )+ 3 , helpMsgTextWidth )
129+ content := fmt .Sprintf (" %-*s " , optNameColWidth , optLeftPaddedNames [i ])
130+ content += wrapBlurb (desc , len (content ), helpMsgTextWidth )
134131 u .WriteString (content )
135132 u .WriteByte ('\n' )
136133 }
@@ -346,7 +343,7 @@ func DefaultFullHelp(c *CommandInfo) string {
346343 return u .String ()
347344}
348345
349- func (o * InputInfo ) optUsgNameAndArg () string {
346+ func (o * InputInfo ) leftPaddedNames () string {
350347 var s string
351348 if o .NameShort != "" {
352349 s += "-" + o .NameShort
0 commit comments