usage.go 5.3 KB

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