github.com/waltonchain/waltonchain_gwtc_src@v1.1.4-0.20201225072101-8a298c95a819/cmd/gwtc/usage.go (about)

     1  // Copyright 2015 The go-ethereum Authors
     2  // This file is part of go-wtc.
     3  //
     4  // go-wtc 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-wtc 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-wtc. If not, see <http://www.gnu.org/licenses/>.
    16  
    17  // Contains the gwtc command usage template and generator.
    18  
    19  package main
    20  
    21  import (
    22  	"io"
    23  	"sort"
    24  
    25  	"github.com/wtc/go-wtc/cmd/utils"
    26  	"github.com/wtc/go-wtc/internal/debug"
    27  	"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-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: "WTC",
    67  		Flags: []cli.Flag{
    68  			configFileFlag,
    69  			utils.DataDirFlag,
    70  			utils.KeyStoreDirFlag,
    71  			utils.NoUSBFlag,
    72  			utils.NetworkIdFlag,
    73  			utils.TestnetFlag,
    74  			utils.RinkebyFlag,
    75  			utils.DevModeFlag,
    76  			utils.SyncModeFlag,
    77  			utils.EthStatsURLFlag,
    78  			utils.IdentityFlag,
    79  			utils.LightServFlag,
    80  			utils.LightPeersFlag,
    81  			utils.LightKDFFlag,
    82  		},
    83  	},
    84  	{
    85  		Name: "ETHASH",
    86  		Flags: []cli.Flag{
    87  			utils.EthashCacheDirFlag,
    88  			utils.EthashCachesInMemoryFlag,
    89  			utils.EthashCachesOnDiskFlag,
    90  			utils.EthashDatasetDirFlag,
    91  			utils.EthashDatasetsInMemoryFlag,
    92  			utils.EthashDatasetsOnDiskFlag,
    93  			utils.GPUPowFlag,
    94  			utils.GPUPortFlag,
    95  			utils.GPUGetFlag,
    96  		},
    97  	},
    98  	{
    99  		Name: "TRANSACTION POOL",
   100  		Flags: []cli.Flag{
   101  			utils.TxPoolNoLocalsFlag,
   102  			utils.TxPoolJournalFlag,
   103  			utils.TxPoolRejournalFlag,
   104  			utils.TxPoolPriceLimitFlag,
   105  			utils.TxPoolPriceBumpFlag,
   106  			utils.TxPoolAccountSlotsFlag,
   107  			utils.TxPoolGlobalSlotsFlag,
   108  			utils.TxPoolAccountQueueFlag,
   109  			utils.TxPoolGlobalQueueFlag,
   110  			utils.TxPoolLifetimeFlag,
   111  		},
   112  	},
   113  	{
   114  		Name: "PERFORMANCE TUNING",
   115  		Flags: []cli.Flag{
   116  			utils.CacheFlag,
   117  			utils.TrieCacheGenFlag,
   118  		},
   119  	},
   120  	{
   121  		Name: "ACCOUNT",
   122  		Flags: []cli.Flag{
   123  			utils.UnlockedAccountFlag,
   124  			utils.PasswordFileFlag,
   125  		},
   126  	},
   127  	{
   128  		Name: "API AND CONSOLE",
   129  		Flags: []cli.Flag{
   130  			utils.RPCEnabledFlag,
   131  			utils.RPCListenAddrFlag,
   132  			utils.RPCPortFlag,
   133  			utils.RPCApiFlag,
   134  			utils.WSEnabledFlag,
   135  			utils.WSListenAddrFlag,
   136  			utils.WSPortFlag,
   137  			utils.WSApiFlag,
   138  			utils.WSAllowedOriginsFlag,
   139  			utils.IPCDisabledFlag,
   140  			utils.IPCPathFlag,
   141  			utils.RPCCORSDomainFlag,
   142  			utils.JSpathFlag,
   143  			utils.ExecFlag,
   144  			utils.PreloadJSFlag,
   145  		},
   146  	},
   147  	{
   148  		Name: "NETWORKING",
   149  		Flags: []cli.Flag{
   150  			utils.BootnodesFlag,
   151  			utils.BootnodesV4Flag,
   152  			utils.BootnodesV5Flag,
   153  			utils.ListenPortFlag,
   154  			utils.MaxPeersFlag,
   155  			utils.MaxPendingPeersFlag,
   156  			utils.NATFlag,
   157  			utils.NoDiscoverFlag,
   158  			utils.DiscoveryV5Flag,
   159  			utils.NetrestrictFlag,
   160  			utils.NodeKeyFileFlag,
   161  			utils.NodeKeyHexFlag,
   162  		},
   163  	},
   164  	{
   165  		Name: "MINER",
   166  		Flags: []cli.Flag{
   167  			utils.MiningEnabledFlag,
   168  			utils.MinerThreadsFlag,
   169  			utils.EtherbaseFlag,
   170  			utils.TargetGasLimitFlag,
   171  			utils.GasPriceFlag,
   172  			utils.ExtraDataFlag,
   173  		},
   174  	},
   175  	{
   176  		Name: "GAS PRICE ORACLE",
   177  		Flags: []cli.Flag{
   178  			utils.GpoBlocksFlag,
   179  			utils.GpoPercentileFlag,
   180  		},
   181  	},
   182  	{
   183  		Name: "VIRTUAL MACHINE",
   184  		Flags: []cli.Flag{
   185  			utils.VMEnableDebugFlag,
   186  		},
   187  	},
   188  	{
   189  		Name: "LOGGING AND DEBUGGING",
   190  		Flags: append([]cli.Flag{
   191  			utils.MetricsEnabledFlag,
   192  			utils.FakePoWFlag,
   193  			utils.NoCompactionFlag,
   194  		}, debug.Flags...),
   195  	},
   196  	{
   197  		Name:  "WHISPER (EXPERIMENTAL)",
   198  		Flags: whisperFlags,
   199  	},
   200  	{
   201  		Name: "DEPRECATED",
   202  		Flags: []cli.Flag{
   203  			utils.FastSyncFlag,
   204  			utils.LightModeFlag,
   205  		},
   206  	},
   207  	{
   208  		Name: "MISC",
   209  	},
   210  }
   211  
   212  // byCategory sorts an array of flagGroup by Name in the order
   213  // defined in AppHelpFlagGroups.
   214  type byCategory []flagGroup
   215  
   216  func (a byCategory) Len() int      { return len(a) }
   217  func (a byCategory) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
   218  func (a byCategory) Less(i, j int) bool {
   219  	iCat, jCat := a[i].Name, a[j].Name
   220  	iIdx, jIdx := len(AppHelpFlagGroups), len(AppHelpFlagGroups) // ensure non categorized flags come last
   221  
   222  	for i, group := range AppHelpFlagGroups {
   223  		if iCat == group.Name {
   224  			iIdx = i
   225  		}
   226  		if jCat == group.Name {
   227  			jIdx = i
   228  		}
   229  	}
   230  
   231  	return iIdx < jIdx
   232  }
   233  
   234  func flagCategory(flag cli.Flag) string {
   235  	for _, category := range AppHelpFlagGroups {
   236  		for _, flg := range category.Flags {
   237  			if flg.GetName() == flag.GetName() {
   238  				return category.Name
   239  			}
   240  		}
   241  	}
   242  	return "MISC"
   243  }
   244  
   245  func init() {
   246  	// Override the default app help template
   247  	cli.AppHelpTemplate = AppHelpTemplate
   248  
   249  	// Define a one shot struct to pass to the usage template
   250  	type helpData struct {
   251  		App        interface{}
   252  		FlagGroups []flagGroup
   253  	}
   254  
   255  	// Override the default app help printer, but only for the global app help
   256  	originalHelpPrinter := cli.HelpPrinter
   257  	cli.HelpPrinter = func(w io.Writer, tmpl string, data interface{}) {
   258  		if tmpl == AppHelpTemplate {
   259  			// Iterate over all the flags and add any uncategorized ones
   260  			categorized := make(map[string]struct{})
   261  			for _, group := range AppHelpFlagGroups {
   262  				for _, flag := range group.Flags {
   263  					categorized[flag.String()] = struct{}{}
   264  				}
   265  			}
   266  			uncategorized := []cli.Flag{}
   267  			for _, flag := range data.(*cli.App).Flags {
   268  				if _, ok := categorized[flag.String()]; !ok {
   269  					uncategorized = append(uncategorized, flag)
   270  				}
   271  			}
   272  			if len(uncategorized) > 0 {
   273  				// Append all ungategorized options to the misc group
   274  				miscs := len(AppHelpFlagGroups[len(AppHelpFlagGroups)-1].Flags)
   275  				AppHelpFlagGroups[len(AppHelpFlagGroups)-1].Flags = append(AppHelpFlagGroups[len(AppHelpFlagGroups)-1].Flags, uncategorized...)
   276  
   277  				// Make sure they are removed afterwards
   278  				defer func() {
   279  					AppHelpFlagGroups[len(AppHelpFlagGroups)-1].Flags = AppHelpFlagGroups[len(AppHelpFlagGroups)-1].Flags[:miscs]
   280  				}()
   281  			}
   282  			// Render out custom usage screen
   283  			originalHelpPrinter(w, tmpl, helpData{data, AppHelpFlagGroups})
   284  		} else if tmpl == utils.CommandHelpTemplate {
   285  			// Iterate over all command specific flags and categorize them
   286  			categorized := make(map[string][]cli.Flag)
   287  			for _, flag := range data.(cli.Command).Flags {
   288  				if _, ok := categorized[flag.String()]; !ok {
   289  					categorized[flagCategory(flag)] = append(categorized[flagCategory(flag)], flag)
   290  				}
   291  			}
   292  
   293  			// sort to get a stable ordering
   294  			sorted := make([]flagGroup, 0, len(categorized))
   295  			for cat, flgs := range categorized {
   296  				sorted = append(sorted, flagGroup{cat, flgs})
   297  			}
   298  			sort.Sort(byCategory(sorted))
   299  
   300  			// add sorted array to data and render with default printer
   301  			originalHelpPrinter(w, tmpl, map[string]interface{}{
   302  				"cmd":              data,
   303  				"categorizedFlags": sorted,
   304  			})
   305  		} else {
   306  			originalHelpPrinter(w, tmpl, data)
   307  		}
   308  	}
   309  }