2020-07-14 03:35:32 -05:00
|
|
|
// Copyright 2020 The go-ethereum Authors
|
|
|
|
// This file is part of go-ethereum.
|
|
|
|
//
|
|
|
|
// go-ethereum is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
|
|
|
// go-ethereum is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU General Public License
|
|
|
|
// along with go-ethereum. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
package flags
|
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
|
|
|
|
"github.com/ethereum/go-ethereum/params"
|
2020-11-25 14:00:23 -06:00
|
|
|
"gopkg.in/urfave/cli.v1"
|
2020-07-14 03:35:32 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2021-02-23 04:27:32 -06:00
|
|
|
CommandHelpTemplate = `{{.cmd.Name}}{{if .cmd.Subcommands}} command{{end}}{{if .cmd.Flags}} [command options]{{end}} {{.cmd.ArgsUsage}}
|
2020-07-14 03:35:32 -05:00
|
|
|
{{if .cmd.Description}}{{.cmd.Description}}
|
|
|
|
{{end}}{{if .cmd.Subcommands}}
|
|
|
|
SUBCOMMANDS:
|
|
|
|
{{range .cmd.Subcommands}}{{.Name}}{{with .ShortName}}, {{.}}{{end}}{{ "\t" }}{{.Usage}}
|
|
|
|
{{end}}{{end}}{{if .categorizedFlags}}
|
|
|
|
{{range $idx, $categorized := .categorizedFlags}}{{$categorized.Name}} OPTIONS:
|
|
|
|
{{range $categorized.Flags}}{{"\t"}}{{.}}
|
|
|
|
{{end}}
|
|
|
|
{{end}}{{end}}`
|
|
|
|
|
2021-02-23 04:27:32 -06:00
|
|
|
OriginCommandHelpTemplate = `{{.Name}}{{if .Subcommands}} command{{end}}{{if .Flags}} [command options]{{end}} {{.ArgsUsage}}
|
2020-07-14 03:35:32 -05:00
|
|
|
{{if .Description}}{{.Description}}
|
|
|
|
{{end}}{{if .Subcommands}}
|
|
|
|
SUBCOMMANDS:
|
|
|
|
{{range .Subcommands}}{{.Name}}{{with .ShortName}}, {{.}}{{end}}{{ "\t" }}{{.Usage}}
|
|
|
|
{{end}}{{end}}{{if .Flags}}
|
|
|
|
OPTIONS:
|
|
|
|
{{range $.Flags}} {{.}}
|
|
|
|
{{end}}
|
|
|
|
{{end}}`
|
|
|
|
|
|
|
|
// AppHelpTemplate is the test template for the default, global app help topic.
|
|
|
|
AppHelpTemplate = `NAME:
|
|
|
|
{{.App.Name}} - {{.App.Usage}}
|
|
|
|
|
2022-03-01 03:53:19 -06:00
|
|
|
Copyright 2013-2022 The go-ethereum Authors
|
2020-07-14 03:35:32 -05:00
|
|
|
|
|
|
|
USAGE:
|
2021-01-24 04:37:08 -06:00
|
|
|
{{.App.HelpName}} [options]{{if .App.Commands}} [command] [command options]{{end}} {{if .App.ArgsUsage}}{{.App.ArgsUsage}}{{else}}[arguments...]{{end}}
|
2020-07-14 03:35:32 -05:00
|
|
|
{{if .App.Version}}
|
|
|
|
VERSION:
|
|
|
|
{{.App.Version}}
|
|
|
|
{{end}}{{if len .App.Authors}}
|
|
|
|
AUTHOR(S):
|
|
|
|
{{range .App.Authors}}{{ . }}{{end}}
|
|
|
|
{{end}}{{if .App.Commands}}
|
|
|
|
COMMANDS:
|
|
|
|
{{range .App.Commands}}{{join .Names ", "}}{{ "\t" }}{{.Usage}}
|
|
|
|
{{end}}{{end}}{{if .FlagGroups}}
|
|
|
|
{{range .FlagGroups}}{{.Name}} OPTIONS:
|
|
|
|
{{range .Flags}}{{.}}
|
|
|
|
{{end}}
|
|
|
|
{{end}}{{end}}{{if .App.Copyright }}
|
|
|
|
COPYRIGHT:
|
|
|
|
{{.App.Copyright}}
|
|
|
|
{{end}}
|
|
|
|
`
|
|
|
|
// ClefAppHelpTemplate is the template for the default, global app help topic.
|
|
|
|
ClefAppHelpTemplate = `NAME:
|
|
|
|
{{.App.Name}} - {{.App.Usage}}
|
|
|
|
|
2022-03-01 03:53:19 -06:00
|
|
|
Copyright 2013-2022 The go-ethereum Authors
|
2020-07-14 03:35:32 -05:00
|
|
|
|
|
|
|
USAGE:
|
|
|
|
{{.App.HelpName}} [options]{{if .App.Commands}} command [command options]{{end}} {{if .App.ArgsUsage}}{{.App.ArgsUsage}}{{else}}[arguments...]{{end}}
|
|
|
|
{{if .App.Version}}
|
|
|
|
COMMANDS:
|
|
|
|
{{range .App.Commands}}{{join .Names ", "}}{{ "\t" }}{{.Usage}}
|
|
|
|
{{end}}{{end}}{{if .FlagGroups}}
|
|
|
|
{{range .FlagGroups}}{{.Name}} OPTIONS:
|
|
|
|
{{range .Flags}}{{.}}
|
|
|
|
{{end}}
|
|
|
|
{{end}}{{end}}{{if .App.Copyright }}
|
|
|
|
COPYRIGHT:
|
|
|
|
{{.App.Copyright}}
|
|
|
|
{{end}}
|
|
|
|
`
|
|
|
|
)
|
|
|
|
|
|
|
|
// HelpData is a one shot struct to pass to the usage template
|
|
|
|
type HelpData struct {
|
|
|
|
App interface{}
|
|
|
|
FlagGroups []FlagGroup
|
|
|
|
}
|
|
|
|
|
|
|
|
// FlagGroup is a collection of flags belonging to a single topic.
|
|
|
|
type FlagGroup struct {
|
|
|
|
Name string
|
|
|
|
Flags []cli.Flag
|
|
|
|
}
|
|
|
|
|
|
|
|
// byCategory sorts an array of FlagGroup by Name in the order
|
|
|
|
// defined in AppHelpFlagGroups.
|
|
|
|
type ByCategory []FlagGroup
|
|
|
|
|
|
|
|
func (a ByCategory) Len() int { return len(a) }
|
|
|
|
func (a ByCategory) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
|
|
|
|
func (a ByCategory) Less(i, j int) bool {
|
|
|
|
iCat, jCat := a[i].Name, a[j].Name
|
|
|
|
iIdx, jIdx := len(a), len(a) // ensure non categorized flags come last
|
|
|
|
|
|
|
|
for i, group := range a {
|
|
|
|
if iCat == group.Name {
|
|
|
|
iIdx = i
|
|
|
|
}
|
|
|
|
if jCat == group.Name {
|
|
|
|
jIdx = i
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return iIdx < jIdx
|
|
|
|
}
|
|
|
|
|
|
|
|
func FlagCategory(flag cli.Flag, flagGroups []FlagGroup) string {
|
|
|
|
for _, category := range flagGroups {
|
|
|
|
for _, flg := range category.Flags {
|
|
|
|
if flg.GetName() == flag.GetName() {
|
|
|
|
return category.Name
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return "MISC"
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewApp creates an app with sane defaults.
|
|
|
|
func NewApp(gitCommit, gitDate, usage string) *cli.App {
|
|
|
|
app := cli.NewApp()
|
2022-03-15 04:38:23 -05:00
|
|
|
app.EnableBashCompletion = true
|
2020-07-14 03:35:32 -05:00
|
|
|
app.Name = filepath.Base(os.Args[0])
|
|
|
|
app.Author = ""
|
|
|
|
app.Email = ""
|
|
|
|
app.Version = params.VersionWithCommit(gitCommit, gitDate)
|
|
|
|
app.Usage = usage
|
|
|
|
return app
|
|
|
|
}
|