usage.go 8.0 KB

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