forked from urfave/cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
flag_mutex.go
43 lines (38 loc) · 873 Bytes
/
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
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
}
func (grp MutuallyExclusiveFlags) check(ctx *Context) error {
oneSet := false
e := &mutuallyExclusiveGroup{}
for _, grpf := range grp.Flags {
for _, f := range grpf {
for _, name := range f.Names() {
if ctx.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
}