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