-
Notifications
You must be signed in to change notification settings - Fork 4
/
regexp.go
81 lines (70 loc) · 1.49 KB
/
regexp.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
package flagvar
import (
"regexp"
"strings"
)
// Regexp is a `flag.Value` for regular expression arguments.
// If `POSIX` is set to true, `regexp.CompilePOSIX` is used instead of `regexp.Compile`.
type Regexp struct {
POSIX bool
Value *regexp.Regexp
Text string
}
// Help returns a string suitable for inclusion in a flag help message.
func (fv *Regexp) Help() string {
if fv.POSIX {
return "a POSIX regular expression"
}
return "a regular expression"
}
// Set is flag.Value.Set
func (fv *Regexp) Set(v string) error {
var err error
var re *regexp.Regexp
if fv.POSIX {
re, err = regexp.CompilePOSIX(v)
} else {
re, err = regexp.Compile(v)
}
if err != nil {
return err
}
fv.Text = v
fv.Value = re
return nil
}
func (fv *Regexp) String() string {
return fv.Text
}
// Regexps is a `flag.Value` for regular expression arguments.
type Regexps struct {
POSIX bool
Values []*regexp.Regexp
Texts []string
}
// Help returns a string suitable for inclusion in a flag help message.
func (fv *Regexps) Help() string {
if fv.POSIX {
return "a POSIX regular expression"
}
return "a regular expression"
}
// Set is flag.Value.Set
func (fv *Regexps) Set(v string) error {
var err error
var re *regexp.Regexp
if fv.POSIX {
re, err = regexp.CompilePOSIX(v)
} else {
re, err = regexp.Compile(v)
}
if err != nil {
return err
}
fv.Texts = append(fv.Texts, v)
fv.Values = append(fv.Values, re)
return nil
}
func (fv *Regexps) String() string {
return strings.Join(fv.Texts, ",")
}