-
Notifications
You must be signed in to change notification settings - Fork 2
/
flags.go
54 lines (40 loc) · 1.18 KB
/
flags.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
package tracegen
import (
"os"
"regexp"
"github.com/pkg/errors"
"github.com/spf13/pflag"
)
type Settings struct {
Exclude []string
Tagged bool
Exported bool
Methods bool
excludePatterns []*regexp.Regexp
}
func (s *Settings) Parse() (err error) {
for _, pattern := range s.Exclude {
re, err := regexp.Compile(pattern)
if err != nil {
return errors.Wrapf(err, "invalid exclude pattern: %q", pattern)
}
s.excludePatterns = append(s.excludePatterns, re)
}
return nil
}
func DefaultSettings() (s Settings) {
s.Exclude = []string{`/cmd(/|$)`}
return s
}
func DefaultFlags(s *Settings) (flags *pflag.FlagSet) {
if s == nil {
settings := DefaultSettings()
s = &settings
}
flags = pflag.NewFlagSet(os.Args[0], pflag.ExitOnError)
flags.StringSliceVar(&s.Exclude, "exclude", s.Exclude, "if specified, do not run on matching packages")
flags.BoolVar(&s.Tagged, "tagged", s.Tagged, "if specified, only run on tagged types, functions, and methods")
flags.BoolVar(&s.Exported, "exported", s.Exported, "if specified, only run on exported types, functions, and methods")
flags.BoolVar(&s.Methods, "methods", s.Methods, "if specified, only run on methods")
return flags
}