11 "golang.org/x/tools/go/analysis"
14 const help = `PROGNAME is a tool for static analysis of Go programs.
16 PROGNAME examines Go source code and reports suspicious constructs,
17 such as Printf calls whose arguments do not align with the format
18 string. It uses heuristics that do not guarantee all reports are
19 genuine problems, but it can find errors not caught by the compilers.
22 // Help implements the help subcommand for a multichecker or unitchecker
23 // style command. The optional args specify the analyzers to describe.
24 // Help calls log.Fatal if no such analyzer exists.
25 func Help(progname string, analyzers []*analysis.Analyzer, args []string) {
26 // No args: show summary of all analyzers.
28 fmt.Println(strings.Replace(help, "PROGNAME", progname, -1))
29 fmt.Println("Registered analyzers:")
31 sort.Slice(analyzers, func(i, j int) bool {
32 return analyzers[i].Name < analyzers[j].Name
34 for _, a := range analyzers {
35 title := strings.Split(a.Doc, "\n\n")[0]
36 fmt.Printf(" %-12s %s\n", a.Name, title)
38 fmt.Println("\nBy default all analyzers are run.")
39 fmt.Println("To select specific analyzers, use the -NAME flag for each one,")
40 fmt.Println(" or -NAME=false to run all analyzers not explicitly disabled.")
42 // Show only the core command-line flags.
43 fmt.Println("\nCore flags:")
45 fs := flag.NewFlagSet("", flag.ExitOnError)
46 flag.VisitAll(func(f *flag.Flag) {
47 if !strings.Contains(f.Name, ".") {
48 fs.Var(f.Value, f.Name, f.Usage)
51 fs.SetOutput(os.Stdout)
54 fmt.Printf("\nTo see details and flags of a specific analyzer, run '%s help name'.\n", progname)
59 // Show help on specific analyzer(s).
61 for _, arg := range args {
62 for _, a := range analyzers {
64 paras := strings.Split(a.Doc, "\n\n")
66 fmt.Printf("%s: %s\n", a.Name, title)
68 // Show only the flags relating to this analysis,
71 fs := flag.NewFlagSet(a.Name, flag.ExitOnError)
72 a.Flags.VisitAll(func(f *flag.Flag) {
75 fmt.Println("\nAnalyzer flags:")
78 fs.Var(f.Value, a.Name+"."+f.Name, f.Usage)
80 fs.SetOutput(os.Stdout)
84 fmt.Printf("\n%s\n", strings.Join(paras[1:], "\n\n"))
90 log.Fatalf("Analyzer %q not registered", arg)