commands.go

 1package commands
 2
 3import (
 4	"fmt"
 5	"github.com/spf13/cobra"
 6)
 7
 8var commandsDesc bool
 9
10func runCommands(cmd *cobra.Command, args []string) error {
11	first := true
12
13	allCmds := cmd.Root().Commands()
14
15	for _, cmd := range allCmds {
16		if !first {
17			fmt.Println()
18		}
19
20		first = false
21
22		if commandsDesc {
23			fmt.Printf("# %s\n", cmd.Short)
24		}
25
26		fmt.Printf("%s %s",
27			rootCommandName,
28			cmd.Use,
29		)
30
31		if commandsDesc {
32			fmt.Println()
33		}
34	}
35
36	if !commandsDesc {
37		fmt.Println()
38	}
39
40	return nil
41}
42
43var commandsCmd = &cobra.Command{
44	Use:   "commands [<option>...]",
45	Short: "Display available commands",
46	RunE:  runCommands,
47}
48
49func init() {
50	rootCmd.AddCommand(commandsCmd)
51
52	commandsCmd.Flags().BoolVarP(&commandsDesc, "pretty", "p", false,
53		"Output the command description as well as Markdown compatible comment",
54	)
55}