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