Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions context.go
Original file line number Diff line number Diff line change
Expand Up @@ -883,6 +883,11 @@ func (c *Context) Run(binds ...any) (err error) {
func (c *Context) PrintUsage(summary bool) error {
options := c.helpOptions
options.Summary = summary
return c.printHelp(options)
}

func (c *Context) printHelp(options HelpOptions) error {
options.ValueFormatter = c.Kong.helpFormatter
Copy link
Contributor Author

@mpeyper mpeyper Nov 4, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This could be collapsed into PrintUsages if we wanted to, but it would make the usage in kong.go look a bit strange:

_ = parseErr.Context.printUsages(parseErr.Context.helpOptions.Summary)

return c.help(options, c)
}

Expand Down
27 changes: 14 additions & 13 deletions help.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ func (h helpFlag) IgnoreDefault() {}
func (h helpFlag) BeforeReset(ctx *Context) error {
options := ctx.Kong.helpOptions
options.Summary = false
err := ctx.Kong.help(options, ctx)
err := ctx.printHelp(options)
if err != nil {
return err
}
Expand Down Expand Up @@ -58,6 +58,9 @@ type HelpOptions struct {
// If this is set to a non-positive number, the terminal width is used; otherwise,
// the min of this value or the terminal width is used.
WrapUpperBound int

// ValueFormatter is used to format the help text of flags and positional arguments.
ValueFormatter HelpValueFormatter
}

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

type helpWriter struct {
indent string
width int
lines *[]string
helpFormatter HelpValueFormatter
indent string
width int
lines *[]string
HelpOptions
}

Expand All @@ -379,11 +381,10 @@ func newHelpWriter(ctx *Context, options HelpOptions) *helpWriter {
wrapWidth = options.WrapUpperBound
}
w := &helpWriter{
indent: "",
width: wrapWidth,
lines: &lines,
helpFormatter: ctx.Kong.helpFormatter,
HelpOptions: options,
indent: "",
width: wrapWidth,
lines: &lines,
HelpOptions: options,
}
return w
}
Expand All @@ -398,7 +399,7 @@ func (h *helpWriter) Print(text string) {

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

func (h *helpWriter) String() string {
Expand Down Expand Up @@ -426,7 +427,7 @@ func (h *helpWriter) Wrap(text string) {
func writePositionals(w *helpWriter, args []*Positional) {
rows := [][2]string{}
for _, arg := range args {
rows = append(rows, [2]string{arg.Summary(), w.helpFormatter(arg)})
rows = append(rows, [2]string{arg.Summary(), w.HelpOptions.ValueFormatter(arg)})
}
writeTwoColumns(w, rows)
}
Expand All @@ -448,7 +449,7 @@ func writeFlags(w *helpWriter, groups [][]*Flag) {
}
for _, flag := range group {
if !flag.Hidden {
rows = append(rows, [2]string{formatFlag(haveShort, flag), w.helpFormatter(flag.Value)})
rows = append(rows, [2]string{formatFlag(haveShort, flag), w.HelpOptions.ValueFormatter(flag.Value)})
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion kong.go
Original file line number Diff line number Diff line change
Expand Up @@ -472,7 +472,7 @@ func (k *Kong) FatalIfErrorf(err error, args ...any) {
if errors.As(err, &parseErr) {
switch k.usageOnError {
case fullUsage:
_ = k.help(k.helpOptions, parseErr.Context)
_ = parseErr.Context.printHelp(k.helpOptions)
fmt.Fprintln(k.Stdout)
case shortUsage:
_ = k.shortHelp(k.helpOptions, parseErr.Context)
Expand Down