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