/
flags.go
266 lines (213 loc) · 10.3 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
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
package cmd
import (
"flag"
"os"
"path/filepath"
"runtime"
"time"
)
const AllNonNoticeChecks = "<all-non-notice>"
const AllChecks = "<all>"
type ParsedFlags struct {
PHP7 bool
PprofHost string
CPUProfile string
MemProfile string
StrictMixed bool
IgnoreVendor bool
MaxFileSize int
FullAnalysisFiles string
IndexOnlyFiles string
CheckAutoGenerated bool
RulesList string
Output string
OutputJSON bool
OutputBaseline bool
Debug bool
DebugParseDuration time.Duration
MaxConcurrency int
StubsDir string
CacheDir string
DisableCache bool
IgnoreTriggerError bool
ApplyQuickFixes bool
KPHP bool
Baseline string
ConservativeBaseline bool
MisspellList string
UnusedVarPattern string
AllowAll bool
AllowChecks string
AllowDisable string
ReportsExclude string
ReportsExcludeChecks string
ReportsCritical string
PhpExtensionsArg string
Gitignore bool
GitPushArg string
GitAuthorsWhitelist string
GitWorkTree string
GitSkipFetch bool
GitDisableCompensateMaster bool
GitFullDiff bool
GitIncludeUntracked bool
GitRepo string
// These two flags are mutated in prepareGitArgs.
// This is bad, but it's easier for now than to fix this
// without introducing other issues.
Mutable struct {
GitCommitFrom string
GitCommitTo string
}
}
type FlagsGroups struct {
Groups []string
Map map[string][]string
}
func NewFlagsGroups() *FlagsGroups {
return &FlagsGroups{Map: make(map[string][]string)}
}
func (fg *FlagsGroups) AddGroup(group string) {
fg.Groups = append(fg.Groups, group)
}
func (fg *FlagsGroups) Add(group, name string) {
fg.Map[group] = append(fg.Map[group], name)
}
func DefaultCacheDir() string {
defaultCacheDir, err := os.UserCacheDir()
if err != nil {
defaultCacheDir = ""
} else {
defaultCacheDir = filepath.Join(defaultCacheDir, "noverify-cache")
}
return defaultCacheDir
}
func RegisterCheckFlags(ctx *AppContext) (*flag.FlagSet, *FlagsGroups) {
fs := flag.NewFlagSet("check", flag.ContinueOnError)
groups := NewFlagsGroups()
groups.AddGroup("Checks")
groups.AddGroup("Files")
groups.AddGroup("Analyze setting")
groups.AddGroup("Additional")
groups.AddGroup("Output")
groups.AddGroup("Diff mode")
groups.AddGroup("Baseline")
groups.AddGroup("Dynamic rules")
groups.AddGroup("Debug")
groups.AddGroup("Deprecated")
// Checks group.
fs.StringVar(&ctx.ParsedFlags.AllowChecks, "allow-checks", AllChecks,
"Comma-separated list of check names to be enabled")
fs.BoolVar(&ctx.ParsedFlags.AllowAll, "allow-all-checks", false,
"Enable all checks, including those disabled by default")
fs.StringVar(&ctx.ParsedFlags.ReportsExcludeChecks, "exclude-checks", "",
"Comma-separated list of check names to be excluded")
fs.StringVar(&ctx.ParsedFlags.ReportsCritical, "critical", AllNonNoticeChecks,
"Comma-separated list of check names that are considered critical (all non-notice checks by default)")
groups.Add("Checks", "allow-checks")
groups.Add("Checks", "allow-all-checks")
groups.Add("Checks", "exclude-checks")
groups.Add("Checks", "critical")
// Files group.
fs.StringVar(&ctx.ParsedFlags.FullAnalysisFiles, "full-analysis-files", "", "Comma-separated list of files to do full analysis")
fs.StringVar(&ctx.ParsedFlags.IndexOnlyFiles, "index-only-files", "", "Comma-separated list of files to do only indexing")
fs.StringVar(&ctx.ParsedFlags.ReportsExclude, "exclude", "", "Exclude regexp for filenames in reports list")
fs.StringVar(&ctx.ParsedFlags.PhpExtensionsArg, "php-extensions", "php,inc,php5,phtml", "Comma-separated list of PHP extensions to be recognized")
fs.StringVar(&ctx.ParsedFlags.AllowDisable, "allow-disable", "", "Regexp for filenames where '@linter disable' is allowed")
groups.Add("Files", "index-only-files")
groups.Add("Files", "php-extensions")
groups.Add("Files", "exclude")
groups.Add("Files", "allow-disable")
groups.Add("Files", "full-analysis-files")
// Analyze setting group.
fs.IntVar(&ctx.ParsedFlags.MaxConcurrency, "cores", runtime.NumCPU(), "Max cores")
fs.StringVar(&ctx.ParsedFlags.StubsDir, "stubs-dir", "", "Directory with phpstorm-stubs")
fs.StringVar(&ctx.ParsedFlags.CacheDir, "cache-dir", DefaultCacheDir(), "Directory for linter cache (greatly improves indexing speed)")
fs.BoolVar(&ctx.ParsedFlags.DisableCache, "disable-cache", false, "Do not use cache, cache-dir will be ignored")
fs.IntVar(&ctx.ParsedFlags.MaxFileSize, "max-sum-filesize", 20*1024*1024, "Max total file size to be parsed concurrently in bytes (limits max memory consumption)")
fs.StringVar(&ctx.ParsedFlags.MisspellList, "misspell-list", "Eng",
"Comma-separated list of misspelling dicts; predefined sets are Eng, Eng/US and Eng/UK")
fs.BoolVar(&ctx.ParsedFlags.IgnoreVendor, "ignore-vendor", false,
"Do not use the vendor folder to get information about functions and classes (not recommended as type inference will work much worse)")
fs.BoolVar(&ctx.ParsedFlags.PHP7, "php7", false, "Analyze as PHP 7")
fs.BoolVar(&ctx.ParsedFlags.StrictMixed, "strict-mixed", false, "Use strict mixed mode")
groups.Add("Analyze setting", "strict-mixed")
groups.Add("Analyze setting", "php7")
groups.Add("Analyze setting", "cores")
groups.Add("Analyze setting", "cache-dir")
groups.Add("Analyze setting", "disable-cache")
groups.Add("Analyze setting", "misspell-list")
groups.Add("Analyze setting", "max-sum-filesize")
groups.Add("Analyze setting", "stubs-dir")
groups.Add("Analyze setting", "ignore-vendor")
// Additional group.
fs.BoolVar(&ctx.ParsedFlags.CheckAutoGenerated, "check-auto-generated", false, "Check auto-generated PHP files")
fs.BoolVar(&ctx.ParsedFlags.IgnoreTriggerError, "ignore-trigger-error", false, "Do not treat trigger_error as a function to stop script execution")
fs.BoolVar(&ctx.ParsedFlags.ApplyQuickFixes, "fix", false, "Apply a quickfix where possible (updates source files)")
fs.BoolVar(&ctx.ParsedFlags.KPHP, "kphp", false, "Interpret code as KPHP")
fs.StringVar(&ctx.ParsedFlags.UnusedVarPattern, "unused-var-regex", `^_$`,
"Regexp that specifies variable name that will not be given an unused warning, but which should not be used as values")
groups.Add("Additional", "check-auto-generated")
groups.Add("Additional", "ignore-trigger-error")
groups.Add("Additional", "unused-var-regex")
groups.Add("Additional", "fix")
groups.Add("Additional", "kphp")
// Output group.
fs.StringVar(&ctx.ParsedFlags.Output, "output", "", "Output reports to a specified file instead of stderr")
fs.BoolVar(&ctx.ParsedFlags.OutputJSON, "output-json", false, "Format output as JSON")
groups.Add("Output", "output")
groups.Add("Output", "output-json")
// Git group.
fs.BoolVar(&ctx.ParsedFlags.Gitignore, "gitignore", false,
"If enabled, noverify tries to use .gitignore files to exclude matched ignored files from the analysis")
fs.StringVar(&ctx.ParsedFlags.GitRepo, "git", "", "Path to folder with git repository")
fs.StringVar(&ctx.ParsedFlags.Mutable.GitCommitFrom, "git-commit-from", "", "Analyze changes between commits <git-commit-from> and <git-commit-to>")
fs.StringVar(&ctx.ParsedFlags.Mutable.GitCommitTo, "git-commit-to", "", "Analyze changes between commits <git-commit-from> and <git-commit-to>")
fs.StringVar(&ctx.ParsedFlags.GitPushArg, "git-push-arg", "", "In {pre,post}-receive hooks a whole line from stdin can be passed")
fs.StringVar(&ctx.ParsedFlags.GitAuthorsWhitelist, "git-author-whitelist", "", "Comma-separated whitelist for commit authors for which changes the linter will analyze")
fs.StringVar(&ctx.ParsedFlags.GitWorkTree, "git-work-tree", "", "Working directory in which the --git-include-untracked flag will be taken into account")
fs.BoolVar(&ctx.ParsedFlags.GitIncludeUntracked, "git-include-untracked", true, "Include untracked (new, uncommitted files) into analysis")
fs.BoolVar(&ctx.ParsedFlags.GitSkipFetch, "git-skip-fetch", false, "Do not fetch ORIGIN_MASTER (use this option if you already fetch to ORIGIN_MASTER before that)")
fs.BoolVar(&ctx.ParsedFlags.GitDisableCompensateMaster, "git-disable-compensate-master", false, "Do not try to compensate for changes in ORIGIN_MASTER after branch point")
fs.BoolVar(&ctx.ParsedFlags.GitFullDiff, "git-full-diff", false, "Compute full diff, in which linter analyzes all files, not just changed ones")
groups.Add("Diff mode", "git")
groups.Add("Diff mode", "git-full-diff")
groups.Add("Diff mode", "git-commit-from")
groups.Add("Diff mode", "git-commit-to")
groups.Add("Diff mode", "git-author-whitelist")
groups.Add("Diff mode", "git-disable-compensate-master")
groups.Add("Diff mode", "git-include-untracked")
groups.Add("Diff mode", "git-work-tree")
groups.Add("Diff mode", "git-skip-fetch")
groups.Add("Diff mode", "git-push-arg")
groups.Add("Diff mode", "gitignore")
// Baseline group.
fs.StringVar(&ctx.ParsedFlags.Baseline, "baseline", "",
"Path to a suppress profile created by --output-baseline")
fs.BoolVar(&ctx.ParsedFlags.ConservativeBaseline, "conservative-baseline", false,
"Use a conservative baseline mode in which it will have less false positive, but more false negatives")
fs.BoolVar(&ctx.ParsedFlags.OutputBaseline, "output-baseline", false, "Output a suppression profile instead of reports")
groups.Add("Baseline", "baseline")
groups.Add("Baseline", "conservative-baseline")
groups.Add("Baseline", "output-baseline")
// Dynamic rules group.
fs.StringVar(&ctx.ParsedFlags.RulesList, "rules", "",
"Comma-separated list of files or folders with dynamic rules")
groups.Add("Dynamic rules", "rules")
// Debug group.
fs.StringVar(&ctx.ParsedFlags.PprofHost, "pprof", "", "HTTP pprof endpoint (e.g. localhost:8080)")
fs.BoolVar(&ctx.ParsedFlags.Debug, "debug", false, "Enable debug output")
fs.DurationVar(&ctx.ParsedFlags.DebugParseDuration, "debug-parse-duration", 0, "Print files that took longer than the specified time to analyse")
fs.StringVar(&ctx.ParsedFlags.CPUProfile, "cpuprofile", "", "Write cpu profile to 'file'")
fs.StringVar(&ctx.ParsedFlags.MemProfile, "memprofile", "", "Write memory profile to 'file'")
groups.Add("Debug", "debug")
groups.Add("Debug", "debug-parse-duration")
groups.Add("Debug", "pprof")
groups.Add("Debug", "cpuprofile")
groups.Add("Debug", "memprofile")
// Deprecated group.
var encodingUnused string
fs.StringVar(&encodingUnused, "encoding", "", "Deprecated and unused")
groups.Add("Deprecated", "encoding")
return fs, groups
}