@@ -3,8 +3,10 @@ package cmd
33import (
44 "fmt"
55 "strings"
6+ "sort"
67
78 "github.com/spf13/cobra"
9+ "github.com/lets-cli/lets/set"
810)
911
1012// newRootCmd represents the base command when called without any subcommands.
@@ -58,42 +60,58 @@ func PrintHelpMessage(cmd *cobra.Command) error {
5860 return err
5961}
6062
61- func buildGroupedCommandHelp (cmd * cobra.Command ) string {
62- help := ""
63- cmds := cmd .Commands ()
64- groups := cmd .Groups ()
65-
66- groupCmdMap := make (map [string ]map [string ][]* cobra.Command )
67-
68- // todo: add sort
69-
70- for _ , group := range groups {
71- if _ , ok := groupCmdMap [group .Title ]; ! ok {
72- groupCmdMap [group .Title ] = make (map [string ][]* cobra.Command )
73- }
74- for _ , c := range cmds {
75- if c .GroupID == group .ID && (c .IsAvailableCommand () || c .Name () == "help" ) {
76- subgroup := c .Annotations ["SubGroupName" ]
77- groupCmdMap [group.Title ][subgroup ] = append (groupCmdMap [group.Title ][subgroup ], c )
78- }
79- }
80- }
81-
82- for groupName , GroupsMap := range groupCmdMap {
83- help += fmt .Sprintf ("%s\n " , groupName )
84- for subgroupName , cmds := range GroupsMap {
85- intend := ""
86- if len (GroupsMap ) > 1 {
87- help += fmt .Sprintf ("\n %-*s\n " , cmd .NamePadding (), subgroupName )
88- intend = " "
89- }
90- for _ , c := range cmds {
63+ func buildGroupCommandHelp (cmd * cobra.Command , group * cobra.Group ) string {
64+ help := ""
65+ cmds := []* cobra.Command {}
66+
67+ // select commands that belong to the specified group
68+ for _ , c := range cmd .Commands () {
69+ if c .GroupID == group .ID && (c .IsAvailableCommand () || c .Name () == "help" ) {
70+ cmds = append (cmds , c )
71+ }
72+ }
73+
74+ sort .Slice (cmds , func (i , j int ) bool {
75+ return cmds [i ].Name () < cmds [j ].Name ()
76+ })
77+
78+ // Create a list of subgroups
79+ subGroupNameSet := set .NewSet [string ]()
80+
81+ for _ , c := range cmds {
82+ if subgroup , ok := c .Annotations ["SubGroupName" ]; ok && subgroup != "" {
83+ subGroupNameSet .Add (subgroup )
84+ }
85+ }
86+
87+ subGroupNameList := subGroupNameSet .ToList ()
88+ sort .Strings (subGroupNameList )
89+
90+ // generate output
91+ help += fmt .Sprintf ("%s\n " , group .Title )
92+
93+ for _ , subgroupName := range subGroupNameList {
94+ intend := ""
95+ if len (subGroupNameList ) > 1 {
96+ help += fmt .Sprintf ("\n %s\n " , subgroupName )
97+ intend = " "
98+ }
99+ for _ , c := range cmds {
100+ if subgroup , ok := c .Annotations ["SubGroupName" ]; ok && subgroup == subgroupName {
91101 help += fmt .Sprintf ("%s %-*s %s\n " , intend , cmd .NamePadding (), c .Name (), c .Short )
92- }
93- }
94- help += "\n "
95- }
96- return help
102+ }
103+ }
104+ }
105+
106+ for _ , c := range cmds {
107+ if _ , ok := c .Annotations ["SubGroupName" ]; ! ok {
108+ help += fmt .Sprintf (" %-*s %s\n " , cmd .NamePadding (), c .Name (), c .Short )
109+ }
110+ }
111+
112+ help += "\n "
113+
114+ return help
97115}
98116
99117
@@ -112,7 +130,9 @@ func PrintRootHelpMessage(cmd *cobra.Command) error {
112130 help += "\n "
113131
114132 // Commands
115- help += buildGroupedCommandHelp (cmd )
133+ for _ , group := range cmd .Groups () {
134+ help += buildGroupCommandHelp (cmd , group )
135+ }
116136
117137 // Flags
118138 if cmd .HasAvailableLocalFlags () {
0 commit comments