cmd/utils: customize cli.HelpPrinter to fix alignment (#19956)
This copies cli.printHelp but changes minwidth to 38. Custom flag code is improved to print the default value using cli.FlagStringer like all built-in flags do.
This commit is contained in:
parent
1eaf66ae60
commit
4d358b9fc0
@ -20,7 +20,6 @@ import (
|
|||||||
"encoding"
|
"encoding"
|
||||||
"errors"
|
"errors"
|
||||||
"flag"
|
"flag"
|
||||||
"fmt"
|
|
||||||
"math/big"
|
"math/big"
|
||||||
"os"
|
"os"
|
||||||
"os/user"
|
"os/user"
|
||||||
@ -34,33 +33,44 @@ import (
|
|||||||
// Custom type which is registered in the flags library which cli uses for
|
// Custom type which is registered in the flags library which cli uses for
|
||||||
// argument parsing. This allows us to expand Value to an absolute path when
|
// argument parsing. This allows us to expand Value to an absolute path when
|
||||||
// the argument is parsed
|
// the argument is parsed
|
||||||
type DirectoryString struct {
|
type DirectoryString string
|
||||||
Value string
|
|
||||||
|
func (s *DirectoryString) String() string {
|
||||||
|
return string(*s)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (self *DirectoryString) String() string {
|
func (s *DirectoryString) Set(value string) error {
|
||||||
return self.Value
|
*s = DirectoryString(expandPath(value))
|
||||||
}
|
|
||||||
|
|
||||||
func (self *DirectoryString) Set(value string) error {
|
|
||||||
self.Value = expandPath(value)
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// Custom cli.Flag type which expand the received string to an absolute path.
|
// Custom cli.Flag type which expand the received string to an absolute path.
|
||||||
// e.g. ~/.ethereum -> /home/username/.ethereum
|
// e.g. ~/.ethereum -> /home/username/.ethereum
|
||||||
type DirectoryFlag struct {
|
type DirectoryFlag struct {
|
||||||
Name string
|
Name string
|
||||||
Value DirectoryString
|
Value DirectoryString
|
||||||
Usage string
|
Usage string
|
||||||
|
EnvVar string
|
||||||
}
|
}
|
||||||
|
|
||||||
func (self DirectoryFlag) String() string {
|
func (f DirectoryFlag) String() string {
|
||||||
fmtString := "%s %v\t%v"
|
return cli.FlagStringer(f)
|
||||||
if len(self.Value.Value) > 0 {
|
}
|
||||||
fmtString = "%s \"%v\"\t%v"
|
|
||||||
}
|
// called by cli library, grabs variable from environment (if in env)
|
||||||
return fmt.Sprintf(fmtString, prefixedNames(self.Name), self.Value.Value, self.Usage)
|
// and adds variable to flag set for parsing.
|
||||||
|
func (f DirectoryFlag) Apply(set *flag.FlagSet) {
|
||||||
|
eachName(f.Name, func(name string) {
|
||||||
|
set.Var(&f.Value, f.Name, f.Usage)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
func (f DirectoryFlag) GetName() string {
|
||||||
|
return f.Name
|
||||||
|
}
|
||||||
|
|
||||||
|
func (f *DirectoryFlag) Set(value string) {
|
||||||
|
f.Value.Set(value)
|
||||||
}
|
}
|
||||||
|
|
||||||
func eachName(longName string, fn func(string)) {
|
func eachName(longName string, fn func(string)) {
|
||||||
@ -71,14 +81,6 @@ func eachName(longName string, fn func(string)) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// called by cli library, grabs variable from environment (if in env)
|
|
||||||
// and adds variable to flag set for parsing.
|
|
||||||
func (self DirectoryFlag) Apply(set *flag.FlagSet) {
|
|
||||||
eachName(self.Name, func(name string) {
|
|
||||||
set.Var(&self.Value, self.Name, self.Usage)
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
type TextMarshaler interface {
|
type TextMarshaler interface {
|
||||||
encoding.TextMarshaler
|
encoding.TextMarshaler
|
||||||
encoding.TextUnmarshaler
|
encoding.TextUnmarshaler
|
||||||
@ -103,9 +105,10 @@ func (v textMarshalerVal) Set(s string) error {
|
|||||||
|
|
||||||
// TextMarshalerFlag wraps a TextMarshaler value.
|
// TextMarshalerFlag wraps a TextMarshaler value.
|
||||||
type TextMarshalerFlag struct {
|
type TextMarshalerFlag struct {
|
||||||
Name string
|
Name string
|
||||||
Value TextMarshaler
|
Value TextMarshaler
|
||||||
Usage string
|
Usage string
|
||||||
|
EnvVar string
|
||||||
}
|
}
|
||||||
|
|
||||||
func (f TextMarshalerFlag) GetName() string {
|
func (f TextMarshalerFlag) GetName() string {
|
||||||
@ -113,7 +116,7 @@ func (f TextMarshalerFlag) GetName() string {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (f TextMarshalerFlag) String() string {
|
func (f TextMarshalerFlag) String() string {
|
||||||
return fmt.Sprintf("%s \"%v\"\t%v", prefixedNames(f.Name), f.Value, f.Usage)
|
return cli.FlagStringer(f)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (f TextMarshalerFlag) Apply(set *flag.FlagSet) {
|
func (f TextMarshalerFlag) Apply(set *flag.FlagSet) {
|
||||||
@ -134,9 +137,10 @@ func GlobalTextMarshaler(ctx *cli.Context, name string) TextMarshaler {
|
|||||||
// BigFlag is a command line flag that accepts 256 bit big integers in decimal or
|
// BigFlag is a command line flag that accepts 256 bit big integers in decimal or
|
||||||
// hexadecimal syntax.
|
// hexadecimal syntax.
|
||||||
type BigFlag struct {
|
type BigFlag struct {
|
||||||
Name string
|
Name string
|
||||||
Value *big.Int
|
Value *big.Int
|
||||||
Usage string
|
Usage string
|
||||||
|
EnvVar string
|
||||||
}
|
}
|
||||||
|
|
||||||
// bigValue turns *big.Int into a flag.Value
|
// bigValue turns *big.Int into a flag.Value
|
||||||
@ -163,11 +167,7 @@ func (f BigFlag) GetName() string {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (f BigFlag) String() string {
|
func (f BigFlag) String() string {
|
||||||
fmtString := "%s %v\t%v"
|
return cli.FlagStringer(f)
|
||||||
if f.Value != nil {
|
|
||||||
fmtString = "%s \"%v\"\t%v"
|
|
||||||
}
|
|
||||||
return fmt.Sprintf(fmtString, prefixedNames(f.Name), f.Value, f.Usage)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (f BigFlag) Apply(set *flag.FlagSet) {
|
func (f BigFlag) Apply(set *flag.FlagSet) {
|
||||||
@ -207,14 +207,6 @@ func prefixedNames(fullName string) (prefixed string) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
func (self DirectoryFlag) GetName() string {
|
|
||||||
return self.Name
|
|
||||||
}
|
|
||||||
|
|
||||||
func (self *DirectoryFlag) Set(value string) {
|
|
||||||
self.Value.Value = value
|
|
||||||
}
|
|
||||||
|
|
||||||
// Expands a file path
|
// Expands a file path
|
||||||
// 1. replace tilde with users home dir
|
// 1. replace tilde with users home dir
|
||||||
// 2. expands embedded environment variables
|
// 2. expands embedded environment variables
|
||||||
|
@ -21,12 +21,15 @@ import (
|
|||||||
"crypto/ecdsa"
|
"crypto/ecdsa"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"io"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"math/big"
|
"math/big"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
|
"text/tabwriter"
|
||||||
|
"text/template"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/ethereum/go-ethereum/accounts"
|
"github.com/ethereum/go-ethereum/accounts"
|
||||||
@ -90,8 +93,8 @@ GLOBAL OPTIONS:
|
|||||||
{{range .Flags}}{{.}}
|
{{range .Flags}}{{.}}
|
||||||
{{end}}{{end}}
|
{{end}}{{end}}
|
||||||
`
|
`
|
||||||
|
|
||||||
cli.CommandHelpTemplate = CommandHelpTemplate
|
cli.CommandHelpTemplate = CommandHelpTemplate
|
||||||
|
cli.HelpPrinter = printHelp
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewApp creates an app with sane defaults.
|
// NewApp creates an app with sane defaults.
|
||||||
@ -105,6 +108,17 @@ func NewApp(gitCommit, gitDate, usage string) *cli.App {
|
|||||||
return app
|
return app
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func printHelp(out io.Writer, templ string, data interface{}) {
|
||||||
|
funcMap := template.FuncMap{"join": strings.Join}
|
||||||
|
t := template.Must(template.New("help").Funcs(funcMap).Parse(templ))
|
||||||
|
w := tabwriter.NewWriter(out, 38, 8, 2, ' ', 0)
|
||||||
|
err := t.Execute(w, data)
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
w.Flush()
|
||||||
|
}
|
||||||
|
|
||||||
// These are all the command line flags we support.
|
// These are all the command line flags we support.
|
||||||
// If you add to this list, please remember to include the
|
// If you add to this list, please remember to include the
|
||||||
// flag in the appropriate command definition.
|
// flag in the appropriate command definition.
|
||||||
@ -117,7 +131,7 @@ var (
|
|||||||
DataDirFlag = DirectoryFlag{
|
DataDirFlag = DirectoryFlag{
|
||||||
Name: "datadir",
|
Name: "datadir",
|
||||||
Usage: "Data directory for the databases and keystore",
|
Usage: "Data directory for the databases and keystore",
|
||||||
Value: DirectoryString{node.DefaultDataDir()},
|
Value: DirectoryString(node.DefaultDataDir()),
|
||||||
}
|
}
|
||||||
AncientFlag = DirectoryFlag{
|
AncientFlag = DirectoryFlag{
|
||||||
Name: "datadir.ancient",
|
Name: "datadir.ancient",
|
||||||
@ -168,7 +182,7 @@ var (
|
|||||||
DocRootFlag = DirectoryFlag{
|
DocRootFlag = DirectoryFlag{
|
||||||
Name: "docroot",
|
Name: "docroot",
|
||||||
Usage: "Document Root for HTTPClient file scheme",
|
Usage: "Document Root for HTTPClient file scheme",
|
||||||
Value: DirectoryString{homeDir()},
|
Value: DirectoryString(homeDir()),
|
||||||
}
|
}
|
||||||
ExitWhenSyncedFlag = cli.BoolFlag{
|
ExitWhenSyncedFlag = cli.BoolFlag{
|
||||||
Name: "exitwhensynced",
|
Name: "exitwhensynced",
|
||||||
@ -291,8 +305,8 @@ var (
|
|||||||
}
|
}
|
||||||
EthashDatasetDirFlag = DirectoryFlag{
|
EthashDatasetDirFlag = DirectoryFlag{
|
||||||
Name: "ethash.dagdir",
|
Name: "ethash.dagdir",
|
||||||
Usage: "Directory to store the ethash mining DAGs (default = inside home folder)",
|
Usage: "Directory to store the ethash mining DAGs",
|
||||||
Value: DirectoryString{eth.DefaultConfig.Ethash.DatasetDir},
|
Value: DirectoryString(eth.DefaultConfig.Ethash.DatasetDir),
|
||||||
}
|
}
|
||||||
EthashDatasetsInMemoryFlag = cli.IntFlag{
|
EthashDatasetsInMemoryFlag = cli.IntFlag{
|
||||||
Name: "ethash.dagsinmem",
|
Name: "ethash.dagsinmem",
|
||||||
|
Loading…
Reference in New Issue
Block a user