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