github.com/LampardNguyen234/go-ethereum@v1.10.16-0.20220117140830-b6a3b0260724/internal/flags/helpers.go (about)

     1  // Copyright 2020 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  package flags
    18  
    19  import (
    20  	"os"
    21  	"path/filepath"
    22  
    23  	"github.com/LampardNguyen234/go-ethereum/params"
    24  	"gopkg.in/urfave/cli.v1"
    25  )
    26  
    27  var (
    28  	CommandHelpTemplate = `{{.cmd.Name}}{{if .cmd.Subcommands}} command{{end}}{{if .cmd.Flags}} [command options]{{end}} {{.cmd.ArgsUsage}}
    29  {{if .cmd.Description}}{{.cmd.Description}}
    30  {{end}}{{if .cmd.Subcommands}}
    31  SUBCOMMANDS:
    32    {{range .cmd.Subcommands}}{{.Name}}{{with .ShortName}}, {{.}}{{end}}{{ "\t" }}{{.Usage}}
    33    {{end}}{{end}}{{if .categorizedFlags}}
    34  {{range $idx, $categorized := .categorizedFlags}}{{$categorized.Name}} OPTIONS:
    35  {{range $categorized.Flags}}{{"\t"}}{{.}}
    36  {{end}}
    37  {{end}}{{end}}`
    38  
    39  	OriginCommandHelpTemplate = `{{.Name}}{{if .Subcommands}} command{{end}}{{if .Flags}} [command options]{{end}} {{.ArgsUsage}}
    40  {{if .Description}}{{.Description}}
    41  {{end}}{{if .Subcommands}}
    42  SUBCOMMANDS:
    43    {{range .Subcommands}}{{.Name}}{{with .ShortName}}, {{.}}{{end}}{{ "\t" }}{{.Usage}}
    44    {{end}}{{end}}{{if .Flags}}
    45  OPTIONS:
    46  {{range $.Flags}}   {{.}}
    47  {{end}}
    48  {{end}}`
    49  
    50  	// AppHelpTemplate is the test template for the default, global app help topic.
    51  	AppHelpTemplate = `NAME:
    52     {{.App.Name}} - {{.App.Usage}}
    53  
    54     Copyright 2013-2021 The go-ethereum Authors
    55  
    56  USAGE:
    57     {{.App.HelpName}} [options]{{if .App.Commands}} [command] [command options]{{end}} {{if .App.ArgsUsage}}{{.App.ArgsUsage}}{{else}}[arguments...]{{end}}
    58     {{if .App.Version}}
    59  VERSION:
    60     {{.App.Version}}
    61     {{end}}{{if len .App.Authors}}
    62  AUTHOR(S):
    63     {{range .App.Authors}}{{ . }}{{end}}
    64     {{end}}{{if .App.Commands}}
    65  COMMANDS:
    66     {{range .App.Commands}}{{join .Names ", "}}{{ "\t" }}{{.Usage}}
    67     {{end}}{{end}}{{if .FlagGroups}}
    68  {{range .FlagGroups}}{{.Name}} OPTIONS:
    69    {{range .Flags}}{{.}}
    70    {{end}}
    71  {{end}}{{end}}{{if .App.Copyright }}
    72  COPYRIGHT:
    73     {{.App.Copyright}}
    74     {{end}}
    75  `
    76  	// ClefAppHelpTemplate is the template for the default, global app help topic.
    77  	ClefAppHelpTemplate = `NAME:
    78     {{.App.Name}} - {{.App.Usage}}
    79  
    80     Copyright 2013-2021 The go-ethereum Authors
    81  
    82  USAGE:
    83     {{.App.HelpName}} [options]{{if .App.Commands}} command [command options]{{end}} {{if .App.ArgsUsage}}{{.App.ArgsUsage}}{{else}}[arguments...]{{end}}
    84     {{if .App.Version}}
    85  COMMANDS:
    86     {{range .App.Commands}}{{join .Names ", "}}{{ "\t" }}{{.Usage}}
    87     {{end}}{{end}}{{if .FlagGroups}}
    88  {{range .FlagGroups}}{{.Name}} OPTIONS:
    89    {{range .Flags}}{{.}}
    90    {{end}}
    91  {{end}}{{end}}{{if .App.Copyright }}
    92  COPYRIGHT:
    93     {{.App.Copyright}}
    94     {{end}}
    95  `
    96  )
    97  
    98  // HelpData is a one shot struct to pass to the usage template
    99  type HelpData struct {
   100  	App        interface{}
   101  	FlagGroups []FlagGroup
   102  }
   103  
   104  // FlagGroup is a collection of flags belonging to a single topic.
   105  type FlagGroup struct {
   106  	Name  string
   107  	Flags []cli.Flag
   108  }
   109  
   110  // byCategory sorts an array of FlagGroup by Name in the order
   111  // defined in AppHelpFlagGroups.
   112  type ByCategory []FlagGroup
   113  
   114  func (a ByCategory) Len() int      { return len(a) }
   115  func (a ByCategory) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
   116  func (a ByCategory) Less(i, j int) bool {
   117  	iCat, jCat := a[i].Name, a[j].Name
   118  	iIdx, jIdx := len(a), len(a) // ensure non categorized flags come last
   119  
   120  	for i, group := range a {
   121  		if iCat == group.Name {
   122  			iIdx = i
   123  		}
   124  		if jCat == group.Name {
   125  			jIdx = i
   126  		}
   127  	}
   128  
   129  	return iIdx < jIdx
   130  }
   131  
   132  func FlagCategory(flag cli.Flag, flagGroups []FlagGroup) string {
   133  	for _, category := range flagGroups {
   134  		for _, flg := range category.Flags {
   135  			if flg.GetName() == flag.GetName() {
   136  				return category.Name
   137  			}
   138  		}
   139  	}
   140  	return "MISC"
   141  }
   142  
   143  // NewApp creates an app with sane defaults.
   144  func NewApp(gitCommit, gitDate, usage string) *cli.App {
   145  	app := cli.NewApp()
   146  	app.Name = filepath.Base(os.Args[0])
   147  	app.Author = ""
   148  	app.Email = ""
   149  	app.Version = params.VersionWithCommit(gitCommit, gitDate)
   150  	app.Usage = usage
   151  	return app
   152  }