github.com/unicornultrafoundation/go-u2u@v1.0.0-rc1.0.20240205080301-e74a83d3fadc/cmd/u2u/launcher/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 launcher
    20  
    21  import (
    22  	"io"
    23  	"sort"
    24  
    25  	cli "gopkg.in/urfave/cli.v1"
    26  
    27  	"github.com/unicornultrafoundation/go-u2u/cmd/utils"
    28  	"github.com/unicornultrafoundation/go-u2u/debug"
    29  	"github.com/unicornultrafoundation/go-u2u/flags"
    30  )
    31  
    32  // AppHelpFlagGroups is the application flags, grouped by functionality.
    33  var AppHelpFlagGroups = calcAppHelpFlagGroups()
    34  
    35  func calcAppHelpFlagGroups() []flags.FlagGroup {
    36  	overrideFlags()
    37  	overrideParams()
    38  
    39  	initFlags()
    40  	return []flags.FlagGroup{
    41  		{
    42  			Name:  "U2U",
    43  			Flags: u2uFlags,
    44  		},
    45  		{
    46  			Name:  "TRANSACTION POOL",
    47  			Flags: txpoolFlags,
    48  		},
    49  		{
    50  			Name:  "PERFORMANCE TUNING",
    51  			Flags: performanceFlags,
    52  		},
    53  		{
    54  			Name:  "ACCOUNT",
    55  			Flags: accountFlags,
    56  		},
    57  		{
    58  			Name:  "API",
    59  			Flags: rpcFlags,
    60  		},
    61  		{
    62  			Name:  "CONSOLE",
    63  			Flags: consoleFlags,
    64  		},
    65  		{
    66  			Name:  "NETWORKING",
    67  			Flags: networkingFlags,
    68  		},
    69  		{
    70  			Name:  "GAS PRICE ORACLE",
    71  			Flags: gpoFlags,
    72  		},
    73  		{
    74  			Name:  "METRICS AND STATS",
    75  			Flags: metricsFlags,
    76  		},
    77  		{
    78  			Name:  "TESTING",
    79  			Flags: testFlags,
    80  		},
    81  		{
    82  			Name:  "LOGGING AND DEBUGGING",
    83  			Flags: debug.Flags,
    84  		},
    85  		{
    86  			Name: "MISC",
    87  			Flags: []cli.Flag{
    88  				cli.HelpFlag,
    89  			},
    90  		},
    91  	}
    92  }
    93  
    94  func init() {
    95  	// Override the default app help template
    96  	cli.AppHelpTemplate = flags.AppHelpTemplate
    97  
    98  	// Override the default app help printer, but only for the global app help
    99  	originalHelpPrinter := cli.HelpPrinter
   100  	cli.HelpPrinter = func(w io.Writer, tmpl string, data interface{}) {
   101  		if tmpl == flags.AppHelpTemplate {
   102  			// Iterate over all the flags and add any uncategorized ones
   103  			categorized := make(map[string]struct{})
   104  			for _, group := range AppHelpFlagGroups {
   105  				for _, flag := range group.Flags {
   106  					categorized[flag.String()] = struct{}{}
   107  				}
   108  			}
   109  			deprecated := make(map[string]struct{})
   110  			for _, flag := range utils.DeprecatedFlags {
   111  				deprecated[flag.String()] = struct{}{}
   112  			}
   113  			// Only add uncategorized flags if they are not deprecated
   114  			var uncategorized []cli.Flag
   115  			for _, flag := range data.(*cli.App).Flags {
   116  				if _, ok := categorized[flag.String()]; !ok {
   117  					if _, ok := deprecated[flag.String()]; !ok {
   118  						uncategorized = append(uncategorized, flag)
   119  					}
   120  				}
   121  			}
   122  			if len(uncategorized) > 0 {
   123  				// Append all ungategorized options to the misc group
   124  				miscs := len(AppHelpFlagGroups[len(AppHelpFlagGroups)-1].Flags)
   125  				AppHelpFlagGroups[len(AppHelpFlagGroups)-1].Flags = append(AppHelpFlagGroups[len(AppHelpFlagGroups)-1].Flags, uncategorized...)
   126  
   127  				// Make sure they are removed afterwards
   128  				defer func() {
   129  					AppHelpFlagGroups[len(AppHelpFlagGroups)-1].Flags = AppHelpFlagGroups[len(AppHelpFlagGroups)-1].Flags[:miscs]
   130  				}()
   131  			}
   132  			// Render out custom usage screen
   133  			originalHelpPrinter(w, tmpl, flags.HelpData{App: data, FlagGroups: AppHelpFlagGroups})
   134  		} else if tmpl == flags.CommandHelpTemplate {
   135  			// Iterate over all command specific flags and categorize them
   136  			categorized := make(map[string][]cli.Flag)
   137  			for _, flag := range data.(cli.Command).Flags {
   138  				if _, ok := categorized[flag.String()]; !ok {
   139  					categorized[flags.FlagCategory(flag, AppHelpFlagGroups)] = append(categorized[flags.FlagCategory(flag, AppHelpFlagGroups)], flag)
   140  				}
   141  			}
   142  
   143  			// sort to get a stable ordering
   144  			sorted := make([]flags.FlagGroup, 0, len(categorized))
   145  			for cat, flgs := range categorized {
   146  				sorted = append(sorted, flags.FlagGroup{Name: cat, Flags: flgs})
   147  			}
   148  			sort.Sort(flags.ByCategory(sorted))
   149  
   150  			// add sorted array to data and render with default printer
   151  			originalHelpPrinter(w, tmpl, map[string]interface{}{
   152  				"cmd":              data,
   153  				"categorizedFlags": sorted,
   154  			})
   155  		} else {
   156  			originalHelpPrinter(w, tmpl, data)
   157  		}
   158  	}
   159  }