-
Notifications
You must be signed in to change notification settings - Fork 11
slices for max cmd name #287
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Reviewer's guide (collapsed on small PRs)Reviewer's GuideRefactors the computation of the maximum command-name length to use Go's slices.MaxFunc utility and documents the fix in the changelog. Flow diagram for updated maxCommandNameLen logicflowchart TD
A[Start maxCommandNameLen] --> B[Get commands list from cmd.Commands]
B --> C[Call slices.MaxFunc on commands]
C --> D["Comparator: cmp.Compare(len(a.Name), len(b.Name))"]
D --> E[slices.MaxFunc returns command with longest name]
E --> F[Compute len of maxCmd.Name]
F --> G[Return length]
G --> H[End]
File-Level Changes
Tips and commandsInteracting with Sourcery
Customizing Your ExperienceAccess your dashboard to:
Getting Help
|
There was a problem hiding this 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 new maxCommandNameLen implementation will panic if cmd.Commands() returns an empty slice, whereas the previous version safely returned 0; consider handling the empty-slice case before calling slices.MaxFunc.
- To reduce indirection and avoid relying on cmp.Compare for simple length comparison, you could replace the comparator with direct integer comparison (e.g., return len(a.Name()) - len(b.Name())) for slightly clearer code.
Prompt for AI Agents
Please address the comments from this code review:
## Overall Comments
- The new maxCommandNameLen implementation will panic if cmd.Commands() returns an empty slice, whereas the previous version safely returned 0; consider handling the empty-slice case before calling slices.MaxFunc.
- To reduce indirection and avoid relying on cmp.Compare for simple length comparison, you could replace the comparator with direct integer comparison (e.g., return len(a.Name()) - len(b.Name())) for slightly clearer code.
## Individual Comments
### Comment 1
<location> `cmd/root.go:66-69` </location>
<code_context>
- }
-
- return maxLen
+ maxCmd := slices.MaxFunc(cmd.Commands(), func(a, b *cobra.Command) int {
+ return cmp.Compare(len(a.Name()), len(b.Name()))
+ })
+ return len(maxCmd.Name())
}
</code_context>
<issue_to_address>
**issue (bug_risk):** Handle the case where `cmd.Commands()` is empty to avoid `slices.MaxFunc` panicking.
Previously this code returned 0 when there were no subcommands; now `slices.MaxFunc` will panic on an empty slice. If `cmd.Commands()` can ever be empty (e.g., for leaf commands), this introduces a runtime crash instead of the prior safe default. Please guard for `len(cmd.Commands()) == 0` and return 0, or otherwise ensure empty slices are handled safely before calling `slices.MaxFunc`.
</issue_to_address>Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.
be29731 to
98556bf
Compare
98556bf to
25ad348
Compare
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Summary by Sourcery
Refine command help formatting by correctly calculating the longest command name and document the fix in the changelog.
Bug Fixes:
Documentation: