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