Skip to content

Commit f4d982d

Browse files
committed
Use slices to find max command name len
1 parent cdb9386 commit f4d982d

1 file changed

Lines changed: 6 additions & 9 deletions

File tree

cmd/root.go

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
package cmd
22

33
import (
4+
"cmp"
45
"fmt"
6+
"slices"
57
"sort"
68
"strings"
79

@@ -61,15 +63,10 @@ func PrintHelpMessage(cmd *cobra.Command) error {
6163
}
6264

6365
func maxCommandNameLen(cmd *cobra.Command) int {
64-
maxLen := 0
65-
66-
for _, c := range cmd.Commands() {
67-
if l := len(c.Name()); l > maxLen {
68-
maxLen = l
69-
}
70-
}
71-
72-
return maxLen
66+
maxCmd := slices.MaxFunc(cmd.Commands(), func(a, b *cobra.Command) int {
67+
return cmp.Compare(len(a.Name()), len(b.Name()))
68+
})
69+
return len(maxCmd.Name())
7370
}
7471

7572
func rpad(s string, padding int) string {

0 commit comments

Comments
 (0)