usage.go 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  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. "sort"
  21. "github.com/ethereum/go-ethereum/cmd/utils"
  22. "github.com/ethereum/go-ethereum/internal/debug"
  23. cli "gopkg.in/urfave/cli.v1"
  24. )
  25. // AppHelpTemplate is the test template for the default, global app help topic.
  26. var AppHelpTemplate = `NAME:
  27. {{.App.Name}} - {{.App.Usage}}
  28. Copyright 2013-2019 The go-ethereum Authors
  29. USAGE:
  30. {{.App.HelpName}} [options]{{if .App.Commands}} command [command options]{{end}} {{if .App.ArgsUsage}}{{.App.ArgsUsage}}{{else}}[arguments...]{{end}}
  31. {{if .App.Version}}
  32. VERSION:
  33. {{.App.Version}}
  34. {{end}}{{if len .App.Authors}}
  35. AUTHOR(S):
  36. {{range .App.Authors}}{{ . }}{{end}}
  37. {{end}}{{if .App.Commands}}
  38. COMMANDS:
  39. {{range .App.Commands}}{{join .Names ", "}}{{ "\t" }}{{.Usage}}
  40. {{end}}{{end}}{{if .FlagGroups}}
  41. {{range .FlagGroups}}{{.Name}} OPTIONS:
  42. {{range .Flags}}{{.}}
  43. {{end}}
  44. {{end}}{{end}}{{if .App.Copyright }}
  45. COPYRIGHT:
  46. {{.App.Copyright}}
  47. {{end}}
  48. `
  49. // flagGroup is a collection of flags belonging to a single topic.
  50. type flagGroup struct {
  51. Name string
  52. Flags []cli.Flag
  53. }
  54. // AppHelpFlagGroups is the application flags, grouped by functionality.
  55. var AppHelpFlagGroups = []flagGroup{
  56. {
  57. Name: "ETHEREUM",
  58. Flags: []cli.Flag{
  59. configFileFlag,
  60. utils.DataDirFlag,
  61. utils.AncientFlag,
  62. utils.KeyStoreDirFlag,
  63. utils.NoUSBFlag,
  64. utils.SmartCardDaemonPathFlag,
  65. utils.NetworkIdFlag,
  66. utils.TestnetFlag,
  67. utils.RinkebyFlag,
  68. utils.GoerliFlag,
  69. utils.SyncModeFlag,
  70. utils.ExitWhenSyncedFlag,
  71. utils.GCModeFlag,
  72. utils.EthStatsURLFlag,
  73. utils.IdentityFlag,
  74. utils.LightKDFFlag,
  75. utils.WhitelistFlag,
  76. },
  77. },
  78. {
  79. Name: "LIGHT CLIENT",
  80. Flags: []cli.Flag{
  81. utils.LightServeFlag,
  82. utils.LightIngressFlag,
  83. utils.LightEgressFlag,
  84. utils.LightMaxPeersFlag,
  85. utils.UltraLightServersFlag,
  86. utils.UltraLightFractionFlag,
  87. utils.UltraLightOnlyAnnounceFlag,
  88. },
  89. },
  90. {
  91. Name: "DEVELOPER CHAIN",
  92. Flags: []cli.Flag{
  93. utils.DeveloperFlag,
  94. utils.DeveloperPeriodFlag,
  95. },
  96. },
  97. {
  98. Name: "ETHASH",
  99. Flags: []cli.Flag{
  100. utils.EthashCacheDirFlag,
  101. utils.EthashCachesInMemoryFlag,
  102. utils.EthashCachesOnDiskFlag,
  103. utils.EthashDatasetDirFlag,
  104. utils.EthashDatasetsInMemoryFlag,
  105. utils.EthashDatasetsOnDiskFlag,
  106. },
  107. },
  108. {
  109. Name: "TRANSACTION POOL",
  110. Flags: []cli.Flag{
  111. utils.TxPoolLocalsFlag,
  112. utils.TxPoolNoLocalsFlag,
  113. utils.TxPoolJournalFlag,
  114. utils.TxPoolRejournalFlag,
  115. utils.TxPoolPriceLimitFlag,
  116. utils.TxPoolPriceBumpFlag,
  117. utils.TxPoolAccountSlotsFlag,
  118. utils.TxPoolGlobalSlotsFlag,
  119. utils.TxPoolAccountQueueFlag,
  120. utils.TxPoolGlobalQueueFlag,
  121. utils.TxPoolLifetimeFlag,
  122. },
  123. },
  124. {
  125. Name: "PERFORMANCE TUNING",
  126. Flags: []cli.Flag{
  127. utils.CacheFlag,
  128. utils.CacheDatabaseFlag,
  129. utils.CacheTrieFlag,
  130. utils.CacheGCFlag,
  131. utils.CacheNoPrefetchFlag,
  132. },
  133. },
  134. {
  135. Name: "ACCOUNT",
  136. Flags: []cli.Flag{
  137. utils.UnlockedAccountFlag,
  138. utils.PasswordFileFlag,
  139. utils.ExternalSignerFlag,
  140. utils.InsecureUnlockAllowedFlag,
  141. },
  142. },
  143. {
  144. Name: "API AND CONSOLE",
  145. Flags: []cli.Flag{
  146. utils.IPCDisabledFlag,
  147. utils.IPCPathFlag,
  148. utils.RPCEnabledFlag,
  149. utils.RPCListenAddrFlag,
  150. utils.RPCPortFlag,
  151. utils.RPCApiFlag,
  152. utils.RPCGlobalGasCap,
  153. utils.RPCCORSDomainFlag,
  154. utils.RPCVirtualHostsFlag,
  155. utils.WSEnabledFlag,
  156. utils.WSListenAddrFlag,
  157. utils.WSPortFlag,
  158. utils.WSApiFlag,
  159. utils.WSAllowedOriginsFlag,
  160. utils.GraphQLEnabledFlag,
  161. utils.GraphQLListenAddrFlag,
  162. utils.GraphQLPortFlag,
  163. utils.GraphQLCORSDomainFlag,
  164. utils.GraphQLVirtualHostsFlag,
  165. utils.JSpathFlag,
  166. utils.ExecFlag,
  167. utils.PreloadJSFlag,
  168. },
  169. },
  170. {
  171. Name: "NETWORKING",
  172. Flags: []cli.Flag{
  173. utils.BootnodesFlag,
  174. utils.BootnodesV4Flag,
  175. utils.BootnodesV5Flag,
  176. utils.ListenPortFlag,
  177. utils.MaxPeersFlag,
  178. utils.MaxPendingPeersFlag,
  179. utils.NATFlag,
  180. utils.NoDiscoverFlag,
  181. utils.DiscoveryV5Flag,
  182. utils.NetrestrictFlag,
  183. utils.NodeKeyFileFlag,
  184. utils.NodeKeyHexFlag,
  185. },
  186. },
  187. {
  188. Name: "MINER",
  189. Flags: []cli.Flag{
  190. utils.MiningEnabledFlag,
  191. utils.MinerThreadsFlag,
  192. utils.MinerNotifyFlag,
  193. utils.MinerGasPriceFlag,
  194. utils.MinerGasTargetFlag,
  195. utils.MinerGasLimitFlag,
  196. utils.MinerEtherbaseFlag,
  197. utils.MinerExtraDataFlag,
  198. utils.MinerRecommitIntervalFlag,
  199. utils.MinerNoVerfiyFlag,
  200. },
  201. },
  202. {
  203. Name: "GAS PRICE ORACLE",
  204. Flags: []cli.Flag{
  205. utils.GpoBlocksFlag,
  206. utils.GpoPercentileFlag,
  207. },
  208. },
  209. {
  210. Name: "VIRTUAL MACHINE",
  211. Flags: []cli.Flag{
  212. utils.VMEnableDebugFlag,
  213. utils.EVMInterpreterFlag,
  214. utils.EWASMInterpreterFlag,
  215. },
  216. },
  217. {
  218. Name: "LOGGING AND DEBUGGING",
  219. Flags: append([]cli.Flag{
  220. utils.FakePoWFlag,
  221. utils.NoCompactionFlag,
  222. }, debug.Flags...),
  223. },
  224. {
  225. Name: "METRICS AND STATS",
  226. Flags: metricsFlags,
  227. },
  228. {
  229. Name: "WHISPER (EXPERIMENTAL)",
  230. Flags: whisperFlags,
  231. },
  232. {
  233. Name: "DEPRECATED",
  234. Flags: []cli.Flag{
  235. utils.LightLegacyServFlag,
  236. utils.LightLegacyPeersFlag,
  237. utils.MinerLegacyThreadsFlag,
  238. utils.MinerLegacyGasTargetFlag,
  239. utils.MinerLegacyGasPriceFlag,
  240. utils.MinerLegacyEtherbaseFlag,
  241. utils.MinerLegacyExtraDataFlag,
  242. },
  243. },
  244. {
  245. Name: "MISC",
  246. },
  247. }
  248. // byCategory sorts an array of flagGroup by Name in the order
  249. // defined in AppHelpFlagGroups.
  250. type byCategory []flagGroup
  251. func (a byCategory) Len() int { return len(a) }
  252. func (a byCategory) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
  253. func (a byCategory) Less(i, j int) bool {
  254. iCat, jCat := a[i].Name, a[j].Name
  255. iIdx, jIdx := len(AppHelpFlagGroups), len(AppHelpFlagGroups) // ensure non categorized flags come last
  256. for i, group := range AppHelpFlagGroups {
  257. if iCat == group.Name {
  258. iIdx = i
  259. }
  260. if jCat == group.Name {
  261. jIdx = i
  262. }
  263. }
  264. return iIdx < jIdx
  265. }
  266. func flagCategory(flag cli.Flag) string {
  267. for _, category := range AppHelpFlagGroups {
  268. for _, flg := range category.Flags {
  269. if flg.GetName() == flag.GetName() {
  270. return category.Name
  271. }
  272. }
  273. }
  274. return "MISC"
  275. }
  276. func init() {
  277. // Override the default app help template
  278. cli.AppHelpTemplate = AppHelpTemplate
  279. // Define a one shot struct to pass to the usage template
  280. type helpData struct {
  281. App interface{}
  282. FlagGroups []flagGroup
  283. }
  284. // Override the default app help printer, but only for the global app help
  285. originalHelpPrinter := cli.HelpPrinter
  286. cli.HelpPrinter = func(w io.Writer, tmpl string, data interface{}) {
  287. if tmpl == AppHelpTemplate {
  288. // Iterate over all the flags and add any uncategorized ones
  289. categorized := make(map[string]struct{})
  290. for _, group := range AppHelpFlagGroups {
  291. for _, flag := range group.Flags {
  292. categorized[flag.String()] = struct{}{}
  293. }
  294. }
  295. var uncategorized []cli.Flag
  296. for _, flag := range data.(*cli.App).Flags {
  297. if _, ok := categorized[flag.String()]; !ok {
  298. uncategorized = append(uncategorized, flag)
  299. }
  300. }
  301. if len(uncategorized) > 0 {
  302. // Append all ungategorized options to the misc group
  303. miscs := len(AppHelpFlagGroups[len(AppHelpFlagGroups)-1].Flags)
  304. AppHelpFlagGroups[len(AppHelpFlagGroups)-1].Flags = append(AppHelpFlagGroups[len(AppHelpFlagGroups)-1].Flags, uncategorized...)
  305. // Make sure they are removed afterwards
  306. defer func() {
  307. AppHelpFlagGroups[len(AppHelpFlagGroups)-1].Flags = AppHelpFlagGroups[len(AppHelpFlagGroups)-1].Flags[:miscs]
  308. }()
  309. }
  310. // Render out custom usage screen
  311. originalHelpPrinter(w, tmpl, helpData{data, AppHelpFlagGroups})
  312. } else if tmpl == utils.CommandHelpTemplate {
  313. // Iterate over all command specific flags and categorize them
  314. categorized := make(map[string][]cli.Flag)
  315. for _, flag := range data.(cli.Command).Flags {
  316. if _, ok := categorized[flag.String()]; !ok {
  317. categorized[flagCategory(flag)] = append(categorized[flagCategory(flag)], flag)
  318. }
  319. }
  320. // sort to get a stable ordering
  321. sorted := make([]flagGroup, 0, len(categorized))
  322. for cat, flgs := range categorized {
  323. sorted = append(sorted, flagGroup{cat, flgs})
  324. }
  325. sort.Sort(byCategory(sorted))
  326. // add sorted array to data and render with default printer
  327. originalHelpPrinter(w, tmpl, map[string]interface{}{
  328. "cmd": data,
  329. "categorizedFlags": sorted,
  330. })
  331. } else {
  332. originalHelpPrinter(w, tmpl, data)
  333. }
  334. }
  335. }