usage.go 8.1 KB

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