1 // Copyright 2018 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
5 // Package singlechecker defines the main function for an analysis
6 // driver with only a single analysis.
7 // This package makes it easy for a provider of an analysis package to
8 // also provide a standalone tool that runs just that analysis.
10 // For example, if example.org/findbadness is an analysis package,
11 // all that is needed to define a standalone tool is a file,
12 // example.org/findbadness/cmd/findbadness/main.go, containing:
14 // // The findbadness command runs an analysis.
18 // "example.org/findbadness"
19 // "golang.org/x/tools/go/analysis/singlechecker"
22 // func main() { singlechecker.Main(findbadness.Analyzer) }
33 "golang.org/x/tools/go/analysis"
34 "golang.org/x/tools/go/analysis/internal/analysisflags"
35 "golang.org/x/tools/go/analysis/internal/checker"
36 "golang.org/x/tools/go/analysis/unitchecker"
39 // Main is the main function for a checker command for a single analysis.
40 func Main(a *analysis.Analyzer) {
42 log.SetPrefix(a.Name + ": ")
44 analyzers := []*analysis.Analyzer{a}
46 if err := analysis.Validate(analyzers); err != nil {
50 checker.RegisterFlags()
53 paras := strings.Split(a.Doc, "\n\n")
54 fmt.Fprintf(os.Stderr, "%s: %s\n\n", a.Name, paras[0])
55 fmt.Fprintf(os.Stderr, "Usage: %s [-flag] [package]\n\n", a.Name)
57 fmt.Fprintln(os.Stderr, strings.Join(paras[1:], "\n\n"))
59 fmt.Fprintln(os.Stderr, "\nFlags:")
63 analyzers = analysisflags.Parse(analyzers, false)
71 if len(args) == 1 && strings.HasSuffix(args[0], ".cfg") {
72 unitchecker.Run(args[0], analyzers)
76 os.Exit(checker.Run(args, analyzers))