erigon-pulse/internal/flags/helpers.go

82 lines
2.1 KiB
Go
Raw Normal View History

// 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/ledgerwatch/erigon/params"
2020-08-07 12:25:40 +00:00
"github.com/urfave/cli"
)
// 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()
app.Name = filepath.Base(os.Args[0])
app.Author = ""
app.Email = ""
app.Version = params.VersionWithCommit(gitCommit, gitDate)
app.Usage = usage
return app
}