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