diff options
author | Michael Muré <batolettre@gmail.com> | 2018-07-19 12:30:25 +0200 |
---|---|---|
committer | Michael Muré <batolettre@gmail.com> | 2018-07-19 12:36:01 +0200 |
commit | 7f5922f905831a85ffee4c9226b65715899ba758 (patch) | |
tree | d18a29a2ec13706efa7f5e9efc9515a92cf4513d /commands/commands.go | |
parent | 459bb8747d9e84493b8d04a3172e6758452a75b9 (diff) | |
download | git-bug-7f5922f905831a85ffee4c9226b65715899ba758.tar.gz |
rework all the commands to use cobra as a parser
Diffstat (limited to 'commands/commands.go')
-rw-r--r-- | commands/commands.go | 60 |
1 files changed, 30 insertions, 30 deletions
diff --git a/commands/commands.go b/commands/commands.go index 1e8e9e19..f06d1ce8 100644 --- a/commands/commands.go +++ b/commands/commands.go @@ -1,55 +1,55 @@ package commands import ( - "flag" "fmt" - "github.com/MichaelMure/git-bug/repository" - "sort" + "github.com/spf13/cobra" ) -var commandsFlagSet = flag.NewFlagSet("commands", flag.ExitOnError) - -var ( - commandsDesc = commandsFlagSet.Bool("pretty", false, "Output the command description as well as Markdown compatible comment") -) - -func runCommands(repo repository.Repo, args []string) error { - commandsFlagSet.Parse(args) - args = commandsFlagSet.Args() +var commandsDesc bool +func runCommands(cmd *cobra.Command, args []string) error { first := true - keys := make([]string, 0, len(CommandMap)) + allCmds := cmd.Root().Commands() - for key := range CommandMap { - keys = append(keys, key) - } - - sort.Strings(keys) - - for _, key := range keys { + for _, cmd := range allCmds { if !first { fmt.Println() } first = false - cmd := CommandMap[key] + if commandsDesc { + fmt.Printf("# %s\n", cmd.Short) + } - if *commandsDesc { - fmt.Printf("# %s\n", cmd.Description) + fmt.Printf("%s %s", + rootCommandName, + cmd.Use, + ) + + if commandsDesc { + fmt.Println() } + } - // TODO: the root name command ("git bug") should be passed from git-bug.go but well ... - fmt.Printf("%s %s %s\n", "git bug", key, cmd.Usage) + if !commandsDesc { + fmt.Println() } return nil } -var commandsCmd = &Command{ - Description: "Display available commands", - Usage: "[<option>...]", - flagSet: commandsFlagSet, - RunMethod: runCommands, +var commandsCmd = &cobra.Command{ + Use: "commands [<option>...]", + Short: "Display available commands", + RunE: runCommands, +} + +func init() { + rootCmd.AddCommand(commandsCmd) + + commandsCmd.Flags().BoolVarP(&commandsDesc, "pretty", "p", false, + "Output the command description as well as Markdown compatible comment", + ) } |