@@ -12,13 +12,13 @@ package commander
12
12
import (
13
13
"bytes"
14
14
"fmt"
15
+ "github.com/gonuts/flag"
15
16
"io"
16
17
"os"
17
18
"os/exec"
19
+ "sort"
18
20
"strings"
19
21
"text/template"
20
-
21
- "github.com/gonuts/flag"
22
22
)
23
23
24
24
// A Commander holds the configuration for the command line tool.
@@ -38,6 +38,45 @@ type Commander struct {
38
38
Commanders []* Commander
39
39
}
40
40
41
+ // Type to allow us to use sort.Sort on a slice of Commanders
42
+ type CommanderSlice []* Commander
43
+
44
+ func (c CommanderSlice ) Len () int {
45
+ return len (c )
46
+ }
47
+
48
+ func (c CommanderSlice ) Less (i , j int ) bool {
49
+ return c [i ].Name < c [j ].Name
50
+ }
51
+
52
+ func (c CommanderSlice ) Swap (i , j int ) {
53
+ c [i ], c [j ] = c [j ], c [i ]
54
+ }
55
+
56
+ type CommandSlice []* Command
57
+
58
+ func (c CommandSlice ) Len () int {
59
+ return len (c )
60
+ }
61
+
62
+ func (c CommandSlice ) Less (i , j int ) bool {
63
+ return c [i ].Name () < c [j ].Name ()
64
+ }
65
+
66
+ func (c CommandSlice ) Swap (i , j int ) {
67
+ c [i ], c [j ] = c [j ], c [i ]
68
+ }
69
+
70
+ // Sort the subcommanders.
71
+ func (c * Commander ) SortCommanders () {
72
+ sort .Sort (CommanderSlice (c .Commanders ))
73
+ }
74
+
75
+ // Sort the commanders
76
+ func (c * Commander ) SortCommands () {
77
+ sort .Sort (CommandSlice (c .Commands ))
78
+ }
79
+
41
80
// Run executes the commander using the provided arguments. The command
42
81
// matching the first argument is executed and it receives the remaining
43
82
// arguments.
@@ -116,7 +155,9 @@ func (c *Commander) Run(args []string) error {
116
155
}
117
156
118
157
func (c * Commander ) usage () error {
119
- err := tmpl (os .Stderr ,strings .Replace (usageTemplate ,"%%MAX%%" ,fmt .Sprintf ("%d" ,c .MaxLen ()),- 1 ),c )
158
+ c .SortCommanders ()
159
+ c .SortCommands ()
160
+ err := tmpl (os .Stderr , strings .Replace (usageTemplate , "%%MAX%%" , fmt .Sprintf ("%d" , c .MaxLen ()), - 1 ), c )
120
161
if err != nil {
121
162
fmt .Println (err )
122
163
}
@@ -149,7 +190,7 @@ func (c *Commander) help(args []string) error {
149
190
c := struct {
150
191
* Command
151
192
ProgramName string
152
- }{cmd , c .Name }
193
+ }{cmd , c .FullSpacedName () }
153
194
return tmpl (os .Stdout , helpTemplate , c )
154
195
}
155
196
}
@@ -159,13 +200,13 @@ func (c *Commander) help(args []string) error {
159
200
160
201
func (c * Commander ) MaxLen () (res int ) {
161
202
res = 0
162
- for _ ,cmd := range c .Commands {
203
+ for _ , cmd := range c .Commands {
163
204
i := len (cmd .Name ())
164
205
if i > res {
165
206
res = i
166
207
}
167
208
}
168
- for _ ,cmd := range c .Commanders {
209
+ for _ , cmd := range c .Commanders {
169
210
i := len (cmd .Name )
170
211
if i > res {
171
212
res = i
@@ -183,6 +224,15 @@ func (c *Commander) FullName() string {
183
224
return n
184
225
}
185
226
227
+ //FullSpacedName returns the full name of the commander, with subcommand names seperated by spaces.
228
+ func (c * Commander ) FullSpacedName () string {
229
+ n := c .Name
230
+ if c .Parent != nil {
231
+ n = c .Parent .FullSpacedName () + " " + n
232
+ }
233
+ return n
234
+ }
235
+
186
236
// A Command is an implementation of a subcommand.
187
237
type Command struct {
188
238
// Run runs the command.
@@ -242,23 +292,21 @@ func (c *Command) Runnable() bool {
242
292
return c .Run != nil
243
293
}
244
294
245
- var usageTemplate = `Usage:
246
- {{.Name}} command [arguments]
295
+ var usageTemplate = `Usage: {{.FullSpacedName}} command [arguments]
247
296
248
- The commands are:
249
- {{range .Commands}}{{if .Runnable}}
250
- {{.Name | printf "%-%%MAX%%s"}} {{.Short}}{{end}}{{end}}
251
- {{range $cmd := .Commanders}}
252
- {{.Name | printf "%-%%MAX%%s"}} {{.Short}}{{end}}
297
+ Commands:
298
+ {{range .Commands}}{{if .Runnable}} {{.Name | printf "%-%%MAX%%s"}} {{.Short}}{{end}}
299
+ {{end}}
253
300
254
- Use "{{$.Name}} help [command]" for more information about a command.
301
+ Subcommands:
302
+ {{range .Commanders}} {{.Name | printf "%-%%MAX%%s"}} {{.Short}}
303
+ {{end}}
304
+ Use "{{.Name}} help [command]" for more information about a command.
255
305
256
306
Additional help topics:
257
- {{range $cmd := .Commands}}{{if not .Runnable}}
307
+ {{range .Commands}}{{if not .Runnable}}
258
308
{{.Name | printf "%-%%MAX%%s"}} {{.Short}}{{end}}{{end}}
259
-
260
309
Use "{{.Name}} help [topic]" for more information about that topic.
261
-
262
310
`
263
311
264
312
var helpTemplate = `{{if .Runnable}}Usage: {{.ProgramName}} {{.UsageLine}}
0 commit comments