github.com/core-coin/go-core/v2@v2.1.9/internal/flags/helpers.go (about)

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