forked from monochromegane/the_platinum_searcher
-
Notifications
You must be signed in to change notification settings - Fork 0
/
option.go
152 lines (127 loc) · 6.89 KB
/
option.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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
package the_platinum_searcher
import "github.com/jessevdk/go-flags"
// Top level options
type Option struct {
Version bool `long:"version" description:"Show version"`
OutputOption *OutputOption `group:"Output Options"`
SearchOption *SearchOption `group:"Search Options"`
}
// Output options.
type OutputOption struct {
Color func() `long:"color" description:"Print color codes in results (default: true)"`
NoColor func() `long:"nocolor" description:"Don't print color codes in results (default: false)"`
ForceColor bool // Force color. Not user option.
EnableColor bool // Enable color. Not user option.
ColorLineNumber func(string) `long:"color-line-number" description:"Color codes for line numbers (default: 1;33)"`
ColorPath func(string) `long:"color-path" description:"Color codes for path names (default: 1;32)"`
ColorMatch func(string) `long:"color-match" description:"Color codes for result matches (default: 30;43)"`
ColorCodeLineNumber string // Color line numbers. Not user option.
ColorCodePath string // Color path names. Not user option.
ColorCodeMatch string // Color result matches. Not user option.
Group func() `long:"group" description:"Print file name at header (default: true)"`
NoGroup func() `long:"nogroup" description:"Don't print file name at header (default: false)"`
ForceGroup bool // Force group. Not user option.
EnableGroup bool // Enable group. Not user option.
Null bool `short:"0" long:"null" description:"Separate filenames with null (for 'xargs -0') (default: false)"`
Column bool `long:"column" description:"Print column (default: false)"`
LineNumber func() `long:"numbers" description:"Print Line number. (default: true)"`
NoLineNumber func() `short:"N" long:"nonumbers" description:"Omit Line number. (default: false)"`
ForceLineNumber bool // Force line number. Not user option.
EnableLineNumber bool // Enable line number. Not user option.
After int `short:"A" long:"after" description:"Print lines after match"`
Before int `short:"B" long:"before" description:"Print lines before match"`
Context int `short:"C" long:"context" description:"Print lines before and after match"`
FilesWithMatches bool `short:"l" long:"files-with-matches" description:"Only print filenames that contain matches"`
Count bool `short:"c" long:"count" description:"Only print the number of matching lines for each input file."`
OutputEncode string `short:"o" long:"output-encode" description:"Specify output encoding (none, jis, sjis, euc)"`
}
func newOutputOption() *OutputOption {
opt := &OutputOption{}
opt.Color = opt.SetEnableColor
opt.NoColor = opt.SetDisableColor
opt.EnableColor = true
opt.Group = opt.SetEnableGroup
opt.NoGroup = opt.SetDisableGroup
opt.EnableGroup = true
opt.LineNumber = opt.SetEnableLineNumber
opt.NoLineNumber = opt.SetDisableLineNumber
opt.EnableLineNumber = true
opt.ColorLineNumber = opt.SetColorLineNumber
opt.ColorPath = opt.SetColorPath
opt.ColorMatch = opt.SetColorMatch
opt.ColorCodeLineNumber = "1;33" // yellow with black background
opt.ColorCodePath = "1;32" // bold green
opt.ColorCodeMatch = "30;43" // black with yellow background
return opt
}
func (o *OutputOption) SetEnableColor() {
o.ForceColor = true
o.EnableColor = true
}
func (o *OutputOption) SetDisableColor() {
o.EnableColor = false
}
func (o *OutputOption) SetEnableLineNumber() {
o.ForceLineNumber = true
o.EnableLineNumber = true
}
func (o *OutputOption) SetDisableLineNumber() {
o.EnableLineNumber = false
}
func (o *OutputOption) SetEnableGroup() {
o.ForceGroup = true
o.EnableGroup = true
}
func (o *OutputOption) SetDisableGroup() {
o.EnableGroup = false
}
func (o *OutputOption) SetColorLineNumber(code string) {
o.ColorCodeLineNumber = code
}
func (o *OutputOption) SetColorPath(code string) {
o.ColorCodePath = code
}
func (o *OutputOption) SetColorMatch(code string) {
o.ColorCodeMatch = code
}
// Search options.
type SearchOption struct {
Regexp bool `short:"e" description:"Parse PATTERN as a regular expression (default: false). Accepted syntax is the same as https://github.com/google/re2/wiki/Syntax except from \\C"`
IgnoreCase bool `short:"i" long:"ignore-case" description:"Match case insensitively"`
SmartCase bool `short:"S" long:"smart-case" description:"Match case insensitively unless PATTERN contains uppercase characters"`
WordRegexp bool `short:"w" long:"word-regexp" description:"Only match whole words"`
Ignore []string `long:"ignore" description:"Ignore files/directories matching pattern"`
VcsIgnore []string `long:"vcs-ignore" description:"VCS ignore files" default:".gitignore"`
GlobalGitIgnore bool `long:"global-gitignore" description:"Use git's global gitignore file for ignore patterns"`
HomePtIgnore bool `long:"home-ptignore" description:"Use $Home/.ptignore file for ignore patterns"`
SkipVcsIgnore bool `short:"U" long:"skip-vcs-ignores" description:"Don't use VCS ignore file for ignore patterns"`
FilesWithRegexp func(string) `short:"g" description:"Print filenames matching PATTERN"`
EnableFilesWithRegexp bool // Enable files with regexp. Not user option.
PatternFilesWithRegexp string // Pattern files with regexp. Not user option.
FileSearchRegexp string `short:"G" long:"file-search-regexp" description:"PATTERN Limit search to filenames matching PATTERN"`
Depth int `long:"depth" default:"25" description:"Search up to NUM directories deep"`
Follow bool `short:"f" long:"follow" description:"Follow symlinks"`
Hidden bool `long:"hidden" description:"Search hidden files and directories"`
SearchStream bool // Input from pipe. Not user option.
}
func (o *SearchOption) SetFilesWithRegexp(p string) {
o.EnableFilesWithRegexp = true
o.PatternFilesWithRegexp = p
}
func newSearchOption() *SearchOption {
opt := &SearchOption{}
opt.FilesWithRegexp = opt.SetFilesWithRegexp
return opt
}
func newOptionParser(opts *Option) *flags.Parser {
output := flags.NewNamedParser("pt", flags.Default)
output.AddGroup("Output Options", "", &OutputOption{})
search := flags.NewNamedParser("pt", flags.Default)
search.AddGroup("Search Options", "", &SearchOption{})
opts.OutputOption = newOutputOption()
opts.SearchOption = newSearchOption()
parser := flags.NewParser(opts, flags.Default)
parser.Name = "pt"
parser.Usage = "[OPTIONS] PATTERN [PATH]"
return parser
}