Skip to content

Commit

Permalink
Fix multi-line arg and option usage
Browse files Browse the repository at this point in the history
  • Loading branch information
rliebz committed Jan 5, 2025
1 parent aabcad7 commit f42be4d
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 5 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@ This project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html

## Unreleased

## 0.7.3 (2025-01-05)

### Fixed

- Handle multi-line usage for args and options in help text.

## 0.7.2 (2025-01-05)

### Added
Expand Down
12 changes: 9 additions & 3 deletions appcli/help.go
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ Arguments:
}

func formatArg(arg *runner.Arg, width int) string {
line := pad(arg.Name, width) + arg.Usage
line := pad(arg.Name, width) + formatUsage(arg.Usage, width)

if len(arg.ValuesAllowed) > 0 {
if arg.Usage != "" {
Expand Down Expand Up @@ -271,7 +271,7 @@ Options:
}

func formatOpt(flag cli.Flag, opt *runner.Option, width int) string {
line := pad(flagPrefix(flag, opt), width) + unquoteUsage(opt.Usage)
line := pad(flagPrefix(flag, opt), width) + formatUsage(opt.Usage, width)
defaultValue, hasDefault := opt.StaticDefault()

if hasDefault {
Expand Down Expand Up @@ -305,11 +305,17 @@ func maxOptionWidth(command *cli.Command, opts []*runner.Option) int {
func flagPrefix(flag cli.Flag, opt *runner.Option) string {
text := cli.FlagStringer(flag)
if opt.Usage != "" {
text, _, _ = strings.Cut(text, unquoteUsage(opt.Usage))
text, _, _ = strings.Cut(text, strings.TrimSpace(unquoteUsage(opt.Usage)))
}
return strings.TrimRight(text, " \t")
}

func formatUsage(usage string, width int) string {
usage = unquoteUsage(usage)
indent := strings.Repeat(" ", width+3)
return strings.TrimSpace(strings.ReplaceAll(usage, "\n", "\n"+indent))
}

// Unquotes placeholder text from the usage string.
//
// Modified from urfave/cli.
Expand Down
2 changes: 2 additions & 0 deletions main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ Description:
Arguments:
short The first argument
longer-name The second argument
which is multi-line
One of: foo, bar
no-details
values-only One of: baz, qux
Expand All @@ -103,6 +104,7 @@ Options:
--bool-default-true Boolean value (default: true)
-b, --brief A brief flag
--much-less-brief <value> A much less brief flag
which is multi-line
One of: baz, qux
--numeric <value> This is numeric (default: 0)
--only-default <value> Default: some-default
Expand Down
8 changes: 6 additions & 2 deletions testdata/help.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,9 @@ tasks:
short:
usage: The first argument
longer-name:
usage: The second argument
usage: |
The second argument
which is multi-line
values:
- foo
- bar
Expand All @@ -53,7 +55,9 @@ tasks:
placeholder:
usage: With a value named `val`
much-less-brief:
usage: A much less brief flag
usage: |
A much less brief flag
which is multi-line
values:
- baz
- qux
Expand Down

0 comments on commit f42be4d

Please sign in to comment.