usage.go 9.6 KB

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