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