Skip to content

Commit d8de683

Browse files
authored
feat: expose ValueFormatter option through HelpOptions for custom HelpPrinters (#563)
1 parent ecee830 commit d8de683

File tree

3 files changed

+20
-14
lines changed

3 files changed

+20
-14
lines changed

context.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -883,6 +883,11 @@ func (c *Context) Run(binds ...any) (err error) {
883883
func (c *Context) PrintUsage(summary bool) error {
884884
options := c.helpOptions
885885
options.Summary = summary
886+
return c.printHelp(options)
887+
}
888+
889+
func (c *Context) printHelp(options HelpOptions) error {
890+
options.ValueFormatter = c.Kong.helpFormatter
886891
return c.help(options, c)
887892
}
888893

help.go

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ func (h helpFlag) IgnoreDefault() {}
2121
func (h helpFlag) BeforeReset(ctx *Context) error {
2222
options := ctx.Kong.helpOptions
2323
options.Summary = false
24-
err := ctx.Kong.help(options, ctx)
24+
err := ctx.printHelp(options)
2525
if err != nil {
2626
return err
2727
}
@@ -58,6 +58,9 @@ type HelpOptions struct {
5858
// If this is set to a non-positive number, the terminal width is used; otherwise,
5959
// the min of this value or the terminal width is used.
6060
WrapUpperBound int
61+
62+
// ValueFormatter is used to format the help text of flags and positional arguments.
63+
ValueFormatter HelpValueFormatter
6164
}
6265

6366
// Apply options to Kong as a configuration option.
@@ -365,10 +368,9 @@ func printCommandSummary(w *helpWriter, cmd *Command) {
365368
}
366369

367370
type helpWriter struct {
368-
indent string
369-
width int
370-
lines *[]string
371-
helpFormatter HelpValueFormatter
371+
indent string
372+
width int
373+
lines *[]string
372374
HelpOptions
373375
}
374376

@@ -379,11 +381,10 @@ func newHelpWriter(ctx *Context, options HelpOptions) *helpWriter {
379381
wrapWidth = options.WrapUpperBound
380382
}
381383
w := &helpWriter{
382-
indent: "",
383-
width: wrapWidth,
384-
lines: &lines,
385-
helpFormatter: ctx.Kong.helpFormatter,
386-
HelpOptions: options,
384+
indent: "",
385+
width: wrapWidth,
386+
lines: &lines,
387+
HelpOptions: options,
387388
}
388389
return w
389390
}
@@ -398,7 +399,7 @@ func (h *helpWriter) Print(text string) {
398399

399400
// Indent returns a new helpWriter indented by two characters.
400401
func (h *helpWriter) Indent() *helpWriter {
401-
return &helpWriter{indent: h.indent + " ", lines: h.lines, width: h.width - 2, HelpOptions: h.HelpOptions, helpFormatter: h.helpFormatter}
402+
return &helpWriter{indent: h.indent + " ", lines: h.lines, width: h.width - 2, HelpOptions: h.HelpOptions}
402403
}
403404

404405
func (h *helpWriter) String() string {
@@ -426,7 +427,7 @@ func (h *helpWriter) Wrap(text string) {
426427
func writePositionals(w *helpWriter, args []*Positional) {
427428
rows := [][2]string{}
428429
for _, arg := range args {
429-
rows = append(rows, [2]string{arg.Summary(), w.helpFormatter(arg)})
430+
rows = append(rows, [2]string{arg.Summary(), w.HelpOptions.ValueFormatter(arg)})
430431
}
431432
writeTwoColumns(w, rows)
432433
}
@@ -448,7 +449,7 @@ func writeFlags(w *helpWriter, groups [][]*Flag) {
448449
}
449450
for _, flag := range group {
450451
if !flag.Hidden {
451-
rows = append(rows, [2]string{formatFlag(haveShort, flag), w.helpFormatter(flag.Value)})
452+
rows = append(rows, [2]string{formatFlag(haveShort, flag), w.HelpOptions.ValueFormatter(flag.Value)})
452453
}
453454
}
454455
}

kong.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -472,7 +472,7 @@ func (k *Kong) FatalIfErrorf(err error, args ...any) {
472472
if errors.As(err, &parseErr) {
473473
switch k.usageOnError {
474474
case fullUsage:
475-
_ = k.help(k.helpOptions, parseErr.Context)
475+
_ = parseErr.Context.printHelp(k.helpOptions)
476476
fmt.Fprintln(k.Stdout)
477477
case shortUsage:
478478
_ = k.shortHelp(k.helpOptions, parseErr.Context)

0 commit comments

Comments
 (0)