usage.go 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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.NetworkIdFlag,
  59. utils.OlympicFlag,
  60. utils.TestNetFlag,
  61. utils.DevModeFlag,
  62. utils.GenesisFileFlag,
  63. utils.IdentityFlag,
  64. utils.FastSyncFlag,
  65. utils.LightKDFFlag,
  66. utils.CacheFlag,
  67. utils.BlockchainVersionFlag,
  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.WSAllowedDomainsFlag,
  89. utils.IPCDisabledFlag,
  90. utils.IPCApiFlag,
  91. utils.IPCPathFlag,
  92. utils.RPCCORSDomainFlag,
  93. utils.JSpathFlag,
  94. utils.ExecFlag,
  95. },
  96. },
  97. {
  98. Name: "NETWORKING",
  99. Flags: []cli.Flag{
  100. utils.BootnodesFlag,
  101. utils.ListenPortFlag,
  102. utils.MaxPeersFlag,
  103. utils.MaxPendingPeersFlag,
  104. utils.NATFlag,
  105. utils.NoDiscoverFlag,
  106. utils.NodeKeyFileFlag,
  107. utils.NodeKeyHexFlag,
  108. },
  109. },
  110. {
  111. Name: "MINER",
  112. Flags: []cli.Flag{
  113. utils.MiningEnabledFlag,
  114. utils.MinerThreadsFlag,
  115. utils.MiningGPUFlag,
  116. utils.AutoDAGFlag,
  117. utils.EtherbaseFlag,
  118. utils.GasPriceFlag,
  119. utils.ExtraDataFlag,
  120. },
  121. },
  122. {
  123. Name: "GAS PRICE ORACLE",
  124. Flags: []cli.Flag{
  125. utils.GpoMinGasPriceFlag,
  126. utils.GpoMaxGasPriceFlag,
  127. utils.GpoFullBlockRatioFlag,
  128. utils.GpobaseStepDownFlag,
  129. utils.GpobaseStepUpFlag,
  130. utils.GpobaseCorrectionFactorFlag,
  131. },
  132. },
  133. {
  134. Name: "VIRTUAL MACHINE",
  135. Flags: []cli.Flag{
  136. utils.VMDebugFlag,
  137. utils.VMEnableJitFlag,
  138. utils.VMForceJitFlag,
  139. utils.VMJitCacheFlag,
  140. },
  141. },
  142. {
  143. Name: "LOGGING AND DEBUGGING",
  144. Flags: append([]cli.Flag{utils.MetricsEnabledFlag}, debug.Flags...),
  145. },
  146. {
  147. Name: "EXPERIMENTAL",
  148. Flags: []cli.Flag{
  149. utils.WhisperEnabledFlag,
  150. utils.NatspecEnabledFlag,
  151. },
  152. },
  153. {
  154. Name: "MISCELLANEOUS",
  155. Flags: []cli.Flag{
  156. utils.SolcPathFlag,
  157. },
  158. },
  159. }
  160. func init() {
  161. // Override the default app help template
  162. cli.AppHelpTemplate = AppHelpTemplate
  163. // Define a one shot struct to pass to the usage template
  164. type helpData struct {
  165. App interface{}
  166. FlagGroups []flagGroup
  167. }
  168. // Override the default app help printer, but only for the global app help
  169. originalHelpPrinter := cli.HelpPrinter
  170. cli.HelpPrinter = func(w io.Writer, tmpl string, data interface{}) {
  171. if tmpl == AppHelpTemplate {
  172. // Iterate over all the flags and add any uncategorized ones
  173. categorized := make(map[string]struct{})
  174. for _, group := range AppHelpFlagGroups {
  175. for _, flag := range group.Flags {
  176. categorized[flag.String()] = struct{}{}
  177. }
  178. }
  179. uncategorized := []cli.Flag{}
  180. for _, flag := range data.(*cli.App).Flags {
  181. if _, ok := categorized[flag.String()]; !ok {
  182. uncategorized = append(uncategorized, flag)
  183. }
  184. }
  185. if len(uncategorized) > 0 {
  186. // Append all ungategorized options to the misc group
  187. miscs := len(AppHelpFlagGroups[len(AppHelpFlagGroups)-1].Flags)
  188. AppHelpFlagGroups[len(AppHelpFlagGroups)-1].Flags = append(AppHelpFlagGroups[len(AppHelpFlagGroups)-1].Flags, uncategorized...)
  189. // Make sure they are removed afterwards
  190. defer func() {
  191. AppHelpFlagGroups[len(AppHelpFlagGroups)-1].Flags = AppHelpFlagGroups[len(AppHelpFlagGroups)-1].Flags[:miscs]
  192. }()
  193. }
  194. // Render out custom usage screen
  195. originalHelpPrinter(w, tmpl, helpData{data, AppHelpFlagGroups})
  196. } else {
  197. originalHelpPrinter(w, tmpl, data)
  198. }
  199. }
  200. }