You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

help.go 2.5KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. package analysisflags
  2. import (
  3. "flag"
  4. "fmt"
  5. "log"
  6. "os"
  7. "sort"
  8. "strings"
  9. "golang.org/x/tools/go/analysis"
  10. )
  11. const help = `PROGNAME is a tool for static analysis of Go programs.
  12. PROGNAME examines Go source code and reports suspicious constructs,
  13. such as Printf calls whose arguments do not align with the format
  14. string. It uses heuristics that do not guarantee all reports are
  15. genuine problems, but it can find errors not caught by the compilers.
  16. `
  17. // Help implements the help subcommand for a multichecker or unitchecker
  18. // style command. The optional args specify the analyzers to describe.
  19. // Help calls log.Fatal if no such analyzer exists.
  20. func Help(progname string, analyzers []*analysis.Analyzer, args []string) {
  21. // No args: show summary of all analyzers.
  22. if len(args) == 0 {
  23. fmt.Println(strings.Replace(help, "PROGNAME", progname, -1))
  24. fmt.Println("Registered analyzers:")
  25. fmt.Println()
  26. sort.Slice(analyzers, func(i, j int) bool {
  27. return analyzers[i].Name < analyzers[j].Name
  28. })
  29. for _, a := range analyzers {
  30. title := strings.Split(a.Doc, "\n\n")[0]
  31. fmt.Printf(" %-12s %s\n", a.Name, title)
  32. }
  33. fmt.Println("\nBy default all analyzers are run.")
  34. fmt.Println("To select specific analyzers, use the -NAME flag for each one,")
  35. fmt.Println(" or -NAME=false to run all analyzers not explicitly disabled.")
  36. // Show only the core command-line flags.
  37. fmt.Println("\nCore flags:")
  38. fmt.Println()
  39. fs := flag.NewFlagSet("", flag.ExitOnError)
  40. flag.VisitAll(func(f *flag.Flag) {
  41. if !strings.Contains(f.Name, ".") {
  42. fs.Var(f.Value, f.Name, f.Usage)
  43. }
  44. })
  45. fs.SetOutput(os.Stdout)
  46. fs.PrintDefaults()
  47. fmt.Printf("\nTo see details and flags of a specific analyzer, run '%s help name'.\n", progname)
  48. return
  49. }
  50. // Show help on specific analyzer(s).
  51. outer:
  52. for _, arg := range args {
  53. for _, a := range analyzers {
  54. if a.Name == arg {
  55. paras := strings.Split(a.Doc, "\n\n")
  56. title := paras[0]
  57. fmt.Printf("%s: %s\n", a.Name, title)
  58. // Show only the flags relating to this analysis,
  59. // properly prefixed.
  60. first := true
  61. fs := flag.NewFlagSet(a.Name, flag.ExitOnError)
  62. a.Flags.VisitAll(func(f *flag.Flag) {
  63. if first {
  64. first = false
  65. fmt.Println("\nAnalyzer flags:")
  66. fmt.Println()
  67. }
  68. fs.Var(f.Value, a.Name+"."+f.Name, f.Usage)
  69. })
  70. fs.SetOutput(os.Stdout)
  71. fs.PrintDefaults()
  72. if len(paras) > 1 {
  73. fmt.Printf("\n%s\n", strings.Join(paras[1:], "\n\n"))
  74. }
  75. continue outer
  76. }
  77. }
  78. log.Fatalf("Analyzer %q not registered", arg)
  79. }
  80. }