Skip to content

Conversation

@adombeck
Copy link

@adombeck adombeck commented Jan 6, 2026

The subcommands in the Command.Commands() slice are sorted alphabetically by default. That can be disabled by setting cobra.EnableCommandSorting = false in which case the order in which subcommands are added via Command.AddCommand is retained. That order is used in the "Available Commands:" section in the usage message. Let's also keep that order when listing subcommands in the "See Also" section of generated docs.

The cmd.Commands() slice is ordered for a reason, it allows developers to
choose the order in which subcommands are shown in the usage message.
Let's also keep that order when listing subcommands in the See Also
section.
@github-actions github-actions bot added the area/docs-generation Generation of docs via Cobra label Jan 6, 2026
@ccoVeille
Copy link
Contributor

I would prefer an option to do not sort them.

I feel like many people maybe surprised for things no longer being ordered

@adombeck
Copy link
Author

adombeck commented Jan 6, 2026

The commands are not sorted in the default usage function and default usage template, so why sort them by default in the generated docs? That seems unnecessarily inconsistent.

@adombeck
Copy link
Author

adombeck commented Jan 6, 2026

The commands are not sorted in the default usage function and default usage template

Actually, the subcommands returned by Command.Commands are sorted by default. So this PR only changes the behavior if cobra.EnableCommandSorting was set to false, in which case the developers clearly want to retain the order in which commands were added via Command.AddCommand.

I edited the PR description to reflect that.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area/docs-generation Generation of docs via Cobra

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants