Skip to content

Conversation

@Arfey
Copy link
Contributor

@Arfey Arfey commented Jan 9, 2026

Summary by Sourcery

Improve CLI help output formatting to correctly align command names and descriptions, including for very long command names and grouped commands.

Bug Fixes:

  • Fix misaligned indentation and padding in help output for commands, especially when command names are long or when multiple subgroups are present.

Enhancements:

  • Introduce helper utilities to compute maximum command name width, right-pad names, and detect presence of multiple subgroups to drive consistent help formatting.

Tests:

  • Add and update Bats tests and fixtures to cover help output formatting for long command names and grouped commands.

@sourcery-ai
Copy link

sourcery-ai bot commented Jan 9, 2026

Reviewer's Guide

Adjusts Cobra command help formatting to align long command names and internal commands, and adds tests covering long-name layout in both grouped and ungrouped help output.

Flow diagram for buildGroupCommandHelp with new alignment helpers

flowchart TD
  A[buildGroupCommandHelp called with cmd and group] --> B[Initialize help string and empty cmds slice]
  B --> C[Collect commands in group into cmds]
  C --> D[Compute padding using maxColumnNameLen cmd]
  D --> E[Sort cmds by name]
  E --> F[Build subGroupNameList from command annotations]
  F --> G[Append group.Title to help]
  G --> H[Determine intend string using hasSubgroup cmd]
  H --> I{For each subgroupName in subGroupNameList}
  I -->|Yes| J[If multiple subgroups append subgroup header to help]
  J --> K[For each command in cmds with matching SubGroupName]
  K --> L[Append formatted line using rpad command name and padding]
  I -->|No more subgroups| M[For each command in cmds without SubGroupName]
  M --> N[Append formatted line using rpad command name and padding plus intend]
  N --> O[Return help string]
Loading

File-Level Changes

Change Details Files
Refactor help text column alignment for grouped commands using computed padding instead of Cobra's NamePadding.
  • Introduce helper to compute maximum command name length among subcommands
  • Add right-padding helper to format command names to a fixed width
  • Replace cmd.NamePadding-based formatting with custom padding to align names and descriptions, including long names
  • Introduce subgroup-detection helper to control indentation when multiple subgroups exist
cmd/root.go
Update and extend Bats tests to validate help output alignment with long command names.
  • Tighten expected spacing for internal commands in existing grouped command test
  • Add new test suite for grouped commands with a very long command name and corresponding lets.yaml fixture
  • Add new test suite for top-level help with a very long command name and corresponding lets.yaml fixture
tests/command_group.bats
tests/command_group_long.bats
tests/help_long.bats
tests/command_group_long/lets.yaml
tests/help_long/lets.yaml

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

Copy link

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

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

Hey - I've found 1 issue, and left some high level feedback:

  • The hasSubgroup helper duplicates work already done when building subGroupNameList; you can drop the extra traversal and simply derive the indent from len(subGroupNameList) to avoid additional allocations and iteration.
  • The intend variable appears to be a misspelling of indent; renaming it will make the indentation logic easier to follow and less confusing.
  • If command names may contain non-ASCII characters, maxColumnNameLen should use utf8.RuneCountInString instead of len to keep the help columns visually aligned with multibyte characters.
Prompt for AI Agents
Please address the comments from this code review:

## Overall Comments
- The `hasSubgroup` helper duplicates work already done when building `subGroupNameList`; you can drop the extra traversal and simply derive the indent from `len(subGroupNameList)` to avoid additional allocations and iteration.
- The `intend` variable appears to be a misspelling of `indent`; renaming it will make the indentation logic easier to follow and less confusing.
- If command names may contain non-ASCII characters, `maxColumnNameLen` should use `utf8.RuneCountInString` instead of `len` to keep the help columns visually aligned with multibyte characters.

## Individual Comments

### Comment 1
<location> `cmd/root.go:104` </location>
<code_context>
 		}
 	}

+	padding := maxColumnNameLen(cmd)
+
 	sort.Slice(cmds, func(i, j int) bool {
</code_context>

<issue_to_address>
**suggestion:** Column width calculation may misalign multi-byte (non-ASCII) command names

`maxColumnNameLen` and `rpad` rely on `len(s)`, which measures bytes, not display cells. For non-ASCII command names (e.g., emoji, CJK), this will miscompute padding and misalign columns. If such names are supported, use `utf8.RuneCountInString` or a display-width–aware library instead of byte length.

Suggested implementation:

```golang
import (
	"fmt"
	"unicode/utf8"

	"github.com/spf13/cobra"
)

```

```golang
func maxColumnNameLen(cmd *cobra.Command) int {
	max := 0
	for _, c := range cmd.Commands() {
		nameLen := utf8.RuneCountInString(c.Name())
		if nameLen > max {
			max = nameLen
		}
	}
	return max
}

```

```golang
func rpad(s string, padding int) string {
	// padding is the desired rune width of the column
	runeLen := utf8.RuneCountInString(s)
	if runeLen >= padding {
		return s
	}

	// number of spaces to add to reach the target rune width
	spaceCount := padding - runeLen
	return s + fmt.Sprintf("%*s", spaceCount, "")
}

```

If `maxColumnNameLen` or `rpad` are defined with slightly different signatures or logic in your actual file, you should adapt the SEARCH blocks to match the exact existing implementations (e.g., different variable names or comments). The core changes are:
1. Replace all `len(<string>)` calls used for column-width computation with `utf8.RuneCountInString(<string>)`.
2. Ensure any padding calculations that depend on string length use rune counts instead of byte counts.
</issue_to_address>

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

cmd/root.go Outdated
}
}

padding := maxColumnNameLen(cmd)
Copy link

Choose a reason for hiding this comment

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

suggestion: Column width calculation may misalign multi-byte (non-ASCII) command names

maxColumnNameLen and rpad rely on len(s), which measures bytes, not display cells. For non-ASCII command names (e.g., emoji, CJK), this will miscompute padding and misalign columns. If such names are supported, use utf8.RuneCountInString or a display-width–aware library instead of byte length.

Suggested implementation:

import (
	"fmt"
	"unicode/utf8"

	"github.com/spf13/cobra"
)
func maxColumnNameLen(cmd *cobra.Command) int {
	max := 0
	for _, c := range cmd.Commands() {
		nameLen := utf8.RuneCountInString(c.Name())
		if nameLen > max {
			max = nameLen
		}
	}
	return max
}
func rpad(s string, padding int) string {
	// padding is the desired rune width of the column
	runeLen := utf8.RuneCountInString(s)
	if runeLen >= padding {
		return s
	}

	// number of spaces to add to reach the target rune width
	spaceCount := padding - runeLen
	return s + fmt.Sprintf("%*s", spaceCount, "")
}

If maxColumnNameLen or rpad are defined with slightly different signatures or logic in your actual file, you should adapt the SEARCH blocks to match the exact existing implementations (e.g., different variable names or comments). The core changes are:

  1. Replace all len(<string>) calls used for column-width computation with utf8.RuneCountInString(<string>).
  2. Ensure any padding calculations that depend on string length use rune counts instead of byte counts.

return fmt.Sprintf(formattedString, s)
}

func hasSubgroup(cmd *cobra.Command) bool {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Call it for each group. I didn’t want to call it before buildGroupCommandHelp and pass it as a parameter. Since we only have two groups, this is probably fine.

@kindermax kindermax merged commit cdb9386 into lets-cli:master Jan 9, 2026
5 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants