Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cmd/utils: customize cli.HelpPrinter to fix alignment #19956

Merged
merged 2 commits into from
Aug 22, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
82 changes: 37 additions & 45 deletions cmd/utils/customflags.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import (
"encoding"
"errors"
"flag"
"fmt"
"math/big"
"os"
"os/user"
Expand All @@ -34,33 +33,44 @@ import (
// 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
// the argument is parsed
type DirectoryString struct {
Value string
}
type DirectoryString string

func (self *DirectoryString) String() string {
return self.Value
func (s *DirectoryString) String() string {
return string(*s)
}

func (self *DirectoryString) Set(value string) error {
self.Value = expandPath(value)
func (s *DirectoryString) Set(value string) error {
*s = DirectoryString(expandPath(value))
return nil
}

// Custom cli.Flag type which expand the received string to an absolute path.
// e.g. ~/.ethereum -> /home/username/.ethereum
type DirectoryFlag struct {
Name string
Value DirectoryString
Usage string
Name string
Value DirectoryString
Usage string
EnvVar string
}

func (self DirectoryFlag) String() string {
fmtString := "%s %v\t%v"
if len(self.Value.Value) > 0 {
fmtString = "%s \"%v\"\t%v"
}
return fmt.Sprintf(fmtString, prefixedNames(self.Name), self.Value.Value, self.Usage)
func (f DirectoryFlag) String() string {
return cli.FlagStringer(f)
}

// called by cli library, grabs variable from environment (if in env)
// 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)) {
Expand All @@ -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 {
encoding.TextMarshaler
encoding.TextUnmarshaler
Expand All @@ -103,17 +105,18 @@ func (v textMarshalerVal) Set(s string) error {

// TextMarshalerFlag wraps a TextMarshaler value.
type TextMarshalerFlag struct {
Name string
Value TextMarshaler
Usage string
Name string
Value TextMarshaler
Usage string
EnvVar string
}

func (f TextMarshalerFlag) GetName() string {
return f.Name
}

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) {
Expand All @@ -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
// hexadecimal syntax.
type BigFlag struct {
Name string
Value *big.Int
Usage string
Name string
Value *big.Int
Usage string
EnvVar string
}

// bigValue turns *big.Int into a flag.Value
Expand All @@ -163,11 +167,7 @@ func (f BigFlag) GetName() string {
}

func (f BigFlag) String() string {
fmtString := "%s %v\t%v"
if f.Value != nil {
fmtString = "%s \"%v\"\t%v"
}
return fmt.Sprintf(fmtString, prefixedNames(f.Name), f.Value, f.Usage)
return cli.FlagStringer(f)
}

func (f BigFlag) Apply(set *flag.FlagSet) {
Expand Down Expand Up @@ -207,14 +207,6 @@ func prefixedNames(fullName string) (prefixed string) {
return
}

func (self DirectoryFlag) GetName() string {
return self.Name
}

func (self *DirectoryFlag) Set(value string) {
self.Value.Value = value
}

// Expands a file path
// 1. replace tilde with users home dir
// 2. expands embedded environment variables
Expand Down
24 changes: 19 additions & 5 deletions cmd/utils/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,15 @@ import (
"crypto/ecdsa"
"errors"
"fmt"
"io"
"io/ioutil"
"math/big"
"os"
"path/filepath"
"strconv"
"strings"
"text/tabwriter"
"text/template"
"time"

"github.com/ethereum/go-ethereum/accounts"
Expand Down Expand Up @@ -90,8 +93,8 @@ GLOBAL OPTIONS:
{{range .Flags}}{{.}}
{{end}}{{end}}
`

cli.CommandHelpTemplate = CommandHelpTemplate
cli.HelpPrinter = printHelp
}

// NewApp creates an app with sane defaults.
Expand All @@ -105,6 +108,17 @@ func NewApp(gitCommit, gitDate, usage string) *cli.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.
// If you add to this list, please remember to include the
// flag in the appropriate command definition.
Expand All @@ -117,7 +131,7 @@ var (
DataDirFlag = DirectoryFlag{
Name: "datadir",
Usage: "Data directory for the databases and keystore",
Value: DirectoryString{node.DefaultDataDir()},
Value: DirectoryString(node.DefaultDataDir()),
}
AncientFlag = DirectoryFlag{
Name: "datadir.ancient",
Expand Down Expand Up @@ -168,7 +182,7 @@ var (
DocRootFlag = DirectoryFlag{
Name: "docroot",
Usage: "Document Root for HTTPClient file scheme",
Value: DirectoryString{homeDir()},
Value: DirectoryString(homeDir()),
}
ExitWhenSyncedFlag = cli.BoolFlag{
Name: "exitwhensynced",
Expand Down Expand Up @@ -291,8 +305,8 @@ var (
}
EthashDatasetDirFlag = DirectoryFlag{
Name: "ethash.dagdir",
Usage: "Directory to store the ethash mining DAGs (default = inside home folder)",
Value: DirectoryString{eth.DefaultConfig.Ethash.DatasetDir},
Usage: "Directory to store the ethash mining DAGs",
Value: DirectoryString(eth.DefaultConfig.Ethash.DatasetDir),
}
EthashDatasetsInMemoryFlag = cli.IntFlag{
Name: "ethash.dagsinmem",
Expand Down