github.com/XinFinOrg/xdcchain@v1.1.0/cmd/XDC/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 XDC command usage template and generator.
    18  
    19  package main
    20  
    21  import (
    22  	"io"
    23  	"sort"
    24  
    25  	"strings"
    26  
    27  	"github.com/ethereum/go-ethereum/cmd/utils"
    28  	"github.com/ethereum/go-ethereum/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 (c) 2018 XDCchain
    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: "XDCCHAIN",
    69  		Flags: []cli.Flag{
    70  			configFileFlag,
    71  			utils.DataDirFlag,
    72  			utils.KeyStoreDirFlag,
    73  			//utils.NoUSBFlag,
    74  			utils.NetworkIdFlag,
    75  			//utils.TestnetFlag,
    76  			//utils.RinkebyFlag,
    77  			//utils.GoerliFlag,
    78  			utils.SyncModeFlag,
    79  			utils.GCModeFlag,
    80  			utils.EthStatsURLFlag,
    81  			utils.IdentityFlag,
    82  			//utils.LightServFlag,
    83  			//utils.LightPeersFlag,
    84  			//utils.LightKDFFlag,
    85  			utils.WhitelistFlag,
    86  		},
    87  	},
    88  	//{
    89  	//	Name: "DEVELOPER CHAIN",
    90  	//	Flags: []cli.Flag{
    91  	//		utils.DeveloperFlag,
    92  	//		utils.DeveloperPeriodFlag,
    93  	//	},
    94  	//},
    95  	//{
    96  	//	Name: "ETHASH",
    97  	//	Flags: []cli.Flag{
    98  	//		utils.EthashCacheDirFlag,
    99  	//		utils.EthashCachesInMemoryFlag,
   100  	//		utils.EthashCachesOnDiskFlag,
   101  	//		utils.EthashDatasetDirFlag,
   102  	//		utils.EthashDatasetsInMemoryFlag,
   103  	//		utils.EthashDatasetsOnDiskFlag,
   104  	//	},
   105  	//},
   106  	//{
   107  	//	Name: "DASHBOARD",
   108  	//	Flags: []cli.Flag{
   109  	//		utils.DashboardEnabledFlag,
   110  	//		utils.DashboardAddrFlag,
   111  	//		utils.DashboardPortFlag,
   112  	//		utils.DashboardRefreshFlag,
   113  	//		utils.DashboardAssetsFlag,
   114  	//	},
   115  	//},
   116  	//{
   117  	//	Name: "TRANSACTION POOL",
   118  	//	Flags: []cli.Flag{
   119  	//		utils.TxPoolLocalsFlag,
   120  	//		utils.TxPoolNoLocalsFlag,
   121  	//		utils.TxPoolJournalFlag,
   122  	//		utils.TxPoolRejournalFlag,
   123  	//		utils.TxPoolPriceLimitFlag,
   124  	//		utils.TxPoolPriceBumpFlag,
   125  	//		utils.TxPoolAccountSlotsFlag,
   126  	//		utils.TxPoolGlobalSlotsFlag,
   127  	//		utils.TxPoolAccountQueueFlag,
   128  	//		utils.TxPoolGlobalQueueFlag,
   129  	//		utils.TxPoolLifetimeFlag,
   130  	//	},
   131  	//},
   132  	//{
   133  	//	Name: "PERFORMANCE TUNING",
   134  	//	Flags: []cli.Flag{
   135  	//		utils.CacheFlag,
   136  	//		utils.CacheDatabaseFlag,
   137  	//		utils.CacheTrieFlag,
   138  	//		utils.CacheGCFlag,
   139  	//		utils.TrieCacheGenFlag,
   140  	//	},
   141  	//},
   142  	{
   143  		Name: "ACCOUNT",
   144  		Flags: []cli.Flag{
   145  			utils.UnlockedAccountFlag,
   146  			utils.PasswordFileFlag,
   147  		},
   148  	},
   149  	{
   150  		Name: "API AND CONSOLE",
   151  		Flags: []cli.Flag{
   152  			utils.RPCEnabledFlag,
   153  			utils.RPCListenAddrFlag,
   154  			utils.RPCPortFlag,
   155  			utils.RPCApiFlag,
   156  			utils.RPCGlobalGasCap,
   157  			utils.WSEnabledFlag,
   158  			utils.WSListenAddrFlag,
   159  			utils.WSPortFlag,
   160  			utils.WSApiFlag,
   161  			utils.WSAllowedOriginsFlag,
   162  			utils.IPCDisabledFlag,
   163  			utils.IPCPathFlag,
   164  			utils.RPCCORSDomainFlag,
   165  			utils.RPCVirtualHostsFlag,
   166  			utils.JSpathFlag,
   167  			utils.ExecFlag,
   168  			utils.PreloadJSFlag,
   169  		},
   170  	},
   171  	{
   172  		Name: "NETWORKING",
   173  		Flags: []cli.Flag{
   174  			utils.BootnodesFlag,
   175  			utils.BootnodesV4Flag,
   176  			utils.BootnodesV5Flag,
   177  			utils.ListenPortFlag,
   178  			utils.MaxPeersFlag,
   179  			utils.MaxPendingPeersFlag,
   180  			utils.NATFlag,
   181  			utils.NoDiscoverFlag,
   182  			//utils.DiscoveryV5Flag,
   183  			//utils.NetrestrictFlag,
   184  			utils.NodeKeyFileFlag,
   185  			utils.NodeKeyHexFlag,
   186  		},
   187  	},
   188  	{
   189  		Name: "STAKER",
   190  		Flags: []cli.Flag{
   191  			utils.StakingEnabledFlag,
   192  			utils.StakerThreadsFlag,
   193  			utils.MinerNotifyFlag,
   194  			utils.MinerGasPriceFlag,
   195  			utils.MinerGasTargetFlag,
   196  			utils.MinerGasLimitFlag,
   197  			utils.MinerEtherbaseFlag,
   198  			utils.MinerExtraDataFlag,
   199  			utils.MinerRecommitIntervalFlag,
   200  			utils.MinerNoVerfiyFlag,
   201  		},
   202  	},
   203  	//{
   204  	//	Name: "GAS PRICE ORACLE",
   205  	//	Flags: []cli.Flag{
   206  	//		utils.GpoBlocksFlag,
   207  	//		utils.GpoPercentileFlag,
   208  	//	},
   209  	//},
   210  	//{
   211  	//	Name: "VIRTUAL MACHINE",
   212  	//	Flags: []cli.Flag{
   213  	//		utils.VMEnableDebugFlag,
   214  	//		utils.EVMInterpreterFlag,
   215  	//		utils.EWASMInterpreterFlag,
   216  	//	},
   217  	//},
   218  	{
   219  		Name: "LOGGING AND DEBUGGING",
   220  		Flags: append([]cli.Flag{
   221  			//utils.FakePoWFlag,
   222  			//utils.NoCompactionFlag,
   223  		}, debug.Flags...),
   224  	},
   225  	{
   226  		Name: "METRICS AND STATS",
   227  		Flags: []cli.Flag{
   228  			utils.MetricsEnabledFlag,
   229  			utils.MetricsEnableInfluxDBFlag,
   230  			utils.MetricsInfluxDBEndpointFlag,
   231  			utils.MetricsInfluxDBDatabaseFlag,
   232  			utils.MetricsInfluxDBUsernameFlag,
   233  			utils.MetricsInfluxDBPasswordFlag,
   234  			utils.MetricsInfluxDBTagsFlag,
   235  		},
   236  	},
   237  	//{
   238  	//	Name:  "WHISPER (EXPERIMENTAL)",
   239  	//	Flags: whisperFlags,
   240  	//},
   241  	{
   242  		Name: "DEPRECATED",
   243  		Flags: []cli.Flag{
   244  			utils.StakerLegacyThreadsFlag,
   245  			utils.MinerLegacyGasTargetFlag,
   246  			utils.MinerLegacyGasPriceFlag,
   247  			utils.MinerLegacyEtherbaseFlag,
   248  			utils.MinerLegacyExtraDataFlag,
   249  		},
   250  	},
   251  	{
   252  		Name: "MISC",
   253  	},
   254  }
   255  
   256  // byCategory sorts an array of flagGroup by Name in the order
   257  // defined in AppHelpFlagGroups.
   258  type byCategory []flagGroup
   259  
   260  func (a byCategory) Len() int      { return len(a) }
   261  func (a byCategory) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
   262  func (a byCategory) Less(i, j int) bool {
   263  	iCat, jCat := a[i].Name, a[j].Name
   264  	iIdx, jIdx := len(AppHelpFlagGroups), len(AppHelpFlagGroups) // ensure non categorized flags come last
   265  
   266  	for i, group := range AppHelpFlagGroups {
   267  		if iCat == group.Name {
   268  			iIdx = i
   269  		}
   270  		if jCat == group.Name {
   271  			jIdx = i
   272  		}
   273  	}
   274  
   275  	return iIdx < jIdx
   276  }
   277  
   278  func flagCategory(flag cli.Flag) string {
   279  	for _, category := range AppHelpFlagGroups {
   280  		for _, flg := range category.Flags {
   281  			if flg.GetName() == flag.GetName() {
   282  				return category.Name
   283  			}
   284  		}
   285  	}
   286  	return "MISC"
   287  }
   288  
   289  func init() {
   290  	// Override the default app help template
   291  	cli.AppHelpTemplate = AppHelpTemplate
   292  
   293  	// Define a one shot struct to pass to the usage template
   294  	type helpData struct {
   295  		App        interface{}
   296  		FlagGroups []flagGroup
   297  	}
   298  
   299  	// Override the default app help printer, but only for the global app help
   300  	originalHelpPrinter := cli.HelpPrinter
   301  	cli.HelpPrinter = func(w io.Writer, tmpl string, data interface{}) {
   302  		if tmpl == AppHelpTemplate {
   303  			// Iterate over all the flags and add any uncategorized ones
   304  			categorized := make(map[string]struct{})
   305  			for _, group := range AppHelpFlagGroups {
   306  				for _, flag := range group.Flags {
   307  					categorized[flag.String()] = struct{}{}
   308  				}
   309  			}
   310  			uncategorized := []cli.Flag{}
   311  			for _, flag := range data.(*cli.App).Flags {
   312  				if _, ok := categorized[flag.String()]; !ok {
   313  					if strings.HasPrefix(flag.GetName(), "dashboard") {
   314  						continue
   315  					}
   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  }