-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
example.go
108 lines (91 loc) · 2.37 KB
/
example.go
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
package main
import (
"fmt"
"os"
"github.com/gavv/cobradoc"
"github.com/spf13/cobra"
)
const (
groupMain = "main"
groupDocs = "docs"
)
var rootCmd = &cobra.Command{
Use: "example",
Short: "Example command",
CompletionOptions: cobra.CompletionOptions{
DisableDefaultCmd: true,
},
}
var helloCmd = &cobra.Command{
GroupID: groupMain,
Use: "hello",
Short: "Say hello",
Run: func(cmd *cobra.Command, args []string) {
count, _ := cmd.Flags().GetInt("count")
for i := 0; i < count; i++ {
fmt.Println("hello!")
}
},
}
var byeCmd = &cobra.Command{
GroupID: groupMain,
Use: "bye",
Short: "Say goodbye",
Run: func(cmd *cobra.Command, args []string) {
name, _ := cmd.Flags().GetString("name")
fmt.Printf("bye, %s!\n", name)
},
}
var manCmd = &cobra.Command{
GroupID: groupDocs,
Use: "man",
Short: "Generate manual page",
Run: func(cmd *cobra.Command, args []string) {
cobradoc.WriteDocument(os.Stdout, rootCmd, cobradoc.Troff, cobradoc.Options{
LongDescription: "Example command using cobra and cobradoc.",
ExtraSections: []cobradoc.ExtraSection{
{
Title: cobradoc.BUGS,
Text: "Please report bugs at https://github.com/gavv/cobradoc",
},
},
})
},
}
var markdownCmd = &cobra.Command{
GroupID: groupDocs,
Use: "markdown",
Short: "Generate markdown page",
Run: func(cmd *cobra.Command, args []string) {
cobradoc.WriteDocument(os.Stdout, rootCmd, cobradoc.Markdown, cobradoc.Options{
LongDescription: "Example command using cobra and cobradoc.",
})
},
}
func init() {
cobra.EnableCommandSorting = false
helloCmd.Flags().IntP("count", "c", 1, "how many times to greet?")
helloCmd.Flags().SortFlags = false
helloCmd.InheritedFlags().SortFlags = false
byeCmd.PersistentFlags().StringP("name", "n", "John", "who got the goodbye?")
byeCmd.Flags().SortFlags = false
byeCmd.InheritedFlags().SortFlags = false
rootCmd.PersistentFlags().StringP("value", "v", "", "string value")
rootCmd.PersistentFlags().Bool("flag", false, "boolean flag")
rootCmd.PersistentFlags().SortFlags = false
rootCmd.AddGroup(&cobra.Group{
Title: "Main Commands",
ID: groupMain,
})
rootCmd.AddCommand(helloCmd)
rootCmd.AddCommand(byeCmd)
rootCmd.AddGroup(&cobra.Group{
Title: "Documentation Commands",
ID: groupDocs,
})
rootCmd.AddCommand(manCmd)
rootCmd.AddCommand(markdownCmd)
}
func main() {
rootCmd.Execute()
}