/
matchertest.go
65 lines (52 loc) · 1.38 KB
/
matchertest.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
package matchertest
import (
"strings"
"fmt"
"github.com/jawher/mow.cli/internal/container"
"github.com/jawher/mow.cli/internal/matcher"
"github.com/jawher/mow.cli/internal/values"
)
// NewArg creates a positional argument matcher given its name, e.g. SRC
func NewArg(name string) matcher.Matcher {
var s string
con := &container.Container{
Name: name,
Names: []string{name},
Value: values.NewString(&s, ""),
}
return matcher.NewArg(con)
}
// NewOpt creates a short and long option matcher given its (space separated) names, e.g. -f --force
func NewOpt(name string) matcher.Matcher {
s := ""
names := strings.Fields(name)
con := &container.Container{
Name: name,
Names: names,
Value: values.NewString(&s, ""),
}
index := map[string]*container.Container{}
for _, n := range names {
index[n] = con
}
return matcher.NewOpt(con, index)
}
// NewOptions create an options matcher given their names, e.g. -abc
func NewOptions(names string) matcher.Matcher {
names = strings.TrimPrefix(names, "-")
var cons []*container.Container
index := map[string]*container.Container{}
for _, r := range names {
name := fmt.Sprintf("%c", r)
var s string
fname := "-" + name
con := &container.Container{
Name: fname,
Names: []string{fname},
Value: values.NewString(&s, ""),
}
cons = append(cons, con)
index[fname] = con
}
return matcher.NewOptions(cons, index)
}