github.com/reapchain/go-reapchain@v0.2.15-0.20210609012950-9735c110c705/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  	"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-2017 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.KeyStoreDirFlag,
    71  			utils.NoUSBFlag,
    72  			utils.NetworkIdFlag,
    73  			utils.TestnetFlag,
    74  			utils.RinkebyFlag,
    75  			utils.OttomanFlag,
    76  			utils.ReapChainFlag,
    77  			utils.DevModeFlag,
    78  			utils.SyncModeFlag,
    79  			utils.EthStatsURLFlag,
    80  			utils.IdentityFlag,
    81  			utils.LightServFlag,
    82  			utils.LightPeersFlag,
    83  			utils.LightKDFFlag,
    84  			utils.GovernanceFlag,
    85  		},
    86  	},
    87  	{
    88  		Name: "ETHASH",
    89  		Flags: []cli.Flag{
    90  			utils.EthashCacheDirFlag,
    91  			utils.EthashCachesInMemoryFlag,
    92  			utils.EthashCachesOnDiskFlag,
    93  			utils.EthashDatasetDirFlag,
    94  			utils.EthashDatasetsInMemoryFlag,
    95  			utils.EthashDatasetsOnDiskFlag,
    96  		},
    97  	},
    98  	{
    99  		Name: "TRANSACTION POOL",
   100  		Flags: []cli.Flag{
   101  			utils.TxPoolPriceLimitFlag,
   102  			utils.TxPoolPriceBumpFlag,
   103  			utils.TxPoolAccountSlotsFlag,
   104  			utils.TxPoolGlobalSlotsFlag,
   105  			utils.TxPoolAccountQueueFlag,
   106  			utils.TxPoolGlobalQueueFlag,
   107  			utils.TxPoolLifetimeFlag,
   108  		},
   109  	},
   110  	{
   111  		Name: "PERFORMANCE TUNING",
   112  		Flags: []cli.Flag{
   113  			utils.CacheFlag,
   114  			utils.TrieCacheGenFlag,
   115  		},
   116  	},
   117  	{
   118  		Name: "ACCOUNT",
   119  		Flags: []cli.Flag{
   120  			utils.UnlockedAccountFlag,
   121  			utils.PasswordFileFlag,
   122  		},
   123  	},
   124  	{
   125  		Name: "API AND CONSOLE",
   126  		Flags: []cli.Flag{
   127  			utils.RPCEnabledFlag,
   128  			utils.RPCListenAddrFlag,
   129  			utils.RPCPortFlag,
   130  			utils.RPCApiFlag,
   131  			utils.WSEnabledFlag,
   132  			utils.WSListenAddrFlag,
   133  			utils.WSPortFlag,
   134  			utils.WSApiFlag,
   135  			utils.WSAllowedOriginsFlag,
   136  			utils.IPCDisabledFlag,
   137  			utils.IPCPathFlag,
   138  			utils.RPCCORSDomainFlag,
   139  			utils.JSpathFlag,
   140  			utils.ExecFlag,
   141  			utils.PreloadJSFlag,
   142  		},
   143  	},
   144  	{
   145  		Name: "NETWORKING",
   146  		Flags: []cli.Flag{
   147  			utils.BootnodesFlag,
   148  			utils.BootnodesV4Flag,
   149  			utils.BootnodesV5Flag,
   150  			utils.ListenPortFlag,
   151  			utils.ListenLocalIPFlag,
   152  			utils.ListenSetIPFlag,
   153  			//utils.BootnodeportFlag,
   154  			utils.MaxPeersFlag,
   155  			utils.MaxPendingPeersFlag,
   156  			utils.NATFlag,
   157  			utils.NoDiscoverFlag,
   158  			utils.DiscoveryV5Flag,
   159  			utils.NetrestrictFlag,
   160  			utils.NodeKeyFileFlag,
   161  			utils.NodeKeyHexFlag,
   162  		},
   163  	},
   164  	{
   165  		Name: "MINER",
   166  		Flags: []cli.Flag{
   167  			utils.MiningEnabledFlag,
   168  			utils.MinerThreadsFlag,
   169  			utils.EtherbaseFlag,
   170  			utils.TargetGasLimitFlag,
   171  			utils.GasPriceFlag,
   172  			utils.ExtraDataFlag,
   173  		},
   174  	},
   175  	{
   176  		Name: "GAS PRICE ORACLE",
   177  		Flags: []cli.Flag{
   178  			utils.GpoBlocksFlag,
   179  			utils.GpoPercentileFlag,
   180  		},
   181  	},
   182  	{
   183  		Name: "VIRTUAL MACHINE",
   184  		Flags: []cli.Flag{
   185  			utils.VMEnableDebugFlag,
   186  		},
   187  	},
   188  	{
   189  		Name: "LOGGING AND DEBUGGING",
   190  		Flags: append([]cli.Flag{
   191  			utils.MetricsEnabledFlag,
   192  			utils.FakePoWFlag,
   193  			utils.NoCompactionFlag,
   194  		}, debug.Flags...),
   195  	},
   196  	{
   197  		Name: "DEPRECATED",
   198  		Flags: []cli.Flag{
   199  			utils.FastSyncFlag,
   200  			utils.LightModeFlag,
   201  		},
   202  	},
   203  	{
   204  		Name: "EXPERIMENTAL",
   205  		Flags: []cli.Flag{
   206  			utils.WhisperEnabledFlag,
   207  		},
   208  	},
   209  	/* {
   210  		Name: "ISTANBUL",
   211  		Flags: []cli.Flag{
   212  			utils.IstanbulRequestTimeoutFlag,
   213  			utils.IstanbulBlockPeriodFlag,
   214  			utils.IstanbulBlockPauseTimeFlag,
   215  
   216  		},
   217  	}, */
   218  	{
   219  		Name: "PODC",
   220  		Flags: []cli.Flag{
   221  			utils.PoDCRequestTimeoutFlag,
   222  			utils.PoDCBlockPeriodFlag,
   223  			utils.PoDCBlockPauseTimeFlag,
   224  
   225  		},
   226  	},
   227  	/* {
   228  		Name: "REAPCHAIN",
   229  		Flags: []cli.Flag{
   230  			utils.IstanbulRequestTimeoutFlag,
   231  			utils.IstanbulBlockPeriodFlag,
   232  			utils.IstanbulBlockPauseTimeFlag,
   233  			utils.ListenLocalIPFlag,
   234  
   235  		},
   236  
   237  	}, */
   238  }
   239  
   240  // byCategory sorts an array of flagGroup by Name in the order
   241  // defined in AppHelpFlagGroups.
   242  type byCategory []flagGroup
   243  
   244  func (a byCategory) Len() int      { return len(a) }
   245  func (a byCategory) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
   246  func (a byCategory) Less(i, j int) bool {
   247  	iCat, jCat := a[i].Name, a[j].Name
   248  	iIdx, jIdx := len(AppHelpFlagGroups), len(AppHelpFlagGroups) // ensure non categorized flags come last
   249  
   250  	for i, group := range AppHelpFlagGroups {
   251  		if iCat == group.Name {
   252  			iIdx = i
   253  		}
   254  		if jCat == group.Name {
   255  			jIdx = i
   256  		}
   257  	}
   258  
   259  	return iIdx < jIdx
   260  }
   261  
   262  func flagCategory(flag cli.Flag) string {
   263  	for _, category := range AppHelpFlagGroups {
   264  		for _, flg := range category.Flags {
   265  			if flg.GetName() == flag.GetName() {
   266  				return category.Name
   267  			}
   268  		}
   269  	}
   270  	return "MISC"
   271  }
   272  
   273  func init() {
   274  	// Override the default app help template
   275  	cli.AppHelpTemplate = AppHelpTemplate
   276  
   277  	// Define a one shot struct to pass to the usage template
   278  	type helpData struct {
   279  		App        interface{}
   280  		FlagGroups []flagGroup
   281  	}
   282  
   283  	// Override the default app help printer, but only for the global app help
   284  	originalHelpPrinter := cli.HelpPrinter
   285  	cli.HelpPrinter = func(w io.Writer, tmpl string, data interface{}) {
   286  		if tmpl == AppHelpTemplate {
   287  			// Iterate over all the flags and add any uncategorized ones
   288  			categorized := make(map[string]struct{})
   289  			for _, group := range AppHelpFlagGroups {
   290  				for _, flag := range group.Flags {
   291  					categorized[flag.String()] = struct{}{}
   292  				}
   293  			}
   294  			uncategorized := []cli.Flag{}
   295  			for _, flag := range data.(*cli.App).Flags {
   296  				if _, ok := categorized[flag.String()]; !ok {
   297  					uncategorized = append(uncategorized, flag)
   298  				}
   299  			}
   300  			if len(uncategorized) > 0 {
   301  				// Append all ungategorized options to the misc group
   302  				miscs := len(AppHelpFlagGroups[len(AppHelpFlagGroups)-1].Flags)
   303  				AppHelpFlagGroups[len(AppHelpFlagGroups)-1].Flags = append(AppHelpFlagGroups[len(AppHelpFlagGroups)-1].Flags, uncategorized...)
   304  
   305  				// Make sure they are removed afterwards
   306  				defer func() {
   307  					AppHelpFlagGroups[len(AppHelpFlagGroups)-1].Flags = AppHelpFlagGroups[len(AppHelpFlagGroups)-1].Flags[:miscs]
   308  				}()
   309  			}
   310  			// Render out custom usage screen
   311  			originalHelpPrinter(w, tmpl, helpData{data, AppHelpFlagGroups})
   312  		} else if tmpl == utils.CommandHelpTemplate {
   313  			// Iterate over all command specific flags and categorize them
   314  			categorized := make(map[string][]cli.Flag)
   315  			for _, flag := range data.(cli.Command).Flags {
   316  				if _, ok := categorized[flag.String()]; !ok {
   317  					categorized[flagCategory(flag)] = append(categorized[flagCategory(flag)], flag)
   318  				}
   319  			}
   320  
   321  			// sort to get a stable ordering
   322  			sorted := make([]flagGroup, 0, len(categorized))
   323  			for cat, flgs := range categorized {
   324  				sorted = append(sorted, flagGroup{cat, flgs})
   325  			}
   326  			sort.Sort(byCategory(sorted))
   327  
   328  			// add sorted array to data and render with default printer
   329  			originalHelpPrinter(w, tmpl, map[string]interface{}{
   330  				"cmd":              data,
   331  				"categorizedFlags": sorted,
   332  			})
   333  		} else {
   334  			originalHelpPrinter(w, tmpl, data)
   335  		}
   336  	}
   337  }