Debian Codegraph @

flag_mutex.go

raw symbols SCIP
1 package cli
2
3 // MutuallyExclusiveFlags defines a mutually exclusive flag group
4 // Multiple option paths can be provided out of which
5 // only one can be defined on cmdline
6 // So for example
7 // [ --foo | [ --bar something --darth somethingelse ] ]
8 type MutuallyExclusiveFlags struct {
9 // Flag list
10 Flags [][]Flag
11
12 // whether this group is required
13 Required bool
14
15 // Category to apply to all flags within group
16 Category string
17 }
18
19 func (grp MutuallyExclusiveFlags) check(_ *Command) error {
20 oneSet := false
21 e := &mutuallyExclusiveGroup{}
22
23 for _, grpf := range grp.Flags {
24 for _, f := range grpf {
25 if f.IsSet() {
26 if oneSet {
27 e.flag2Name = f.Names()[0]
28 return e
29 }
30 e.flag1Name = f.Names()[0]
31 oneSet = true
32 break
33 }
34 if oneSet {
35 break
36 }
37 }
38 }
39
40 if !oneSet && grp.Required {
41 return &mutuallyExclusiveGroupRequiredFlag{flags: &grp}
42 }
43 return nil
44 }
45
46 func (grp MutuallyExclusiveFlags) propagateCategory() {
47 for _, grpf := range grp.Flags {
48 for _, f := range grpf {
49 if cf, ok := f.(CategorizableFlag); ok {
50 cf.SetCategory(grp.Category)
51 }
52 }
53 }
54 }