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