github.com/newbtp/btp@v0.0.0-20190709081714-e4aafa07224e/cmd/geth/usage.go (about)

     1  // Copyright 2015 The go-btpereum Authors
     2  // This file is part of go-btpereum.
     3  //
     4  // go-btpereum 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-btpereum 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-btpereum. If not, see <http://www.gnu.org/licenses/>.
    16  
    17  // Contains the gbtp command usage template and generator.
    18  
    19  package main
    20  
    21  import (
    22  	"io"
    23  	"sort"
    24  
    25  	"strings"
    26  
    27  	"github.com/btpereum/go-btpereum/cmd/utils"
    28  	"github.com/btpereum/go-btpereum/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-btpereum 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: "btpEREUM",
    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.btpStatsURLFlag,
    84  			utils.IdentityFlag,
    85  			utils.LightServFlag,
    86  			utils.LightBandwidthInFlag,
    87  			utils.LightBandwidthOutFlag,
    88  			utils.LightPeersFlag,
    89  			utils.LightKDFFlag,
    90  			utils.WhitelistFlag,
    91  		},
    92  	},
    93  	{
    94  		Name: "DEVELOPER CHAIN",
    95  		Flags: []cli.Flag{
    96  			utils.DeveloperFlag,
    97  			utils.DeveloperPeriodFlag,
    98  		},
    99  	},
   100  	{
   101  		Name: "btpASH",
   102  		Flags: []cli.Flag{
   103  			utils.btpashCacheDirFlag,
   104  			utils.btpashCachesInMemoryFlag,
   105  			utils.btpashCachesOnDiskFlag,
   106  			utils.btpashDatasetDirFlag,
   107  			utils.btpashDatasetsInMemoryFlag,
   108  			utils.btpashDatasetsOnDiskFlag,
   109  		},
   110  	},
   111  	//{
   112  	//	Name: "DASHBOARD",
   113  	//	Flags: []cli.Flag{
   114  	//		utils.DashboardEnabledFlag,
   115  	//		utils.DashboardAddrFlag,
   116  	//		utils.DashboardPortFlag,
   117  	//		utils.DashboardRefreshFlag,
   118  	//		utils.DashboardAssetsFlag,
   119  	//	},
   120  	//},
   121  	{
   122  		Name: "TRANSACTION POOL",
   123  		Flags: []cli.Flag{
   124  			utils.TxPoolLocalsFlag,
   125  			utils.TxPoolNoLocalsFlag,
   126  			utils.TxPoolJournalFlag,
   127  			utils.TxPoolRejournalFlag,
   128  			utils.TxPoolPriceLimitFlag,
   129  			utils.TxPoolPriceBumpFlag,
   130  			utils.TxPoolAccountSlotsFlag,
   131  			utils.TxPoolGlobalSlotsFlag,
   132  			utils.TxPoolAccountQueueFlag,
   133  			utils.TxPoolGlobalQueueFlag,
   134  			utils.TxPoolLifetimeFlag,
   135  		},
   136  	},
   137  	{
   138  		Name: "PERFORMANCE TUNING",
   139  		Flags: []cli.Flag{
   140  			utils.CacheFlag,
   141  			utils.CacheDatabaseFlag,
   142  			utils.CacheTrieFlag,
   143  			utils.CacheGCFlag,
   144  			utils.CacheNoPrefetchFlag,
   145  		},
   146  	},
   147  	{
   148  		Name: "ACCOUNT",
   149  		Flags: []cli.Flag{
   150  			utils.UnlockedAccountFlag,
   151  			utils.PasswordFileFlag,
   152  			utils.ExternalSignerFlag,
   153  			utils.InsecureUnlockAllowedFlag,
   154  		},
   155  	},
   156  	{
   157  		Name: "API AND CONSOLE",
   158  		Flags: []cli.Flag{
   159  			utils.IPCDisabledFlag,
   160  			utils.IPCPathFlag,
   161  			utils.RPCEnabledFlag,
   162  			utils.RPCListenAddrFlag,
   163  			utils.RPCPortFlag,
   164  			utils.RPCApiFlag,
   165  			utils.RPCGlobalGasCap,
   166  			utils.RPCCORSDomainFlag,
   167  			utils.RPCVirtualHostsFlag,
   168  			utils.WSEnabledFlag,
   169  			utils.WSListenAddrFlag,
   170  			utils.WSPortFlag,
   171  			utils.WSApiFlag,
   172  			utils.WSAllowedOriginsFlag,
   173  			utils.GraphQLEnabledFlag,
   174  			utils.GraphQLListenAddrFlag,
   175  			utils.GraphQLPortFlag,
   176  			utils.GraphQLCORSDomainFlag,
   177  			utils.GraphQLVirtualHostsFlag,
   178  			utils.JSpathFlag,
   179  			utils.ExecFlag,
   180  			utils.PreloadJSFlag,
   181  		},
   182  	},
   183  	{
   184  		Name: "NETWORKING",
   185  		Flags: []cli.Flag{
   186  			utils.BootnodesFlag,
   187  			utils.BootnodesV4Flag,
   188  			utils.BootnodesV5Flag,
   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.MinerbtperbaseFlag,
   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.MinerLegacyThreadsFlag,
   249  			utils.MinerLegacyGasTargetFlag,
   250  			utils.MinerLegacyGasPriceFlag,
   251  			utils.MinerLegacybtperbaseFlag,
   252  			utils.MinerLegacyExtraDataFlag,
   253  		},
   254  	},
   255  	{
   256  		Name: "MISC",
   257  	},
   258  }
   259  
   260  // byCategory sorts an array of flagGroup by Name in the order
   261  // defined in AppHelpFlagGroups.
   262  type byCategory []flagGroup
   263  
   264  func (a byCategory) Len() int      { return len(a) }
   265  func (a byCategory) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
   266  func (a byCategory) Less(i, j int) bool {
   267  	iCat, jCat := a[i].Name, a[j].Name
   268  	iIdx, jIdx := len(AppHelpFlagGroups), len(AppHelpFlagGroups) // ensure non categorized flags come last
   269  
   270  	for i, group := range AppHelpFlagGroups {
   271  		if iCat == group.Name {
   272  			iIdx = i
   273  		}
   274  		if jCat == group.Name {
   275  			jIdx = i
   276  		}
   277  	}
   278  
   279  	return iIdx < jIdx
   280  }
   281  
   282  func flagCategory(flag cli.Flag) string {
   283  	for _, category := range AppHelpFlagGroups {
   284  		for _, flg := range category.Flags {
   285  			if flg.GetName() == flag.GetName() {
   286  				return category.Name
   287  			}
   288  		}
   289  	}
   290  	return "MISC"
   291  }
   292  
   293  func init() {
   294  	// Override the default app help template
   295  	cli.AppHelpTemplate = AppHelpTemplate
   296  
   297  	// Define a one shot struct to pass to the usage template
   298  	type helpData struct {
   299  		App        interface{}
   300  		FlagGroups []flagGroup
   301  	}
   302  
   303  	// Override the default app help printer, but only for the global app help
   304  	originalHelpPrinter := cli.HelpPrinter
   305  	cli.HelpPrinter = func(w io.Writer, tmpl string, data interface{}) {
   306  		if tmpl == AppHelpTemplate {
   307  			// Iterate over all the flags and add any uncategorized ones
   308  			categorized := make(map[string]struct{})
   309  			for _, group := range AppHelpFlagGroups {
   310  				for _, flag := range group.Flags {
   311  					categorized[flag.String()] = struct{}{}
   312  				}
   313  			}
   314  			var uncategorized []cli.Flag
   315  			for _, flag := range data.(*cli.App).Flags {
   316  				if _, ok := categorized[flag.String()]; !ok {
   317  					if strings.HasPrefix(flag.GetName(), "dashboard") {
   318  						continue
   319  					}
   320  					uncategorized = append(uncategorized, flag)
   321  				}
   322  			}
   323  			if len(uncategorized) > 0 {
   324  				// Append all ungategorized options to the misc group
   325  				miscs := len(AppHelpFlagGroups[len(AppHelpFlagGroups)-1].Flags)
   326  				AppHelpFlagGroups[len(AppHelpFlagGroups)-1].Flags = append(AppHelpFlagGroups[len(AppHelpFlagGroups)-1].Flags, uncategorized...)
   327  
   328  				// Make sure they are removed afterwards
   329  				defer func() {
   330  					AppHelpFlagGroups[len(AppHelpFlagGroups)-1].Flags = AppHelpFlagGroups[len(AppHelpFlagGroups)-1].Flags[:miscs]
   331  				}()
   332  			}
   333  			// Render out custom usage screen
   334  			originalHelpPrinter(w, tmpl, helpData{data, AppHelpFlagGroups})
   335  		} else if tmpl == utils.CommandHelpTemplate {
   336  			// Iterate over all command specific flags and categorize them
   337  			categorized := make(map[string][]cli.Flag)
   338  			for _, flag := range data.(cli.Command).Flags {
   339  				if _, ok := categorized[flag.String()]; !ok {
   340  					categorized[flagCategory(flag)] = append(categorized[flagCategory(flag)], flag)
   341  				}
   342  			}
   343  
   344  			// sort to get a stable ordering
   345  			sorted := make([]flagGroup, 0, len(categorized))
   346  			for cat, flgs := range categorized {
   347  				sorted = append(sorted, flagGroup{cat, flgs})
   348  			}
   349  			sort.Sort(byCategory(sorted))
   350  
   351  			// add sorted array to data and render with default printer
   352  			originalHelpPrinter(w, tmpl, map[string]interface{}{
   353  				"cmd":              data,
   354  				"categorizedFlags": sorted,
   355  			})
   356  		} else {
   357  			originalHelpPrinter(w, tmpl, data)
   358  		}
   359  	}
   360  }