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