usage.go 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. // Copyright 2015 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. // Contains the geth command usage template and generator.
  17. package main
  18. import (
  19. "io"
  20. "github.com/ethereum/go-ethereum/cmd/utils"
  21. "github.com/ethereum/go-ethereum/internal/debug"
  22. "gopkg.in/urfave/cli.v1"
  23. )
  24. // AppHelpTemplate is the test template for the default, global app help topic.
  25. var AppHelpTemplate = `NAME:
  26. {{.App.Name}} - {{.App.Usage}}
  27. Copyright 2013-2016 The go-ethereum Authors
  28. USAGE:
  29. {{.App.HelpName}} [options]{{if .App.Commands}} command [command options]{{end}} {{if .App.ArgsUsage}}{{.App.ArgsUsage}}{{else}}[arguments...]{{end}}
  30. {{if .App.Version}}
  31. VERSION:
  32. {{.App.Version}}
  33. {{end}}{{if len .App.Authors}}
  34. AUTHOR(S):
  35. {{range .App.Authors}}{{ . }}{{end}}
  36. {{end}}{{if .App.Commands}}
  37. COMMANDS:
  38. {{range .App.Commands}}{{join .Names ", "}}{{ "\t" }}{{.Usage}}
  39. {{end}}{{end}}{{if .FlagGroups}}
  40. {{range .FlagGroups}}{{.Name}} OPTIONS:
  41. {{range .Flags}}{{.}}
  42. {{end}}
  43. {{end}}{{end}}{{if .App.Copyright }}
  44. COPYRIGHT:
  45. {{.App.Copyright}}
  46. {{end}}
  47. `
  48. // flagGroup is a collection of flags belonging to a single topic.
  49. type flagGroup struct {
  50. Name string
  51. Flags []cli.Flag
  52. }
  53. // AppHelpFlagGroups is the application flags, grouped by functionality.
  54. var AppHelpFlagGroups = []flagGroup{
  55. {
  56. Name: "ETHEREUM",
  57. Flags: []cli.Flag{
  58. utils.DataDirFlag,
  59. utils.KeyStoreDirFlag,
  60. utils.NetworkIdFlag,
  61. utils.OlympicFlag,
  62. utils.TestNetFlag,
  63. utils.DevModeFlag,
  64. utils.IdentityFlag,
  65. utils.FastSyncFlag,
  66. utils.LightKDFFlag,
  67. utils.CacheFlag,
  68. },
  69. },
  70. {
  71. Name: "ACCOUNT",
  72. Flags: []cli.Flag{
  73. utils.UnlockedAccountFlag,
  74. utils.PasswordFileFlag,
  75. },
  76. },
  77. {
  78. Name: "API AND CONSOLE",
  79. Flags: []cli.Flag{
  80. utils.RPCEnabledFlag,
  81. utils.RPCListenAddrFlag,
  82. utils.RPCPortFlag,
  83. utils.RPCApiFlag,
  84. utils.WSEnabledFlag,
  85. utils.WSListenAddrFlag,
  86. utils.WSPortFlag,
  87. utils.WSApiFlag,
  88. utils.WSAllowedOriginsFlag,
  89. utils.IPCDisabledFlag,
  90. utils.IPCApiFlag,
  91. utils.IPCPathFlag,
  92. utils.RPCCORSDomainFlag,
  93. utils.JSpathFlag,
  94. utils.ExecFlag,
  95. utils.PreloadJSFlag,
  96. },
  97. },
  98. {
  99. Name: "NETWORKING",
  100. Flags: []cli.Flag{
  101. utils.BootnodesFlag,
  102. utils.ListenPortFlag,
  103. utils.MaxPeersFlag,
  104. utils.MaxPendingPeersFlag,
  105. utils.NATFlag,
  106. utils.NoDiscoverFlag,
  107. utils.NodeKeyFileFlag,
  108. utils.NodeKeyHexFlag,
  109. },
  110. },
  111. {
  112. Name: "MINER",
  113. Flags: []cli.Flag{
  114. utils.MiningEnabledFlag,
  115. utils.MinerThreadsFlag,
  116. utils.MiningGPUFlag,
  117. utils.AutoDAGFlag,
  118. utils.EtherbaseFlag,
  119. utils.TargetGasLimitFlag,
  120. utils.GasPriceFlag,
  121. utils.ExtraDataFlag,
  122. },
  123. },
  124. {
  125. Name: "GAS PRICE ORACLE",
  126. Flags: []cli.Flag{
  127. utils.GpoMinGasPriceFlag,
  128. utils.GpoMaxGasPriceFlag,
  129. utils.GpoFullBlockRatioFlag,
  130. utils.GpobaseStepDownFlag,
  131. utils.GpobaseStepUpFlag,
  132. utils.GpobaseCorrectionFactorFlag,
  133. },
  134. },
  135. {
  136. Name: "VIRTUAL MACHINE",
  137. Flags: []cli.Flag{
  138. utils.VMEnableJitFlag,
  139. utils.VMForceJitFlag,
  140. utils.VMJitCacheFlag,
  141. },
  142. },
  143. {
  144. Name: "LOGGING AND DEBUGGING",
  145. Flags: append([]cli.Flag{
  146. utils.MetricsEnabledFlag,
  147. utils.FakePoWFlag,
  148. }, debug.Flags...),
  149. },
  150. {
  151. Name: "EXPERIMENTAL",
  152. Flags: []cli.Flag{
  153. utils.WhisperEnabledFlag,
  154. utils.NatspecEnabledFlag,
  155. },
  156. },
  157. {
  158. Name: "MISCELLANEOUS",
  159. Flags: []cli.Flag{
  160. utils.SolcPathFlag,
  161. },
  162. },
  163. }
  164. func init() {
  165. // Override the default app help template
  166. cli.AppHelpTemplate = AppHelpTemplate
  167. // Define a one shot struct to pass to the usage template
  168. type helpData struct {
  169. App interface{}
  170. FlagGroups []flagGroup
  171. }
  172. // Override the default app help printer, but only for the global app help
  173. originalHelpPrinter := cli.HelpPrinter
  174. cli.HelpPrinter = func(w io.Writer, tmpl string, data interface{}) {
  175. if tmpl == AppHelpTemplate {
  176. // Iterate over all the flags and add any uncategorized ones
  177. categorized := make(map[string]struct{})
  178. for _, group := range AppHelpFlagGroups {
  179. for _, flag := range group.Flags {
  180. categorized[flag.String()] = struct{}{}
  181. }
  182. }
  183. uncategorized := []cli.Flag{}
  184. for _, flag := range data.(*cli.App).Flags {
  185. if _, ok := categorized[flag.String()]; !ok {
  186. uncategorized = append(uncategorized, flag)
  187. }
  188. }
  189. if len(uncategorized) > 0 {
  190. // Append all ungategorized options to the misc group
  191. miscs := len(AppHelpFlagGroups[len(AppHelpFlagGroups)-1].Flags)
  192. AppHelpFlagGroups[len(AppHelpFlagGroups)-1].Flags = append(AppHelpFlagGroups[len(AppHelpFlagGroups)-1].Flags, uncategorized...)
  193. // Make sure they are removed afterwards
  194. defer func() {
  195. AppHelpFlagGroups[len(AppHelpFlagGroups)-1].Flags = AppHelpFlagGroups[len(AppHelpFlagGroups)-1].Flags[:miscs]
  196. }()
  197. }
  198. // Render out custom usage screen
  199. originalHelpPrinter(w, tmpl, helpData{data, AppHelpFlagGroups})
  200. } else {
  201. originalHelpPrinter(w, tmpl, data)
  202. }
  203. }
  204. }