...

Source file src/cmd/vendor/golang.org/x/tools/go/analysis/internal/analysisflags/help.go

     1	package analysisflags
     2	
     3	import (
     4		"flag"
     5		"fmt"
     6		"log"
     7		"os"
     8		"sort"
     9		"strings"
    10	
    11		"golang.org/x/tools/go/analysis"
    12	)
    13	
    14	const help = `PROGNAME is a tool for static analysis of Go programs.
    15	
    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.
    20	`
    21	
    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.
    27		if len(args) == 0 {
    28			fmt.Println(strings.Replace(help, "PROGNAME", progname, -1))
    29			fmt.Println("Registered analyzers:")
    30			fmt.Println()
    31			sort.Slice(analyzers, func(i, j int) bool {
    32				return analyzers[i].Name < analyzers[j].Name
    33			})
    34			for _, a := range analyzers {
    35				title := strings.Split(a.Doc, "\n\n")[0]
    36				fmt.Printf("    %-12s %s\n", a.Name, title)
    37			}
    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.")
    41	
    42			// Show only the core command-line flags.
    43			fmt.Println("\nCore flags:")
    44			fmt.Println()
    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)
    49				}
    50			})
    51			fs.SetOutput(os.Stdout)
    52			fs.PrintDefaults()
    53	
    54			fmt.Printf("\nTo see details and flags of a specific analyzer, run '%s help name'.\n", progname)
    55	
    56			return
    57		}
    58	
    59		// Show help on specific analyzer(s).
    60	outer:
    61		for _, arg := range args {
    62			for _, a := range analyzers {
    63				if a.Name == arg {
    64					paras := strings.Split(a.Doc, "\n\n")
    65					title := paras[0]
    66					fmt.Printf("%s: %s\n", a.Name, title)
    67	
    68					// Show only the flags relating to this analysis,
    69					// properly prefixed.
    70					first := true
    71					fs := flag.NewFlagSet(a.Name, flag.ExitOnError)
    72					a.Flags.VisitAll(func(f *flag.Flag) {
    73						if first {
    74							first = false
    75							fmt.Println("\nAnalyzer flags:")
    76							fmt.Println()
    77						}
    78						fs.Var(f.Value, a.Name+"."+f.Name, f.Usage)
    79					})
    80					fs.SetOutput(os.Stdout)
    81					fs.PrintDefaults()
    82	
    83					if len(paras) > 1 {
    84						fmt.Printf("\n%s\n", strings.Join(paras[1:], "\n\n"))
    85					}
    86	
    87					continue outer
    88				}
    89			}
    90			log.Fatalf("Analyzer %q not registered", arg)
    91		}
    92	}
    93	

View as plain text