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