-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
flag_mutex.go
56 lines (49 loc) · 1.12 KB
/
flag_mutex.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
package cli
// MutuallyExclusiveFlags defines a mutually exclusive flag group
// Multiple option paths can be provided out of which
// only one can be defined on cmdline
// So for example
// [ --foo | [ --bar something --darth somethingelse ] ]
type MutuallyExclusiveFlags struct {
// Flag list
Flags [][]Flag
// whether this group is required
Required bool
// Category to apply to all flags within group
Category string
}
func (grp MutuallyExclusiveFlags) check(cmd *Command) error {
oneSet := false
e := &mutuallyExclusiveGroup{}
for _, grpf := range grp.Flags {
for _, f := range grpf {
for _, name := range f.Names() {
if cmd.IsSet(name) {
if oneSet {
e.flag2Name = name
return e
}
e.flag1Name = name
oneSet = true
break
}
}
if oneSet {
break
}
}
}
if !oneSet && grp.Required {
return &mutuallyExclusiveGroupRequiredFlag{flags: &grp}
}
return nil
}
func (grp MutuallyExclusiveFlags) propagateCategory() {
for _, grpf := range grp.Flags {
for _, f := range grpf {
if cf, ok := f.(CategorizableFlag); ok {
cf.SetCategory(grp.Category)
}
}
}
}