usage.go 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378
  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.GoerliFlag,
  67. utils.RinkebyFlag,
  68. utils.YoloV1Flag,
  69. utils.RopstenFlag,
  70. utils.SyncModeFlag,
  71. utils.ExitWhenSyncedFlag,
  72. utils.GCModeFlag,
  73. utils.TxLookupLimitFlag,
  74. utils.EthStatsURLFlag,
  75. utils.IdentityFlag,
  76. utils.LightKDFFlag,
  77. utils.WhitelistFlag,
  78. },
  79. },
  80. {
  81. Name: "LIGHT CLIENT",
  82. Flags: []cli.Flag{
  83. utils.LightServeFlag,
  84. utils.LightIngressFlag,
  85. utils.LightEgressFlag,
  86. utils.LightMaxPeersFlag,
  87. utils.UltraLightServersFlag,
  88. utils.UltraLightFractionFlag,
  89. utils.UltraLightOnlyAnnounceFlag,
  90. },
  91. },
  92. {
  93. Name: "DEVELOPER CHAIN",
  94. Flags: []cli.Flag{
  95. utils.DeveloperFlag,
  96. utils.DeveloperPeriodFlag,
  97. },
  98. },
  99. {
  100. Name: "ETHASH",
  101. Flags: []cli.Flag{
  102. utils.EthashCacheDirFlag,
  103. utils.EthashCachesInMemoryFlag,
  104. utils.EthashCachesOnDiskFlag,
  105. utils.EthashCachesLockMmapFlag,
  106. utils.EthashDatasetDirFlag,
  107. utils.EthashDatasetsInMemoryFlag,
  108. utils.EthashDatasetsOnDiskFlag,
  109. utils.EthashDatasetsLockMmapFlag,
  110. },
  111. },
  112. {
  113. Name: "TRANSACTION POOL",
  114. Flags: []cli.Flag{
  115. utils.TxPoolLocalsFlag,
  116. utils.TxPoolNoLocalsFlag,
  117. utils.TxPoolJournalFlag,
  118. utils.TxPoolRejournalFlag,
  119. utils.TxPoolPriceLimitFlag,
  120. utils.TxPoolPriceBumpFlag,
  121. utils.TxPoolAccountSlotsFlag,
  122. utils.TxPoolGlobalSlotsFlag,
  123. utils.TxPoolAccountQueueFlag,
  124. utils.TxPoolGlobalQueueFlag,
  125. utils.TxPoolLifetimeFlag,
  126. },
  127. },
  128. {
  129. Name: "PERFORMANCE TUNING",
  130. Flags: []cli.Flag{
  131. utils.CacheFlag,
  132. utils.CacheDatabaseFlag,
  133. utils.CacheTrieFlag,
  134. utils.CacheGCFlag,
  135. utils.CacheSnapshotFlag,
  136. utils.CacheNoPrefetchFlag,
  137. },
  138. },
  139. {
  140. Name: "ACCOUNT",
  141. Flags: []cli.Flag{
  142. utils.UnlockedAccountFlag,
  143. utils.PasswordFileFlag,
  144. utils.ExternalSignerFlag,
  145. utils.InsecureUnlockAllowedFlag,
  146. },
  147. },
  148. {
  149. Name: "API AND CONSOLE",
  150. Flags: []cli.Flag{
  151. utils.IPCDisabledFlag,
  152. utils.IPCPathFlag,
  153. utils.HTTPEnabledFlag,
  154. utils.HTTPListenAddrFlag,
  155. utils.HTTPPortFlag,
  156. utils.HTTPApiFlag,
  157. utils.HTTPCORSDomainFlag,
  158. utils.HTTPVirtualHostsFlag,
  159. utils.WSEnabledFlag,
  160. utils.WSListenAddrFlag,
  161. utils.WSPortFlag,
  162. utils.WSApiFlag,
  163. utils.WSAllowedOriginsFlag,
  164. utils.GraphQLEnabledFlag,
  165. utils.GraphQLListenAddrFlag,
  166. utils.GraphQLPortFlag,
  167. utils.GraphQLCORSDomainFlag,
  168. utils.GraphQLVirtualHostsFlag,
  169. utils.RPCGlobalGasCap,
  170. utils.RPCGlobalTxFeeCap,
  171. utils.JSpathFlag,
  172. utils.ExecFlag,
  173. utils.PreloadJSFlag,
  174. },
  175. },
  176. {
  177. Name: "NETWORKING",
  178. Flags: []cli.Flag{
  179. utils.BootnodesFlag,
  180. utils.LegacyBootnodesV4Flag,
  181. utils.LegacyBootnodesV5Flag,
  182. utils.DNSDiscoveryFlag,
  183. utils.ListenPortFlag,
  184. utils.MaxPeersFlag,
  185. utils.MaxPendingPeersFlag,
  186. utils.NATFlag,
  187. utils.NoDiscoverFlag,
  188. utils.DiscoveryV5Flag,
  189. utils.NetrestrictFlag,
  190. utils.NodeKeyFileFlag,
  191. utils.NodeKeyHexFlag,
  192. },
  193. },
  194. {
  195. Name: "MINER",
  196. Flags: []cli.Flag{
  197. utils.MiningEnabledFlag,
  198. utils.MinerThreadsFlag,
  199. utils.MinerNotifyFlag,
  200. utils.MinerGasPriceFlag,
  201. utils.MinerGasTargetFlag,
  202. utils.MinerGasLimitFlag,
  203. utils.MinerEtherbaseFlag,
  204. utils.MinerExtraDataFlag,
  205. utils.MinerRecommitIntervalFlag,
  206. utils.MinerNoVerfiyFlag,
  207. },
  208. },
  209. {
  210. Name: "GAS PRICE ORACLE",
  211. Flags: []cli.Flag{
  212. utils.GpoBlocksFlag,
  213. utils.GpoPercentileFlag,
  214. },
  215. },
  216. {
  217. Name: "VIRTUAL MACHINE",
  218. Flags: []cli.Flag{
  219. utils.VMEnableDebugFlag,
  220. utils.EVMInterpreterFlag,
  221. utils.EWASMInterpreterFlag,
  222. },
  223. },
  224. {
  225. Name: "LOGGING AND DEBUGGING",
  226. Flags: append([]cli.Flag{
  227. utils.FakePoWFlag,
  228. utils.NoCompactionFlag,
  229. }, debug.Flags...),
  230. },
  231. {
  232. Name: "METRICS AND STATS",
  233. Flags: metricsFlags,
  234. },
  235. {
  236. Name: "WHISPER (EXPERIMENTAL)",
  237. Flags: whisperFlags,
  238. },
  239. {
  240. Name: "ALIASED (deprecated)",
  241. Flags: append([]cli.Flag{
  242. utils.LegacyRPCEnabledFlag,
  243. utils.LegacyRPCListenAddrFlag,
  244. utils.LegacyRPCPortFlag,
  245. utils.LegacyRPCCORSDomainFlag,
  246. utils.LegacyRPCVirtualHostsFlag,
  247. utils.LegacyRPCApiFlag,
  248. utils.LegacyWSListenAddrFlag,
  249. utils.LegacyWSPortFlag,
  250. utils.LegacyWSAllowedOriginsFlag,
  251. utils.LegacyWSApiFlag,
  252. utils.LegacyGpoBlocksFlag,
  253. utils.LegacyGpoPercentileFlag,
  254. }, debug.DeprecatedFlags...),
  255. },
  256. {
  257. Name: "MISC",
  258. Flags: []cli.Flag{
  259. utils.SnapshotFlag,
  260. cli.HelpFlag,
  261. },
  262. },
  263. }
  264. // byCategory sorts an array of flagGroup by Name in the order
  265. // defined in AppHelpFlagGroups.
  266. type byCategory []flagGroup
  267. func (a byCategory) Len() int { return len(a) }
  268. func (a byCategory) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
  269. func (a byCategory) Less(i, j int) bool {
  270. iCat, jCat := a[i].Name, a[j].Name
  271. iIdx, jIdx := len(AppHelpFlagGroups), len(AppHelpFlagGroups) // ensure non categorized flags come last
  272. for i, group := range AppHelpFlagGroups {
  273. if iCat == group.Name {
  274. iIdx = i
  275. }
  276. if jCat == group.Name {
  277. jIdx = i
  278. }
  279. }
  280. return iIdx < jIdx
  281. }
  282. func flagCategory(flag cli.Flag) string {
  283. for _, category := range AppHelpFlagGroups {
  284. for _, flg := range category.Flags {
  285. if flg.GetName() == flag.GetName() {
  286. return category.Name
  287. }
  288. }
  289. }
  290. return "MISC"
  291. }
  292. func init() {
  293. // Override the default app help template
  294. cli.AppHelpTemplate = AppHelpTemplate
  295. // Define a one shot struct to pass to the usage template
  296. type helpData struct {
  297. App interface{}
  298. FlagGroups []flagGroup
  299. }
  300. // Override the default app help printer, but only for the global app help
  301. originalHelpPrinter := cli.HelpPrinter
  302. cli.HelpPrinter = func(w io.Writer, tmpl string, data interface{}) {
  303. if tmpl == AppHelpTemplate {
  304. // Iterate over all the flags and add any uncategorized ones
  305. categorized := make(map[string]struct{})
  306. for _, group := range AppHelpFlagGroups {
  307. for _, flag := range group.Flags {
  308. categorized[flag.String()] = struct{}{}
  309. }
  310. }
  311. deprecated := make(map[string]struct{})
  312. for _, flag := range utils.DeprecatedFlags {
  313. deprecated[flag.String()] = struct{}{}
  314. }
  315. // Only add uncategorized flags if they are not deprecated
  316. var uncategorized []cli.Flag
  317. for _, flag := range data.(*cli.App).Flags {
  318. if _, ok := categorized[flag.String()]; !ok {
  319. if _, ok := deprecated[flag.String()]; !ok {
  320. uncategorized = append(uncategorized, flag)
  321. }
  322. }
  323. }
  324. if len(uncategorized) > 0 {
  325. // Append all ungategorized options to the misc group
  326. miscs := len(AppHelpFlagGroups[len(AppHelpFlagGroups)-1].Flags)
  327. AppHelpFlagGroups[len(AppHelpFlagGroups)-1].Flags = append(AppHelpFlagGroups[len(AppHelpFlagGroups)-1].Flags, uncategorized...)
  328. // Make sure they are removed afterwards
  329. defer func() {
  330. AppHelpFlagGroups[len(AppHelpFlagGroups)-1].Flags = AppHelpFlagGroups[len(AppHelpFlagGroups)-1].Flags[:miscs]
  331. }()
  332. }
  333. // Render out custom usage screen
  334. originalHelpPrinter(w, tmpl, helpData{data, AppHelpFlagGroups})
  335. } else if tmpl == utils.CommandHelpTemplate {
  336. // Iterate over all command specific flags and categorize them
  337. categorized := make(map[string][]cli.Flag)
  338. for _, flag := range data.(cli.Command).Flags {
  339. if _, ok := categorized[flag.String()]; !ok {
  340. categorized[flagCategory(flag)] = append(categorized[flagCategory(flag)], flag)
  341. }
  342. }
  343. // sort to get a stable ordering
  344. sorted := make([]flagGroup, 0, len(categorized))
  345. for cat, flgs := range categorized {
  346. sorted = append(sorted, flagGroup{cat, flgs})
  347. }
  348. sort.Sort(byCategory(sorted))
  349. // add sorted array to data and render with default printer
  350. originalHelpPrinter(w, tmpl, map[string]interface{}{
  351. "cmd": data,
  352. "categorizedFlags": sorted,
  353. })
  354. } else {
  355. originalHelpPrinter(w, tmpl, data)
  356. }
  357. }
  358. }