github.com/SmartMeshFoundation/Spectrum@v0.0.0-20220621030607-452a266fee1e/cmd/smc/usage.go (about)

     1  // Copyright 2015 The Spectrum Authors
     2  // This file is part of Spectrum.
     3  //
     4  // Spectrum 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  // Spectrum 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 Spectrum. 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/SmartMeshFoundation/Spectrum/cmd/utils"
    26  	"github.com/SmartMeshFoundation/Spectrum/internal/debug"
    27  	"gopkg.in/urfave/cli.v1"
    28  	"strings"
    29  )
    30  
    31  // AppHelpTemplate is the test template for the default, global app help topic.
    32  var AppHelpTemplate = `NAME:
    33     {{.App.Name}} - {{.App.Usage}}
    34  
    35     Copyright 2017-2019 The Spectrum Authors
    36  
    37  USAGE:
    38     {{.App.HelpName}} [options]{{if .App.Commands}} command [command options]{{end}} {{if .App.ArgsUsage}}{{.App.ArgsUsage}}{{else}}[arguments...]{{end}}
    39     {{if .App.Version}}
    40  VERSION:
    41     {{.App.Version}}
    42     {{end}}{{if len .App.Authors}}
    43  AUTHOR(S):
    44     {{range .App.Authors}}{{ . }}{{end}}
    45     {{end}}{{if .App.Commands}}
    46  COMMANDS:
    47     {{range .App.Commands}}{{join .Names ", "}}{{ "\t" }}{{.Usage}}
    48     {{end}}{{end}}{{if .FlagGroups}}
    49  {{range .FlagGroups}}{{.Name}} OPTIONS:
    50    {{range .Flags}}{{.}}
    51    {{end}}
    52  {{end}}{{end}}{{if .App.Copyright }}
    53  COPYRIGHT:
    54     {{.App.Copyright}}
    55     {{end}}
    56  `
    57  
    58  // flagGroup is a collection of flags belonging to a single topic.
    59  type flagGroup struct {
    60  	Name  string
    61  	Flags []cli.Flag
    62  }
    63  
    64  // AppHelpFlagGroups is the application flags, grouped by functionality.
    65  var AppHelpFlagGroups = []flagGroup{
    66  	{
    67  		Name: "SPECTRUM",
    68  		Flags: []cli.Flag{
    69  			utils.DataDirFlag,
    70  			utils.KeyStoreDirFlag,
    71  			utils.TestnetFlag,
    72  			utils.DevnetFlag,
    73  			utils.DevnetResetFlag,
    74  			utils.DevnetMasterFlag,
    75  			utils.EthStatsURLFlag,
    76  			/*
    77  				utils.DeveloperFlag,
    78  				utils.NetworkIdFlag,
    79  				configFileFlag,
    80  				utils.IdentityFlag,
    81  				utils.NoUSBFlag,
    82  				utils.SyncModeFlag,
    83  				utils.LightServFlag,
    84  				utils.LightPeersFlag,
    85  				utils.LightKDFFlag,
    86  			*/
    87  		},
    88  	},
    89  	/* add by liangc : disable
    90  	{Name: "DEVELOPER CHAIN",
    91  		Flags: []cli.Flag{
    92  			utils.DeveloperFlag,
    93  			utils.DeveloperPeriodFlag,
    94  		},
    95  	},
    96  		{
    97  			Name: "ETHASH",
    98  			Flags: []cli.Flag{
    99  				utils.EthashCacheDirFlag,
   100  				utils.EthashCachesInMemoryFlag,
   101  				utils.EthashCachesOnDiskFlag,
   102  				utils.EthashDatasetDirFlag,
   103  				utils.EthashDatasetsInMemoryFlag,
   104  				utils.EthashDatasetsOnDiskFlag,
   105  			},
   106  		},*/
   107  	//{
   108  	//	Name: "DASHBOARD",
   109  	//	Flags: []cli.Flag{
   110  	//		utils.DashboardEnabledFlag,
   111  	//		utils.DashboardAddrFlag,
   112  	//		utils.DashboardPortFlag,
   113  	//		utils.DashboardRefreshFlag,
   114  	//		utils.DashboardAssetsFlag,
   115  	//	},
   116  	//},
   117  	{
   118  		Name: "TRANSACTION POOL",
   119  		Flags: []cli.Flag{
   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.TrieCacheGenFlag,
   137  		},
   138  	},
   139  	{
   140  		Name: "ACCOUNT",
   141  		Flags: []cli.Flag{
   142  			utils.UnlockedAccountFlag,
   143  			utils.PasswordFileFlag,
   144  		},
   145  	},
   146  	{
   147  		Name: "API AND CONSOLE",
   148  		Flags: []cli.Flag{
   149  			utils.RPCEnabledFlag,
   150  			utils.RPCListenAddrFlag,
   151  			utils.RPCPortFlag,
   152  			utils.RPCApiFlag,
   153  			utils.WSEnabledFlag,
   154  			utils.WSListenAddrFlag,
   155  			utils.WSPortFlag,
   156  			utils.WSApiFlag,
   157  			utils.WSAllowedOriginsFlag,
   158  			utils.IPCDisabledFlag,
   159  			utils.IPCPathFlag,
   160  			utils.RPCCORSDomainFlag,
   161  			utils.JSpathFlag,
   162  			utils.ExecFlag,
   163  			utils.PreloadJSFlag,
   164  		},
   165  	},
   166  	{
   167  		Name: "NETWORKING",
   168  		Flags: []cli.Flag{
   169  			utils.BootnodesFlag,
   170  			utils.ListenPortFlag,
   171  			utils.MaxPeersFlag,
   172  			utils.MaxPendingPeersFlag,
   173  			utils.NATFlag,
   174  			utils.NoDiscoverFlag,
   175  			utils.NetrestrictFlag,
   176  			/* add by liangc : disable
   177  			utils.DiscoveryV5Flag,
   178  			utils.BootnodesV4Flag,
   179  			utils.BootnodesV5Flag,
   180  			utils.NodeKeyFileFlag,
   181  			utils.NodeKeyHexFlag,
   182  			*/
   183  		},
   184  	},
   185  	/* add by liangc : disable
   186  	{
   187  		Name: "MINER",
   188  		Flags: []cli.Flag{
   189  			utils.MiningEnabledFlag,
   190  			utils.MinerThreadsFlag,
   191  			utils.EtherbaseFlag,
   192  			utils.TargetGasLimitFlag,
   193  			utils.GasPriceFlag,
   194  			utils.ExtraDataFlag,
   195  		},
   196  	},
   197  	*/
   198  	{
   199  		Name: "GAS PRICE ORACLE",
   200  		Flags: []cli.Flag{
   201  			utils.GpoBlocksFlag,
   202  			utils.GpoPercentileFlag,
   203  		},
   204  	},
   205  	{
   206  		Name: "VIRTUAL MACHINE",
   207  		Flags: []cli.Flag{
   208  			utils.VMEnableDebugFlag,
   209  		},
   210  	},
   211  	{
   212  		Name: "LOGGING AND DEBUGGING",
   213  		Flags: append([]cli.Flag{
   214  			utils.MetricsEnabledFlag,
   215  			//utils.FakePoWFlag,
   216  			utils.NoCompactionFlag,
   217  		}, debug.Flags...),
   218  	},
   219  	/* add by liangc : disable
   220  	{
   221  		Name:  "WHISPER (EXPERIMENTAL)",
   222  		Flags: whisperFlags,
   223  	},
   224  	{
   225  		Name: "DEPRECATED",
   226  		Flags: []cli.Flag{
   227  			utils.FastSyncFlag,
   228  			utils.LightModeFlag,
   229  		},
   230  	},*/
   231  	{
   232  		Name: "MISC",
   233  	},
   234  }
   235  
   236  // byCategory sorts an array of flagGroup by Name in the order
   237  // defined in AppHelpFlagGroups.
   238  type byCategory []flagGroup
   239  
   240  func (a byCategory) Len() int      { return len(a) }
   241  func (a byCategory) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
   242  func (a byCategory) Less(i, j int) bool {
   243  	iCat, jCat := a[i].Name, a[j].Name
   244  	iIdx, jIdx := len(AppHelpFlagGroups), len(AppHelpFlagGroups) // ensure non categorized flags come last
   245  
   246  	for i, group := range AppHelpFlagGroups {
   247  		if iCat == group.Name {
   248  			iIdx = i
   249  		}
   250  		if jCat == group.Name {
   251  			jIdx = i
   252  		}
   253  	}
   254  
   255  	return iIdx < jIdx
   256  }
   257  
   258  func flagCategory(flag cli.Flag) string {
   259  	for _, category := range AppHelpFlagGroups {
   260  		for _, flg := range category.Flags {
   261  			if flg.GetName() == flag.GetName() {
   262  				return category.Name
   263  			}
   264  		}
   265  	}
   266  	return "MISC"
   267  }
   268  
   269  func init() {
   270  	// Override the default app help template
   271  	cli.AppHelpTemplate = AppHelpTemplate
   272  
   273  	// Define a one shot struct to pass to the usage template
   274  	type helpData struct {
   275  		App        interface{}
   276  		FlagGroups []flagGroup
   277  	}
   278  
   279  	// Override the default app help printer, but only for the global app help
   280  	originalHelpPrinter := cli.HelpPrinter
   281  	cli.HelpPrinter = func(w io.Writer, tmpl string, data interface{}) {
   282  		if tmpl == AppHelpTemplate {
   283  			// Iterate over all the flags and add any uncategorized ones
   284  			categorized := make(map[string]struct{})
   285  			for _, group := range AppHelpFlagGroups {
   286  				for _, flag := range group.Flags {
   287  					categorized[flag.String()] = struct{}{}
   288  				}
   289  			}
   290  			uncategorized := []cli.Flag{}
   291  			for _, flag := range data.(*cli.App).Flags {
   292  				if _, ok := categorized[flag.String()]; !ok {
   293  					if strings.HasPrefix(flag.GetName(), "dashboard") {
   294  						continue
   295  					}
   296  					uncategorized = append(uncategorized, flag)
   297  				}
   298  			}
   299  			if len(uncategorized) > 0 {
   300  				// Append all ungategorized options to the misc group
   301  				miscs := len(AppHelpFlagGroups[len(AppHelpFlagGroups)-1].Flags)
   302  				AppHelpFlagGroups[len(AppHelpFlagGroups)-1].Flags = append(AppHelpFlagGroups[len(AppHelpFlagGroups)-1].Flags, uncategorized...)
   303  
   304  				// Make sure they are removed afterwards
   305  				defer func() {
   306  					AppHelpFlagGroups[len(AppHelpFlagGroups)-1].Flags = AppHelpFlagGroups[len(AppHelpFlagGroups)-1].Flags[:miscs]
   307  				}()
   308  			}
   309  			// Render out custom usage screen
   310  			originalHelpPrinter(w, tmpl, helpData{data, AppHelpFlagGroups})
   311  		} else if tmpl == utils.CommandHelpTemplate {
   312  			// Iterate over all command specific flags and categorize them
   313  			categorized := make(map[string][]cli.Flag)
   314  			for _, flag := range data.(cli.Command).Flags {
   315  				if _, ok := categorized[flag.String()]; !ok {
   316  					categorized[flagCategory(flag)] = append(categorized[flagCategory(flag)], flag)
   317  				}
   318  			}
   319  
   320  			// sort to get a stable ordering
   321  			sorted := make([]flagGroup, 0, len(categorized))
   322  			for cat, flgs := range categorized {
   323  				sorted = append(sorted, flagGroup{cat, flgs})
   324  			}
   325  			sort.Sort(byCategory(sorted))
   326  
   327  			// add sorted array to data and render with default printer
   328  			originalHelpPrinter(w, tmpl, map[string]interface{}{
   329  				"cmd":              data,
   330  				"categorizedFlags": sorted,
   331  			})
   332  		} else {
   333  			originalHelpPrinter(w, tmpl, data)
   334  		}
   335  	}
   336  }