github.com/zhiqiangxu/go-ethereum@v1.9.16-0.20210824055606-be91cfdebc48/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/zhiqiangxu/go-ethereum/cmd/utils"
    26  	"github.com/zhiqiangxu/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
    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.YoloV1Flag,
    78  			utils.RopstenFlag,
    79  			utils.SyncModeFlag,
    80  			utils.ExitWhenSyncedFlag,
    81  			utils.GCModeFlag,
    82  			utils.TxLookupLimitFlag,
    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.HTTPEnabledFlag,
   163  			utils.HTTPListenAddrFlag,
   164  			utils.HTTPPortFlag,
   165  			utils.HTTPApiFlag,
   166  			utils.HTTPCORSDomainFlag,
   167  			utils.HTTPVirtualHostsFlag,
   168  			utils.WSEnabledFlag,
   169  			utils.WSListenAddrFlag,
   170  			utils.WSPortFlag,
   171  			utils.WSApiFlag,
   172  			utils.WSAllowedOriginsFlag,
   173  			utils.GraphQLEnabledFlag,
   174  			utils.GraphQLListenAddrFlag,
   175  			utils.GraphQLPortFlag,
   176  			utils.GraphQLCORSDomainFlag,
   177  			utils.GraphQLVirtualHostsFlag,
   178  			utils.RPCGlobalGasCap,
   179  			utils.JSpathFlag,
   180  			utils.ExecFlag,
   181  			utils.PreloadJSFlag,
   182  		},
   183  	},
   184  	{
   185  		Name: "NETWORKING",
   186  		Flags: []cli.Flag{
   187  			utils.BootnodesFlag,
   188  			utils.LegacyBootnodesV4Flag,
   189  			utils.LegacyBootnodesV5Flag,
   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: "ALIASED (deprecated)",
   249  		Flags: append([]cli.Flag{
   250  			utils.LegacyRPCEnabledFlag,
   251  			utils.LegacyRPCListenAddrFlag,
   252  			utils.LegacyRPCPortFlag,
   253  			utils.LegacyRPCCORSDomainFlag,
   254  			utils.LegacyRPCVirtualHostsFlag,
   255  			utils.LegacyRPCApiFlag,
   256  			utils.LegacyWSListenAddrFlag,
   257  			utils.LegacyWSPortFlag,
   258  			utils.LegacyWSAllowedOriginsFlag,
   259  			utils.LegacyWSApiFlag,
   260  			utils.LegacyGpoBlocksFlag,
   261  			utils.LegacyGpoPercentileFlag,
   262  		}, debug.DeprecatedFlags...),
   263  	},
   264  	{
   265  		Name: "MISC",
   266  		Flags: []cli.Flag{
   267  			utils.SnapshotFlag,
   268  			cli.HelpFlag,
   269  		},
   270  	},
   271  }
   272  
   273  // byCategory sorts an array of flagGroup by Name in the order
   274  // defined in AppHelpFlagGroups.
   275  type byCategory []flagGroup
   276  
   277  func (a byCategory) Len() int      { return len(a) }
   278  func (a byCategory) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
   279  func (a byCategory) Less(i, j int) bool {
   280  	iCat, jCat := a[i].Name, a[j].Name
   281  	iIdx, jIdx := len(AppHelpFlagGroups), len(AppHelpFlagGroups) // ensure non categorized flags come last
   282  
   283  	for i, group := range AppHelpFlagGroups {
   284  		if iCat == group.Name {
   285  			iIdx = i
   286  		}
   287  		if jCat == group.Name {
   288  			jIdx = i
   289  		}
   290  	}
   291  
   292  	return iIdx < jIdx
   293  }
   294  
   295  func flagCategory(flag cli.Flag) string {
   296  	for _, category := range AppHelpFlagGroups {
   297  		for _, flg := range category.Flags {
   298  			if flg.GetName() == flag.GetName() {
   299  				return category.Name
   300  			}
   301  		}
   302  	}
   303  	return "MISC"
   304  }
   305  
   306  func init() {
   307  	// Override the default app help template
   308  	cli.AppHelpTemplate = AppHelpTemplate
   309  
   310  	// Define a one shot struct to pass to the usage template
   311  	type helpData struct {
   312  		App        interface{}
   313  		FlagGroups []flagGroup
   314  	}
   315  
   316  	// Override the default app help printer, but only for the global app help
   317  	originalHelpPrinter := cli.HelpPrinter
   318  	cli.HelpPrinter = func(w io.Writer, tmpl string, data interface{}) {
   319  		if tmpl == AppHelpTemplate {
   320  			// Iterate over all the flags and add any uncategorized ones
   321  			categorized := make(map[string]struct{})
   322  			for _, group := range AppHelpFlagGroups {
   323  				for _, flag := range group.Flags {
   324  					categorized[flag.String()] = struct{}{}
   325  				}
   326  			}
   327  			deprecated := make(map[string]struct{})
   328  			for _, flag := range utils.DeprecatedFlags {
   329  				deprecated[flag.String()] = struct{}{}
   330  			}
   331  			// Only add uncategorized flags if they are not deprecated
   332  			var uncategorized []cli.Flag
   333  			for _, flag := range data.(*cli.App).Flags {
   334  				if _, ok := categorized[flag.String()]; !ok {
   335  					if _, ok := deprecated[flag.String()]; !ok {
   336  						uncategorized = append(uncategorized, flag)
   337  					}
   338  				}
   339  			}
   340  			if len(uncategorized) > 0 {
   341  				// Append all ungategorized options to the misc group
   342  				miscs := len(AppHelpFlagGroups[len(AppHelpFlagGroups)-1].Flags)
   343  				AppHelpFlagGroups[len(AppHelpFlagGroups)-1].Flags = append(AppHelpFlagGroups[len(AppHelpFlagGroups)-1].Flags, uncategorized...)
   344  
   345  				// Make sure they are removed afterwards
   346  				defer func() {
   347  					AppHelpFlagGroups[len(AppHelpFlagGroups)-1].Flags = AppHelpFlagGroups[len(AppHelpFlagGroups)-1].Flags[:miscs]
   348  				}()
   349  			}
   350  			// Render out custom usage screen
   351  			originalHelpPrinter(w, tmpl, helpData{data, AppHelpFlagGroups})
   352  		} else if tmpl == utils.CommandHelpTemplate {
   353  			// Iterate over all command specific flags and categorize them
   354  			categorized := make(map[string][]cli.Flag)
   355  			for _, flag := range data.(cli.Command).Flags {
   356  				if _, ok := categorized[flag.String()]; !ok {
   357  					categorized[flagCategory(flag)] = append(categorized[flagCategory(flag)], flag)
   358  				}
   359  			}
   360  
   361  			// sort to get a stable ordering
   362  			sorted := make([]flagGroup, 0, len(categorized))
   363  			for cat, flgs := range categorized {
   364  				sorted = append(sorted, flagGroup{cat, flgs})
   365  			}
   366  			sort.Sort(byCategory(sorted))
   367  
   368  			// add sorted array to data and render with default printer
   369  			originalHelpPrinter(w, tmpl, map[string]interface{}{
   370  				"cmd":              data,
   371  				"categorizedFlags": sorted,
   372  			})
   373  		} else {
   374  			originalHelpPrinter(w, tmpl, data)
   375  		}
   376  	}
   377  }