Merge pull request #575 from MaximilianMeister/command-ordering

command: enable ordering commands by name
main
Jesse Szwedko 8 years ago committed by GitHub
commit 8ef3805c9d

@ -455,13 +455,13 @@ error.
Flags for the application and commands are shown in the order they are defined. Flags for the application and commands are shown in the order they are defined.
However, it's possible to sort them from outside this library by using `FlagsByName` However, it's possible to sort them from outside this library by using `FlagsByName`
with `sort`. or `CommandsByName` with `sort`.
For example this: For example this:
<!-- { <!-- {
"args": ["&#45;&#45;help"], "args": ["&#45;&#45;help"],
"output": "Load configuration from FILE\n.*Language for the greeting.*" "output": "add a task to the list\n.*complete a task on the list\n.*\n\n.*\n.*Load configuration from FILE\n.*Language for the greeting.*"
} --> } -->
``` go ``` go
package main package main
@ -488,7 +488,27 @@ func main() {
}, },
} }
app.Commands = []cli.Command{
{
Name: "complete",
Aliases: []string{"c"},
Usage: "complete a task on the list",
Action: func(c *cli.Context) error {
return nil
},
},
{
Name: "add",
Aliases: []string{"a"},
Usage: "add a task to the list",
Action: func(c *cli.Context) error {
return nil
},
},
}
sort.Sort(cli.FlagsByName(app.Flags)) sort.Sort(cli.FlagsByName(app.Flags))
sort.Sort(cli.CommandsByName(app.Commands))
app.Run(os.Args) app.Run(os.Args)
} }

@ -61,6 +61,20 @@ type Command struct {
commandNamePath []string commandNamePath []string
} }
type CommandsByName []Command
func (c CommandsByName) Len() int {
return len(c)
}
func (c CommandsByName) Less(i, j int) bool {
return c[i].Name < c[j].Name
}
func (c CommandsByName) Swap(i, j int) {
c[i], c[j] = c[j], c[i]
}
// FullName returns the full name of the command. // FullName returns the full name of the command.
// For subcommands this ensures that parent commands are part of the command path // For subcommands this ensures that parent commands are part of the command path
func (c Command) FullName() string { func (c Command) FullName() string {

Loading…
Cancel
Save