usage.go 9.5 KB

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