-
Notifications
You must be signed in to change notification settings - Fork 55
/
options.go
66 lines (57 loc) · 1.27 KB
/
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
54
55
56
57
58
59
60
61
62
63
64
65
66
package matcher
import (
"strings"
"github.com/jawher/mow.cli/internal/container"
)
// NewOptions create an Options matcher which can parse a group of options
func NewOptions(opts []*container.Container, index map[string]*container.Container) Matcher {
return &options{
options: opts,
index: index,
}
}
type options struct {
options []*container.Container
index map[string]*container.Container
}
func (*options) Priority() int {
return 2
}
func (om *options) Match(args []string, c *ParseContext) (bool, []string) {
ok, nargs := om.try(args, c)
if !ok {
return false, args
}
for {
ok, nnargs := om.try(nargs, c)
if !ok {
return true, nargs
}
nargs = nnargs
}
}
func (om *options) try(args []string, c *ParseContext) (bool, []string) {
if len(args) == 0 || c.RejectOptions {
return false, args
}
for _, o := range om.options {
if _, exclude := c.ExcludedOpts[o]; exclude {
continue
}
if ok, nargs := (&opt{theOne: o, index: om.index}).Match(args, c); ok {
if o.ValueSetFromEnv {
c.ExcludedOpts[o] = struct{}{}
}
return true, nargs
}
}
return false, args
}
func (om *options) String() string {
names := "-"
for _, opt := range om.options {
s := strings.TrimPrefix(opt.Names[0], "-")
names = names + s
}
return names
}