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