github.com/Debrief-BC/go-debrief@v0.0.0-20200420203408-0c26ca968123/cmd/debrief/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/Debrief-BC/go-debrief/cmd/utils"
    26  	"github.com/Debrief-BC/go-debrief/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.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.LightLegacyServFlag,
   249  			utils.LightLegacyPeersFlag,
   250  			utils.MinerLegacyThreadsFlag,
   251  			utils.MinerLegacyGasTargetFlag,
   252  			utils.MinerLegacyGasPriceFlag,
   253  			utils.MinerLegacyEtherbaseFlag,
   254  			utils.MinerLegacyExtraDataFlag,
   255  		},
   256  	},
   257  	{
   258  		Name: "MISC",
   259  	},
   260  }
   261  
   262  // byCategory sorts an array of flagGroup by Name in the order
   263  // defined in AppHelpFlagGroups.
   264  type byCategory []flagGroup
   265  
   266  func (a byCategory) Len() int      { return len(a) }
   267  func (a byCategory) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
   268  func (a byCategory) Less(i, j int) bool {
   269  	iCat, jCat := a[i].Name, a[j].Name
   270  	iIdx, jIdx := len(AppHelpFlagGroups), len(AppHelpFlagGroups) // ensure non categorized flags come last
   271  
   272  	for i, group := range AppHelpFlagGroups {
   273  		if iCat == group.Name {
   274  			iIdx = i
   275  		}
   276  		if jCat == group.Name {
   277  			jIdx = i
   278  		}
   279  	}
   280  
   281  	return iIdx < jIdx
   282  }
   283  
   284  func flagCategory(flag cli.Flag) string {
   285  	for _, category := range AppHelpFlagGroups {
   286  		for _, flg := range category.Flags {
   287  			if flg.GetName() == flag.GetName() {
   288  				return category.Name
   289  			}
   290  		}
   291  	}
   292  	return "MISC"
   293  }
   294  
   295  func init() {
   296  	// Override the default app help template
   297  	cli.AppHelpTemplate = AppHelpTemplate
   298  
   299  	// Define a one shot struct to pass to the usage template
   300  	type helpData struct {
   301  		App        interface{}
   302  		FlagGroups []flagGroup
   303  	}
   304  
   305  	// Override the default app help printer, but only for the global app help
   306  	originalHelpPrinter := cli.HelpPrinter
   307  	cli.HelpPrinter = func(w io.Writer, tmpl string, data interface{}) {
   308  		if tmpl == AppHelpTemplate {
   309  			// Iterate over all the flags and add any uncategorized ones
   310  			categorized := make(map[string]struct{})
   311  			for _, group := range AppHelpFlagGroups {
   312  				for _, flag := range group.Flags {
   313  					categorized[flag.String()] = struct{}{}
   314  				}
   315  			}
   316  			var uncategorized []cli.Flag
   317  			for _, flag := range data.(*cli.App).Flags {
   318  				if _, ok := categorized[flag.String()]; !ok {
   319  					uncategorized = append(uncategorized, flag)
   320  				}
   321  			}
   322  			if len(uncategorized) > 0 {
   323  				// Append all ungategorized options to the misc group
   324  				miscs := len(AppHelpFlagGroups[len(AppHelpFlagGroups)-1].Flags)
   325  				AppHelpFlagGroups[len(AppHelpFlagGroups)-1].Flags = append(AppHelpFlagGroups[len(AppHelpFlagGroups)-1].Flags, uncategorized...)
   326  
   327  				// Make sure they are removed afterwards
   328  				defer func() {
   329  					AppHelpFlagGroups[len(AppHelpFlagGroups)-1].Flags = AppHelpFlagGroups[len(AppHelpFlagGroups)-1].Flags[:miscs]
   330  				}()
   331  			}
   332  			// Render out custom usage screen
   333  			originalHelpPrinter(w, tmpl, helpData{data, AppHelpFlagGroups})
   334  		} else if tmpl == utils.CommandHelpTemplate {
   335  			// Iterate over all command specific flags and categorize them
   336  			categorized := make(map[string][]cli.Flag)
   337  			for _, flag := range data.(cli.Command).Flags {
   338  				if _, ok := categorized[flag.String()]; !ok {
   339  					categorized[flagCategory(flag)] = append(categorized[flagCategory(flag)], flag)
   340  				}
   341  			}
   342  
   343  			// sort to get a stable ordering
   344  			sorted := make([]flagGroup, 0, len(categorized))
   345  			for cat, flgs := range categorized {
   346  				sorted = append(sorted, flagGroup{cat, flgs})
   347  			}
   348  			sort.Sort(byCategory(sorted))
   349  
   350  			// add sorted array to data and render with default printer
   351  			originalHelpPrinter(w, tmpl, map[string]interface{}{
   352  				"cmd":              data,
   353  				"categorizedFlags": sorted,
   354  			})
   355  		} else {
   356  			originalHelpPrinter(w, tmpl, data)
   357  		}
   358  	}
   359  }