gitlab.com/yannislg/go-pulse@v0.0.0-20210722055913-a3e24e95638d/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  	cli "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-2019 The go-ethereum Authors and BSC 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.AncientFlag,
    71  			utils.KeyStoreDirFlag,
    72  			utils.NoUSBFlag,
    73  			utils.DirectBroadcastFlag,
    74  			utils.RangeLimitFlag,
    75  			utils.SmartCardDaemonPathFlag,
    76  			utils.NetworkIdFlag,
    77  			utils.GoerliFlag,
    78  			utils.RinkebyFlag,
    79  			utils.RopstenFlag,
    80  			utils.SyncModeFlag,
    81  			utils.ExitWhenSyncedFlag,
    82  			utils.GCModeFlag,
    83  			utils.EthStatsURLFlag,
    84  			utils.IdentityFlag,
    85  			utils.LightKDFFlag,
    86  			utils.WhitelistFlag,
    87  		},
    88  	},
    89  	{
    90  		Name: "LIGHT CLIENT",
    91  		Flags: []cli.Flag{
    92  			utils.LightServeFlag,
    93  			utils.LightIngressFlag,
    94  			utils.LightEgressFlag,
    95  			utils.LightMaxPeersFlag,
    96  			utils.UltraLightServersFlag,
    97  			utils.UltraLightFractionFlag,
    98  			utils.UltraLightOnlyAnnounceFlag,
    99  		},
   100  	},
   101  	{
   102  		Name: "DEVELOPER CHAIN",
   103  		Flags: []cli.Flag{
   104  			utils.DeveloperFlag,
   105  			utils.DeveloperPeriodFlag,
   106  		},
   107  	},
   108  	{
   109  		Name: "ETHASH",
   110  		Flags: []cli.Flag{
   111  			utils.EthashCacheDirFlag,
   112  			utils.EthashCachesInMemoryFlag,
   113  			utils.EthashCachesOnDiskFlag,
   114  			utils.EthashCachesLockMmapFlag,
   115  			utils.EthashDatasetDirFlag,
   116  			utils.EthashDatasetsInMemoryFlag,
   117  			utils.EthashDatasetsOnDiskFlag,
   118  			utils.EthashDatasetsLockMmapFlag,
   119  		},
   120  	},
   121  	{
   122  		Name: "TRANSACTION POOL",
   123  		Flags: []cli.Flag{
   124  			utils.TxPoolLocalsFlag,
   125  			utils.TxPoolNoLocalsFlag,
   126  			utils.TxPoolJournalFlag,
   127  			utils.TxPoolRejournalFlag,
   128  			utils.TxPoolPriceLimitFlag,
   129  			utils.TxPoolPriceBumpFlag,
   130  			utils.TxPoolAccountSlotsFlag,
   131  			utils.TxPoolGlobalSlotsFlag,
   132  			utils.TxPoolAccountQueueFlag,
   133  			utils.TxPoolGlobalQueueFlag,
   134  			utils.TxPoolLifetimeFlag,
   135  		},
   136  	},
   137  	{
   138  		Name: "PERFORMANCE TUNING",
   139  		Flags: []cli.Flag{
   140  			utils.CacheFlag,
   141  			utils.CacheDatabaseFlag,
   142  			utils.CacheTrieFlag,
   143  			utils.CacheGCFlag,
   144  			utils.CacheSnapshotFlag,
   145  			utils.CacheNoPrefetchFlag,
   146  		},
   147  	},
   148  	{
   149  		Name: "ACCOUNT",
   150  		Flags: []cli.Flag{
   151  			utils.UnlockedAccountFlag,
   152  			utils.PasswordFileFlag,
   153  			utils.ExternalSignerFlag,
   154  			utils.InsecureUnlockAllowedFlag,
   155  		},
   156  	},
   157  	{
   158  		Name: "API AND CONSOLE",
   159  		Flags: []cli.Flag{
   160  			utils.IPCDisabledFlag,
   161  			utils.IPCPathFlag,
   162  			utils.RPCEnabledFlag,
   163  			utils.RPCListenAddrFlag,
   164  			utils.RPCPortFlag,
   165  			utils.RPCApiFlag,
   166  			utils.RPCGlobalGasCap,
   167  			utils.RPCCORSDomainFlag,
   168  			utils.RPCVirtualHostsFlag,
   169  			utils.WSEnabledFlag,
   170  			utils.WSListenAddrFlag,
   171  			utils.WSPortFlag,
   172  			utils.WSApiFlag,
   173  			utils.WSAllowedOriginsFlag,
   174  			utils.GraphQLEnabledFlag,
   175  			utils.GraphQLListenAddrFlag,
   176  			utils.GraphQLPortFlag,
   177  			utils.GraphQLCORSDomainFlag,
   178  			utils.GraphQLVirtualHostsFlag,
   179  			utils.JSpathFlag,
   180  			utils.ExecFlag,
   181  			utils.PreloadJSFlag,
   182  		},
   183  	},
   184  	{
   185  		Name: "NETWORKING",
   186  		Flags: []cli.Flag{
   187  			utils.BootnodesFlag,
   188  			utils.BootnodesV4Flag,
   189  			utils.BootnodesV5Flag,
   190  			utils.DNSDiscoveryFlag,
   191  			utils.ListenPortFlag,
   192  			utils.MaxPeersFlag,
   193  			utils.MaxPendingPeersFlag,
   194  			utils.NATFlag,
   195  			utils.NoDiscoverFlag,
   196  			utils.DiscoveryV5Flag,
   197  			utils.NetrestrictFlag,
   198  			utils.NodeKeyFileFlag,
   199  			utils.NodeKeyHexFlag,
   200  		},
   201  	},
   202  	{
   203  		Name: "MINER",
   204  		Flags: []cli.Flag{
   205  			utils.MiningEnabledFlag,
   206  			utils.MinerThreadsFlag,
   207  			utils.MinerNotifyFlag,
   208  			utils.MinerGasPriceFlag,
   209  			utils.MinerGasTargetFlag,
   210  			utils.MinerGasLimitFlag,
   211  			utils.MinerEtherbaseFlag,
   212  			utils.MinerExtraDataFlag,
   213  			utils.MinerRecommitIntervalFlag,
   214  			utils.MinerNoVerfiyFlag,
   215  		},
   216  	},
   217  	{
   218  		Name: "GAS PRICE ORACLE",
   219  		Flags: []cli.Flag{
   220  			utils.GpoBlocksFlag,
   221  			utils.GpoPercentileFlag,
   222  		},
   223  	},
   224  	{
   225  		Name: "VIRTUAL MACHINE",
   226  		Flags: []cli.Flag{
   227  			utils.VMEnableDebugFlag,
   228  			utils.EVMInterpreterFlag,
   229  			utils.EWASMInterpreterFlag,
   230  		},
   231  	},
   232  	{
   233  		Name: "LOGGING AND DEBUGGING",
   234  		Flags: append([]cli.Flag{
   235  			utils.FakePoWFlag,
   236  			utils.NoCompactionFlag,
   237  		}, debug.Flags...),
   238  	},
   239  	{
   240  		Name:  "METRICS AND STATS",
   241  		Flags: metricsFlags,
   242  	},
   243  	{
   244  		Name:  "WHISPER (EXPERIMENTAL)",
   245  		Flags: whisperFlags,
   246  	},
   247  	{
   248  		Name: "DEPRECATED",
   249  		Flags: []cli.Flag{
   250  			utils.LegacyTestnetFlag,
   251  			utils.LightLegacyServFlag,
   252  			utils.LightLegacyPeersFlag,
   253  			utils.MinerLegacyThreadsFlag,
   254  			utils.MinerLegacyGasTargetFlag,
   255  			utils.MinerLegacyGasPriceFlag,
   256  			utils.MinerLegacyEtherbaseFlag,
   257  			utils.MinerLegacyExtraDataFlag,
   258  		},
   259  	},
   260  	{
   261  		Name: "MISC",
   262  	},
   263  }
   264  
   265  // byCategory sorts an array of flagGroup by Name in the order
   266  // defined in AppHelpFlagGroups.
   267  type byCategory []flagGroup
   268  
   269  func (a byCategory) Len() int      { return len(a) }
   270  func (a byCategory) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
   271  func (a byCategory) Less(i, j int) bool {
   272  	iCat, jCat := a[i].Name, a[j].Name
   273  	iIdx, jIdx := len(AppHelpFlagGroups), len(AppHelpFlagGroups) // ensure non categorized flags come last
   274  
   275  	for i, group := range AppHelpFlagGroups {
   276  		if iCat == group.Name {
   277  			iIdx = i
   278  		}
   279  		if jCat == group.Name {
   280  			jIdx = i
   281  		}
   282  	}
   283  
   284  	return iIdx < jIdx
   285  }
   286  
   287  func flagCategory(flag cli.Flag) string {
   288  	for _, category := range AppHelpFlagGroups {
   289  		for _, flg := range category.Flags {
   290  			if flg.GetName() == flag.GetName() {
   291  				return category.Name
   292  			}
   293  		}
   294  	}
   295  	return "MISC"
   296  }
   297  
   298  func init() {
   299  	// Override the default app help template
   300  	cli.AppHelpTemplate = AppHelpTemplate
   301  
   302  	// Define a one shot struct to pass to the usage template
   303  	type helpData struct {
   304  		App        interface{}
   305  		FlagGroups []flagGroup
   306  	}
   307  
   308  	// Override the default app help printer, but only for the global app help
   309  	originalHelpPrinter := cli.HelpPrinter
   310  	cli.HelpPrinter = func(w io.Writer, tmpl string, data interface{}) {
   311  		if tmpl == AppHelpTemplate {
   312  			// Iterate over all the flags and add any uncategorized ones
   313  			categorized := make(map[string]struct{})
   314  			for _, group := range AppHelpFlagGroups {
   315  				for _, flag := range group.Flags {
   316  					categorized[flag.String()] = struct{}{}
   317  				}
   318  			}
   319  			var uncategorized []cli.Flag
   320  			for _, flag := range data.(*cli.App).Flags {
   321  				if _, ok := categorized[flag.String()]; !ok {
   322  					uncategorized = append(uncategorized, flag)
   323  				}
   324  			}
   325  			if len(uncategorized) > 0 {
   326  				// Append all ungategorized options to the misc group
   327  				miscs := len(AppHelpFlagGroups[len(AppHelpFlagGroups)-1].Flags)
   328  				AppHelpFlagGroups[len(AppHelpFlagGroups)-1].Flags = append(AppHelpFlagGroups[len(AppHelpFlagGroups)-1].Flags, uncategorized...)
   329  
   330  				// Make sure they are removed afterwards
   331  				defer func() {
   332  					AppHelpFlagGroups[len(AppHelpFlagGroups)-1].Flags = AppHelpFlagGroups[len(AppHelpFlagGroups)-1].Flags[:miscs]
   333  				}()
   334  			}
   335  			// Render out custom usage screen
   336  			originalHelpPrinter(w, tmpl, helpData{data, AppHelpFlagGroups})
   337  		} else if tmpl == utils.CommandHelpTemplate {
   338  			// Iterate over all command specific flags and categorize them
   339  			categorized := make(map[string][]cli.Flag)
   340  			for _, flag := range data.(cli.Command).Flags {
   341  				if _, ok := categorized[flag.String()]; !ok {
   342  					categorized[flagCategory(flag)] = append(categorized[flagCategory(flag)], flag)
   343  				}
   344  			}
   345  
   346  			// sort to get a stable ordering
   347  			sorted := make([]flagGroup, 0, len(categorized))
   348  			for cat, flgs := range categorized {
   349  				sorted = append(sorted, flagGroup{cat, flgs})
   350  			}
   351  			sort.Sort(byCategory(sorted))
   352  
   353  			// add sorted array to data and render with default printer
   354  			originalHelpPrinter(w, tmpl, map[string]interface{}{
   355  				"cmd":              data,
   356  				"categorizedFlags": sorted,
   357  			})
   358  		} else {
   359  			originalHelpPrinter(w, tmpl, data)
   360  		}
   361  	}
   362  }