-
Notifications
You must be signed in to change notification settings - Fork 1
/
options.go
53 lines (47 loc) · 931 Bytes
/
options.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
package commander
import "strings"
// _Options
type _Options []*_Option
func (o _Options) IsEmpty() bool {
return len(o) == 0
}
func (o _Options) UsagesString(ones ...bool) (r []string) {
if len(o) == 0 {
return
}
var one bool
if len(o) == 1 && len(ones) != 0 {
one = ones[0]
}
rs := make([]string, 0, len(o))
for _, opt := range o {
if opt.line {
r = append(r, opt.UsageString(one))
} else {
rs = append(rs, opt.UsageString(one))
}
}
if len(rs) != 0 {
r = append(r, strings.Join(rs, " "))
}
return
}
func (o _Options) OptionsString() (r map[string]string) {
r = make(map[string]string, len(o))
for _, opt := range o {
if s := opt.OptionString(); len(s) != 0 {
r[opt.Name()] = s
}
}
return
}
func (o _Options) run(c Context) (result _Result) {
for _, opt := range o {
if r := opt.run(c); r != nil && r.Break() {
if result = r; r.Error() != nil {
break
}
}
}
return
}