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