github.com/neatlab/neatio@v1.7.3-0.20220425043230-d903e92fcc75/chain/neatio/usage.go (about)

     1  package main
     2  
     3  import (
     4  	"io"
     5  	"sort"
     6  
     7  	"github.com/neatlab/neatio/internal/debug"
     8  	"github.com/neatlab/neatio/utilities/utils"
     9  	"gopkg.in/urfave/cli.v1"
    10  )
    11  
    12  var AppHelpTemplate = `NAME:
    13     {{.App.Name}} - {{.App.Usage}}
    14  
    15     Copyright 2022 Neatio Developers
    16  
    17  USAGE:
    18     {{.App.HelpName}} [options]{{if .App.Commands}} command [command options]{{end}} {{if .App.ArgsUsage}}{{.App.ArgsUsage}}{{else}}[arguments...]{{end}}
    19     {{if .App.Version}}
    20  VERSION:
    21     {{.App.Version}}
    22     {{end}}{{if len .App.Authors}}
    23  AUTHOR(S):
    24     {{range .App.Authors}}{{ . }}{{end}}
    25     {{end}}{{if .App.Commands}}
    26  COMMANDS:
    27     {{range .App.Commands}}{{join .Names ", "}}{{ "\t" }}{{.Usage}}
    28     {{end}}{{end}}{{if .FlagGroups}}
    29  {{range .FlagGroups}}{{.Name}} OPTIONS:
    30    {{range .Flags}}{{.}}
    31    {{end}}
    32  {{end}}{{end}}{{if .App.Copyright }}
    33  COPYRIGHT:
    34     {{.App.Copyright}}
    35     {{end}}
    36  `
    37  
    38  type flagGroup struct {
    39  	Name  string
    40  	Flags []cli.Flag
    41  }
    42  
    43  var AppHelpFlagGroups = []flagGroup{
    44  	{
    45  		Name: "NEATIO",
    46  		Flags: []cli.Flag{
    47  			configFileFlag,
    48  			utils.DataDirFlag,
    49  			utils.KeyStoreDirFlag,
    50  			utils.NoUSBFlag,
    51  			utils.NetworkIdFlag,
    52  			utils.TestnetFlag,
    53  			utils.SyncModeFlag,
    54  			utils.GCModeFlag,
    55  			utils.EthStatsURLFlag,
    56  			utils.IdentityFlag,
    57  		},
    58  	},
    59  	{
    60  		Name: "TRANSACTION POOL",
    61  		Flags: []cli.Flag{
    62  			utils.TxPoolNoLocalsFlag,
    63  			utils.TxPoolJournalFlag,
    64  			utils.TxPoolRejournalFlag,
    65  			utils.TxPoolPriceLimitFlag,
    66  			utils.TxPoolPriceBumpFlag,
    67  			utils.TxPoolAccountSlotsFlag,
    68  			utils.TxPoolGlobalSlotsFlag,
    69  			utils.TxPoolAccountQueueFlag,
    70  			utils.TxPoolGlobalQueueFlag,
    71  			utils.TxPoolLifetimeFlag,
    72  		},
    73  	},
    74  	{
    75  		Name: "PERFORMANCE TUNING",
    76  		Flags: []cli.Flag{
    77  			utils.CacheFlag,
    78  			utils.CacheDatabaseFlag,
    79  			utils.CacheTrieFlag,
    80  			utils.CacheGCFlag,
    81  		},
    82  	},
    83  	{
    84  		Name: "ACCOUNT",
    85  		Flags: []cli.Flag{
    86  			utils.UnlockedAccountFlag,
    87  			utils.PasswordFileFlag,
    88  		},
    89  	},
    90  	{
    91  		Name: "API AND CONSOLE",
    92  		Flags: []cli.Flag{
    93  			utils.RPCEnabledFlag,
    94  			utils.RPCListenAddrFlag,
    95  			utils.RPCPortFlag,
    96  			utils.RPCApiFlag,
    97  			utils.WSEnabledFlag,
    98  			utils.WSListenAddrFlag,
    99  			utils.WSPortFlag,
   100  			utils.WSApiFlag,
   101  			utils.WSAllowedOriginsFlag,
   102  			utils.IPCDisabledFlag,
   103  			utils.IPCPathFlag,
   104  			utils.RPCCORSDomainFlag,
   105  			utils.RPCVirtualHostsFlag,
   106  			utils.JSpathFlag,
   107  			utils.ExecFlag,
   108  			utils.PreloadJSFlag,
   109  		},
   110  	},
   111  	{
   112  		Name: "NETWORKING",
   113  		Flags: []cli.Flag{
   114  			utils.BootnodesFlag,
   115  			utils.BootnodesV4Flag,
   116  			utils.BootnodesV5Flag,
   117  			utils.ListenPortFlag,
   118  			utils.MaxPeersFlag,
   119  			utils.MaxPendingPeersFlag,
   120  			utils.NATFlag,
   121  			utils.NoDiscoverFlag,
   122  			utils.DiscoveryV5Flag,
   123  			utils.NetrestrictFlag,
   124  			utils.NodeKeyFileFlag,
   125  			utils.NodeKeyHexFlag,
   126  		},
   127  	},
   128  	{
   129  		Name: "MINER",
   130  		Flags: []cli.Flag{
   131  			utils.MiningEnabledFlag,
   132  			utils.MinerThreadsFlag,
   133  			utils.MinerGasPriceFlag,
   134  			utils.MinerGasTargetFlag,
   135  			utils.MinerGasLimitFlag,
   136  			utils.MinerCoinbaseFlag,
   137  			utils.ExtraDataFlag,
   138  		},
   139  	},
   140  	{
   141  		Name: "GAS PRICE ORACLE",
   142  		Flags: []cli.Flag{
   143  			utils.GpoBlocksFlag,
   144  			utils.GpoPercentileFlag,
   145  		},
   146  	},
   147  	{
   148  		Name: "VIRTUAL MACHINE",
   149  		Flags: []cli.Flag{
   150  			utils.VMEnableDebugFlag,
   151  		},
   152  	},
   153  	{
   154  		Name: "LOGGING AND DEBUGGING",
   155  		Flags: append([]cli.Flag{
   156  			utils.MetricsEnabledFlag,
   157  			utils.NoCompactionFlag,
   158  		}, debug.Flags...),
   159  	},
   160  	{
   161  		Name: "DEPRECATED",
   162  		Flags: []cli.Flag{
   163  			utils.FastSyncFlag,
   164  		},
   165  	},
   166  }
   167  
   168  type byCategory []flagGroup
   169  
   170  func (a byCategory) Len() int      { return len(a) }
   171  func (a byCategory) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
   172  func (a byCategory) Less(i, j int) bool {
   173  	iCat, jCat := a[i].Name, a[j].Name
   174  	iIdx, jIdx := len(AppHelpFlagGroups), len(AppHelpFlagGroups)
   175  
   176  	for i, group := range AppHelpFlagGroups {
   177  		if iCat == group.Name {
   178  			iIdx = i
   179  		}
   180  		if jCat == group.Name {
   181  			jIdx = i
   182  		}
   183  	}
   184  
   185  	return iIdx < jIdx
   186  }
   187  
   188  func flagCategory(flag cli.Flag) string {
   189  	for _, category := range AppHelpFlagGroups {
   190  		for _, flg := range category.Flags {
   191  			if flg.GetName() == flag.GetName() {
   192  				return category.Name
   193  			}
   194  		}
   195  	}
   196  	return "MISC"
   197  }
   198  
   199  func init() {
   200  
   201  	cli.AppHelpTemplate = AppHelpTemplate
   202  
   203  	type helpData struct {
   204  		App        interface{}
   205  		FlagGroups []flagGroup
   206  	}
   207  
   208  	originalHelpPrinter := cli.HelpPrinter
   209  	cli.HelpPrinter = func(w io.Writer, tmpl string, data interface{}) {
   210  		if tmpl == AppHelpTemplate {
   211  
   212  			categorized := make(map[string]struct{})
   213  			for _, group := range AppHelpFlagGroups {
   214  				for _, flag := range group.Flags {
   215  					categorized[flag.String()] = struct{}{}
   216  				}
   217  			}
   218  			uncategorized := []cli.Flag{}
   219  			for _, flag := range data.(*cli.App).Flags {
   220  				if _, ok := categorized[flag.String()]; !ok {
   221  					uncategorized = append(uncategorized, flag)
   222  				}
   223  			}
   224  			if len(uncategorized) > 0 {
   225  
   226  				miscs := len(AppHelpFlagGroups[len(AppHelpFlagGroups)-1].Flags)
   227  				AppHelpFlagGroups[len(AppHelpFlagGroups)-1].Flags = append(AppHelpFlagGroups[len(AppHelpFlagGroups)-1].Flags, uncategorized...)
   228  
   229  				defer func() {
   230  					AppHelpFlagGroups[len(AppHelpFlagGroups)-1].Flags = AppHelpFlagGroups[len(AppHelpFlagGroups)-1].Flags[:miscs]
   231  				}()
   232  			}
   233  
   234  			originalHelpPrinter(w, tmpl, helpData{data, AppHelpFlagGroups})
   235  		} else if tmpl == utils.CommandHelpTemplate {
   236  
   237  			categorized := make(map[string][]cli.Flag)
   238  			for _, flag := range data.(cli.Command).Flags {
   239  				if _, ok := categorized[flag.String()]; !ok {
   240  					categorized[flagCategory(flag)] = append(categorized[flagCategory(flag)], flag)
   241  				}
   242  			}
   243  
   244  			sorted := make([]flagGroup, 0, len(categorized))
   245  			for cat, flgs := range categorized {
   246  				sorted = append(sorted, flagGroup{cat, flgs})
   247  			}
   248  			sort.Sort(byCategory(sorted))
   249  
   250  			originalHelpPrinter(w, tmpl, map[string]interface{}{
   251  				"cmd":              data,
   252  				"categorizedFlags": sorted,
   253  			})
   254  		} else {
   255  			originalHelpPrinter(w, tmpl, data)
   256  		}
   257  	}
   258  }