Skip to content

Commit e361e11

Browse files
committed
refactor: put option blurbs on new line when one breaches threshold
1 parent ab0d530 commit e361e11

File tree

2 files changed

+77
-30
lines changed

2 files changed

+77
-30
lines changed

examples_test.go

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -325,7 +325,9 @@ func ExampleDefaultShortHelp_complex() {
325325
Opt(cli.NewOpt("bb").
326326
Help("another option that is required and has a really long blurb to show how it will be wrapped").
327327
Required()).
328-
Opt(cli.NewOpt("short-blurb-but-really-long-name").Help("another option")).
328+
Opt(cli.NewOpt("kind-of-a-long-name").
329+
Help("due to this option's name, the blurbs for each option on this command " +
330+
"will begin on their own non-indented lines")).
329331
Arg(cli.NewArg("posarg1").Required().Help("a required positional argument")).
330332
Arg(cli.NewArg("posarg2").Env("PA2").Help("an optional positional argument")).
331333
Opt(cli.NewBoolOpt("h").
@@ -343,12 +345,16 @@ func ExampleDefaultShortHelp_complex() {
343345
// example [options] [arguments]
344346
//
345347
// options:
346-
// --aa <arg> an option (default: def) [$AA]
347-
// --bb <arg> another option that is required and has a really long blurb to show
348-
// how it will be wrapped (required)
349-
// -h will show the default short help message
350-
// --short-blurb-but-really-long-name <arg>
351-
// another option
348+
// --aa <arg>
349+
// an option (default: def) [$AA]
350+
// --bb <arg>
351+
// another option that is required and has a really long blurb to show how it will be
352+
// wrapped (required)
353+
// -h
354+
// will show the default short help message
355+
// --kind-of-a-long-name <arg>
356+
// due to this option's name, the blurbs for each option on this command will begin on
357+
// their own non-indented lines
352358
//
353359
// arguments:
354360
// <posarg1> a required positional argument (required)

help.go

Lines changed: 64 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -64,33 +64,76 @@ func DefaultShortHelp(c *CommandInfo) string {
6464
return strings.Compare(nameToCmpA, nameToCmpB)
6565
})
6666

67-
var optNameColWidth int
67+
var nonCondensedOpts bool
6868
for i := range c.Opts {
69-
if l := len(c.Opts[i].optUsgNameAndArg()); l > optNameColWidth && l <= helpShortMsgMaxFirstColLen {
70-
optNameColWidth = l
69+
if l := len(c.Opts[i].optUsgNameAndArg()); l >= helpShortMsgMaxFirstColLen {
70+
nonCondensedOpts = true
71+
break
7172
}
7273
}
73-
for _, o := range opts {
74-
paddedNameAndArg := fmt.Sprintf(" %-*s", optNameColWidth, o.optUsgNameAndArg())
75-
desc := o.HelpBlurb
76-
if o.IsRequired {
77-
desc += " (required)"
78-
}
79-
if o.HasStrDefault {
80-
desc += fmt.Sprintf(" (default: %v)", o.StrDefault)
74+
75+
if nonCondensedOpts {
76+
for _, o := range opts {
77+
desc := o.HelpBlurb
78+
if o.IsRequired {
79+
desc += " (required)"
80+
}
81+
if o.HasStrDefault {
82+
desc += fmt.Sprintf(" (default: %v)", o.StrDefault)
83+
}
84+
if o.EnvVar != "" {
85+
desc += " [$" + o.EnvVar + "]"
86+
}
87+
var namesAndVal string
88+
{
89+
if o.NameShort != "" {
90+
namesAndVal += "-" + o.NameShort
91+
}
92+
if o.NameLong != "" {
93+
if o.NameShort != "" {
94+
namesAndVal += ", "
95+
}
96+
namesAndVal += "--" + o.NameLong
97+
}
98+
if an := o.optUsgArgName(); an != "" {
99+
namesAndVal += " " + an
100+
}
101+
}
102+
content := " " + namesAndVal
103+
content += "\n" + strings.Repeat(" ", 6)
104+
content += wrapBlurb(desc, 6, helpMsgTextWidth)
105+
u.WriteString(content)
106+
u.WriteByte('\n')
81107
}
82-
if o.EnvVar != "" {
83-
desc += " [$" + o.EnvVar + "]"
108+
} 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+
}
84114
}
85-
content := paddedNameAndArg
86-
if len(paddedNameAndArg) > helpShortMsgMaxFirstColLen {
87-
content += "\n" + strings.Repeat(" ", optNameColWidth+5)
88-
} else {
89-
content += " "
115+
for _, o := range opts {
116+
paddedNameAndArg := fmt.Sprintf(" %-*s", optNameColWidth, o.optUsgNameAndArg())
117+
desc := o.HelpBlurb
118+
if o.IsRequired {
119+
desc += " (required)"
120+
}
121+
if o.HasStrDefault {
122+
desc += fmt.Sprintf(" (default: %v)", o.StrDefault)
123+
}
124+
if o.EnvVar != "" {
125+
desc += " [$" + o.EnvVar + "]"
126+
}
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)
134+
u.WriteString(content)
135+
u.WriteByte('\n')
90136
}
91-
content += wrapBlurb(desc, len(paddedNameAndArg)+3, helpMsgTextWidth)
92-
u.WriteString(content)
93-
u.WriteByte('\n')
94137
}
95138

96139
if len(c.Args) > 0 {
@@ -218,14 +261,12 @@ func DefaultFullHelp(c *CommandInfo) string {
218261
if o.NameShort != "" {
219262
usgNamesAndArg += "-" + o.NameShort
220263
}
221-
222264
if o.NameLong != "" {
223265
if o.NameShort != "" {
224266
usgNamesAndArg += ", "
225267
}
226268
usgNamesAndArg += "--" + o.NameLong
227269
}
228-
229270
if an := o.optUsgArgName(); an != "" {
230271
usgNamesAndArg += " " + an
231272
}

0 commit comments

Comments
 (0)