Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.
 
 
 

71 строка
1.2 KiB

  1. // Copyright 2013 The Go Authors. All rights reserved.
  2. //
  3. // Use of this source code is governed by a BSD-style
  4. // license that can be found in the LICENSE file or at
  5. // https://developers.google.com/open-source/licenses/bsd.
  6. // Command gddo-admin is the GoDoc.org command line administration tool.
  7. package main
  8. import (
  9. "flag"
  10. "fmt"
  11. "os"
  12. "strings"
  13. )
  14. type command struct {
  15. name string
  16. run func(c *command)
  17. flag flag.FlagSet
  18. usage string
  19. }
  20. func (c *command) printUsage() {
  21. fmt.Fprintf(os.Stderr, "%s %s\n", os.Args[0], c.usage)
  22. c.flag.PrintDefaults()
  23. }
  24. var commands = []*command{
  25. blockCommand,
  26. reindexCommand,
  27. deleteCommand,
  28. popularCommand,
  29. dangleCommand,
  30. crawlCommand,
  31. statsCommand,
  32. }
  33. func printUsage() {
  34. var n []string
  35. for _, c := range commands {
  36. n = append(n, c.name)
  37. }
  38. fmt.Fprintf(os.Stderr, "%s %s\n", os.Args[0], strings.Join(n, "|"))
  39. flag.PrintDefaults()
  40. for _, c := range commands {
  41. c.printUsage()
  42. }
  43. }
  44. func main() {
  45. flag.Usage = printUsage
  46. flag.Parse()
  47. args := flag.Args()
  48. if len(args) >= 1 {
  49. for _, c := range commands {
  50. if args[0] == c.name {
  51. c.flag.Usage = func() {
  52. c.printUsage()
  53. os.Exit(2)
  54. }
  55. c.flag.Parse(args[1:])
  56. c.run(c)
  57. return
  58. }
  59. }
  60. }
  61. printUsage()
  62. os.Exit(2)
  63. }