-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgroup.go
More file actions
163 lines (147 loc) · 3.44 KB
/
group.go
File metadata and controls
163 lines (147 loc) · 3.44 KB
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
package cmd
import (
"fmt"
"os"
"strings"
)
// A Group represents a group of commands. Groups can be nested arbitrarily.
//
// The Summary and Details fields are printed at the beginning and end, respectively, of the help
// message. They won’t be printed if left empty.
type Group struct {
Flags
Summary, Details string
name string
groups map[string]*Group
commands map[string]*Cmd
}
// NewGroup returns a new group of commands with the specified name.
func NewGroup(name string) *Group {
return &Group{
Flags: newFlags(),
name: name,
groups: make(map[string]*Group),
commands: make(map[string]*Cmd),
}
}
// Command adds a command.
func (g *Group) Command(name string, f func()) *Cmd {
command := New(fmt.Sprintf("%s %s", g.name, name), f)
g.commands[name] = command
return command
}
// Group adds a sub-group.
func (g *Group) Group(name string) *Group {
group := NewGroup(fmt.Sprintf("%s %s", g.name, name))
g.groups[name] = group
return group
}
func (g *Group) errorAndExit(msg string) {
w := os.Stderr
fmt.Fprintf(w, "%s: %s\n", g.name, msg)
fmt.Fprintf(w, "Try '%s help' for more information.\n", g.name)
os.Exit(2)
}
func (g *Group) helpAndExit() {
fmt.Fprintf(os.Stdout, g.Help())
os.Exit(0)
}
// Help returns a help message.
func (g *Group) Help() string {
defs := []*definitionList{
{
title: "Options",
definitions: g.Flags.defs,
},
{
title: "Groups",
definitions: g.groupDefinitions(),
},
{
title: "Commands",
definitions: g.commandDefinitions(),
},
}
return formatHelp(g.usage(), g.Summary, g.Details, defs)
}
func (g *Group) summary() string {
return g.Summary
}
func (g *Group) details() string {
return g.Details
}
func (g *Group) groupDefinitions() []*definition {
defs := []*definition{}
for name, g := range g.groups {
defs = append(defs, &definition{
terms: []string{name},
text: g.Summary,
})
}
return defs
}
func (g *Group) commandDefinitions() []*definition {
defs := []*definition{}
for name, c := range g.commands {
defs = append(defs, &definition{
terms: []string{name},
text: c.Summary,
})
}
return defs
}
func (g *Group) usage() string {
line := []string{"Usage:", g.name}
if s := g.Flags.usage(); s != "" {
line = append(line, s)
}
groupOrCommand := []string{}
if len(g.groups) > 0 {
groupOrCommand = append(groupOrCommand, "GROUP")
}
if len(g.commands) > 0 {
groupOrCommand = append(groupOrCommand, "COMMAND")
}
line = append(line, strings.Join(groupOrCommand, " | "))
return strings.Join(line, " ")
}
// Run parses the given command-line arguments, sets values for given flags and calls the function
// for the selected command. It’s usually called with os.Args[1:].
func (g *Group) Run(args []string) {
g.run(args, false)
}
func (g *Group) run(args []string, helpMode bool) {
// call Flags.parse
help, args, err := g.Flags.parse(args)
if err != nil {
g.errorAndExit(err.Error())
}
if help {
g.helpAndExit()
}
// select group or command
if len(args) == 0 {
if helpMode {
g.helpAndExit()
}
g.errorAndExit("command expected")
}
a, args := args[0], args[1:]
if a == "help" {
g.run(args, true)
return
}
if group, ok := g.groups[a]; ok {
group.run(args, helpMode)
return
}
if command, ok := g.commands[a]; ok {
if helpMode {
command.helpAndExit()
} else {
command.Run(args)
}
return
}
g.errorAndExit(fmt.Sprintf("'%s' is not a %s command", a, g.name))
}