helpers.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. // Copyright 2020 The go-ethereum Authors
  2. // This file is part of go-ethereum.
  3. //
  4. // go-ethereum is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // go-ethereum is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU General Public License
  15. // along with go-ethereum. If not, see <http://www.gnu.org/licenses/>.
  16. package flags
  17. import (
  18. "os"
  19. "path/filepath"
  20. "github.com/ethereum/go-ethereum/params"
  21. "gopkg.in/urfave/cli.v1"
  22. )
  23. var (
  24. CommandHelpTemplate = `{{.cmd.Name}}{{if .cmd.Subcommands}} command{{end}}{{if .cmd.Flags}} [command options]{{end}} {{.cmd.ArgsUsage}}
  25. {{if .cmd.Description}}{{.cmd.Description}}
  26. {{end}}{{if .cmd.Subcommands}}
  27. SUBCOMMANDS:
  28. {{range .cmd.Subcommands}}{{.Name}}{{with .ShortName}}, {{.}}{{end}}{{ "\t" }}{{.Usage}}
  29. {{end}}{{end}}{{if .categorizedFlags}}
  30. {{range $idx, $categorized := .categorizedFlags}}{{$categorized.Name}} OPTIONS:
  31. {{range $categorized.Flags}}{{"\t"}}{{.}}
  32. {{end}}
  33. {{end}}{{end}}`
  34. OriginCommandHelpTemplate = `{{.Name}}{{if .Subcommands}} command{{end}}{{if .Flags}} [command options]{{end}} {{.ArgsUsage}}
  35. {{if .Description}}{{.Description}}
  36. {{end}}{{if .Subcommands}}
  37. SUBCOMMANDS:
  38. {{range .Subcommands}}{{.Name}}{{with .ShortName}}, {{.}}{{end}}{{ "\t" }}{{.Usage}}
  39. {{end}}{{end}}{{if .Flags}}
  40. OPTIONS:
  41. {{range $.Flags}} {{.}}
  42. {{end}}
  43. {{end}}`
  44. // AppHelpTemplate is the test template for the default, global app help topic.
  45. AppHelpTemplate = `NAME:
  46. {{.App.Name}} - {{.App.Usage}}
  47. Copyright 2013-2021 The go-ethereum Authors
  48. USAGE:
  49. {{.App.HelpName}} [options]{{if .App.Commands}} [command] [command options]{{end}} {{if .App.ArgsUsage}}{{.App.ArgsUsage}}{{else}}[arguments...]{{end}}
  50. {{if .App.Version}}
  51. VERSION:
  52. {{.App.Version}}
  53. {{end}}{{if len .App.Authors}}
  54. AUTHOR(S):
  55. {{range .App.Authors}}{{ . }}{{end}}
  56. {{end}}{{if .App.Commands}}
  57. COMMANDS:
  58. {{range .App.Commands}}{{join .Names ", "}}{{ "\t" }}{{.Usage}}
  59. {{end}}{{end}}{{if .FlagGroups}}
  60. {{range .FlagGroups}}{{.Name}} OPTIONS:
  61. {{range .Flags}}{{.}}
  62. {{end}}
  63. {{end}}{{end}}{{if .App.Copyright }}
  64. COPYRIGHT:
  65. {{.App.Copyright}}
  66. {{end}}
  67. `
  68. // ClefAppHelpTemplate is the template for the default, global app help topic.
  69. ClefAppHelpTemplate = `NAME:
  70. {{.App.Name}} - {{.App.Usage}}
  71. Copyright 2013-2021 The go-ethereum Authors
  72. USAGE:
  73. {{.App.HelpName}} [options]{{if .App.Commands}} command [command options]{{end}} {{if .App.ArgsUsage}}{{.App.ArgsUsage}}{{else}}[arguments...]{{end}}
  74. {{if .App.Version}}
  75. COMMANDS:
  76. {{range .App.Commands}}{{join .Names ", "}}{{ "\t" }}{{.Usage}}
  77. {{end}}{{end}}{{if .FlagGroups}}
  78. {{range .FlagGroups}}{{.Name}} OPTIONS:
  79. {{range .Flags}}{{.}}
  80. {{end}}
  81. {{end}}{{end}}{{if .App.Copyright }}
  82. COPYRIGHT:
  83. {{.App.Copyright}}
  84. {{end}}
  85. `
  86. )
  87. // HelpData is a one shot struct to pass to the usage template
  88. type HelpData struct {
  89. App interface{}
  90. FlagGroups []FlagGroup
  91. }
  92. // FlagGroup is a collection of flags belonging to a single topic.
  93. type FlagGroup struct {
  94. Name string
  95. Flags []cli.Flag
  96. }
  97. // byCategory sorts an array of FlagGroup by Name in the order
  98. // defined in AppHelpFlagGroups.
  99. type ByCategory []FlagGroup
  100. func (a ByCategory) Len() int { return len(a) }
  101. func (a ByCategory) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
  102. func (a ByCategory) Less(i, j int) bool {
  103. iCat, jCat := a[i].Name, a[j].Name
  104. iIdx, jIdx := len(a), len(a) // ensure non categorized flags come last
  105. for i, group := range a {
  106. if iCat == group.Name {
  107. iIdx = i
  108. }
  109. if jCat == group.Name {
  110. jIdx = i
  111. }
  112. }
  113. return iIdx < jIdx
  114. }
  115. func FlagCategory(flag cli.Flag, flagGroups []FlagGroup) string {
  116. for _, category := range flagGroups {
  117. for _, flg := range category.Flags {
  118. if flg.GetName() == flag.GetName() {
  119. return category.Name
  120. }
  121. }
  122. }
  123. return "MISC"
  124. }
  125. // NewApp creates an app with sane defaults.
  126. func NewApp(gitCommit, gitDate, usage string) *cli.App {
  127. app := cli.NewApp()
  128. app.Name = filepath.Base(os.Args[0])
  129. app.Author = ""
  130. app.Email = ""
  131. app.Version = params.VersionWithCommit(gitCommit, gitDate)
  132. app.Usage = usage
  133. return app
  134. }