Skip to content

Commit 8c77ff8

Browse files
Merge pull request #2983 from yuvallanger/sort-subcommands
Sort SUBCOMMANDS section of help output
2 parents 8405b56 + 7470ae5 commit 8c77ff8

File tree

1 file changed

+10
-3
lines changed

1 file changed

+10
-3
lines changed

commands/cli/helptext.go

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -351,18 +351,25 @@ func subcommandText(cmd *cmds.Command, rootName string, path []string) []string
351351
if len(path) > 0 {
352352
prefix += " "
353353
}
354+
355+
// Sorting fixes changing order bug #2981.
356+
sortedNames := make([]string, 0)
357+
for name := range cmd.Subcommands {
358+
sortedNames = append(sortedNames, name)
359+
}
360+
sort.Strings(sortedNames)
361+
354362
subcmds := make([]*cmds.Command, len(cmd.Subcommands))
355363
lines := make([]string, len(cmd.Subcommands))
356364

357-
i := 0
358-
for name, sub := range cmd.Subcommands {
365+
for i, name := range sortedNames {
366+
sub := cmd.Subcommands[name]
359367
usage := usageText(sub)
360368
if len(usage) > 0 {
361369
usage = " " + usage
362370
}
363371
lines[i] = prefix + name + usage
364372
subcmds[i] = sub
365-
i++
366373
}
367374

368375
lines = align(lines)

0 commit comments

Comments
 (0)