usage.go 7.3 KB

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