aboutsummaryrefslogtreecommitdiffstats
path: root/commands/commands.go
blob: 0ab84717e2306a0466e9067d13e75cf0a09b01d1 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
package commands

import (
	"fmt"
	"github.com/spf13/cobra"
)

var commandsDesc bool

func runCommands(cmd *cobra.Command, args []string) error {
	first := true

	allCmds := cmd.Root().Commands()

	for _, cmd := range allCmds {
		if !first {
			fmt.Println()
		}

		first = false

		if commandsDesc {
			fmt.Printf("# %s\n", cmd.Short)
		}

		fmt.Printf("%s %s",
			rootCommandName,
			cmd.Use,
		)

		if commandsDesc {
			fmt.Println()
		}
	}

	if !commandsDesc {
		fmt.Println()
	}

	return nil
}

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",
	)
}