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