Skip to content

Commit 4d358b9

Browse files
SjonHortensiusfjl
authored andcommitted
cmd/utils: customize cli.HelpPrinter to fix alignment (ethereum#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.
1 parent 1eaf66a commit 4d358b9

File tree

2 files changed

+56
-50
lines changed

2 files changed

+56
-50
lines changed

cmd/utils/customflags.go

Lines changed: 37 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ import (
2020
"encoding"
2121
"errors"
2222
"flag"
23-
"fmt"
2423
"math/big"
2524
"os"
2625
"os/user"
@@ -34,33 +33,44 @@ import (
3433
// Custom type which is registered in the flags library which cli uses for
3534
// argument parsing. This allows us to expand Value to an absolute path when
3635
// the argument is parsed
37-
type DirectoryString struct {
38-
Value string
39-
}
36+
type DirectoryString string
4037

41-
func (self *DirectoryString) String() string {
42-
return self.Value
38+
func (s *DirectoryString) String() string {
39+
return string(*s)
4340
}
4441

45-
func (self *DirectoryString) Set(value string) error {
46-
self.Value = expandPath(value)
42+
func (s *DirectoryString) Set(value string) error {
43+
*s = DirectoryString(expandPath(value))
4744
return nil
4845
}
4946

5047
// Custom cli.Flag type which expand the received string to an absolute path.
5148
// e.g. ~/.ethereum -> /home/username/.ethereum
5249
type DirectoryFlag struct {
53-
Name string
54-
Value DirectoryString
55-
Usage string
50+
Name string
51+
Value DirectoryString
52+
Usage string
53+
EnvVar string
5654
}
5755

58-
func (self DirectoryFlag) String() string {
59-
fmtString := "%s %v\t%v"
60-
if len(self.Value.Value) > 0 {
61-
fmtString = "%s \"%v\"\t%v"
62-
}
63-
return fmt.Sprintf(fmtString, prefixedNames(self.Name), self.Value.Value, self.Usage)
56+
func (f DirectoryFlag) String() string {
57+
return cli.FlagStringer(f)
58+
}
59+
60+
// called by cli library, grabs variable from environment (if in env)
61+
// and adds variable to flag set for parsing.
62+
func (f DirectoryFlag) Apply(set *flag.FlagSet) {
63+
eachName(f.Name, func(name string) {
64+
set.Var(&f.Value, f.Name, f.Usage)
65+
})
66+
}
67+
68+
func (f DirectoryFlag) GetName() string {
69+
return f.Name
70+
}
71+
72+
func (f *DirectoryFlag) Set(value string) {
73+
f.Value.Set(value)
6474
}
6575

6676
func eachName(longName string, fn func(string)) {
@@ -71,14 +81,6 @@ func eachName(longName string, fn func(string)) {
7181
}
7282
}
7383

74-
// called by cli library, grabs variable from environment (if in env)
75-
// and adds variable to flag set for parsing.
76-
func (self DirectoryFlag) Apply(set *flag.FlagSet) {
77-
eachName(self.Name, func(name string) {
78-
set.Var(&self.Value, self.Name, self.Usage)
79-
})
80-
}
81-
8284
type TextMarshaler interface {
8385
encoding.TextMarshaler
8486
encoding.TextUnmarshaler
@@ -103,17 +105,18 @@ func (v textMarshalerVal) Set(s string) error {
103105

104106
// TextMarshalerFlag wraps a TextMarshaler value.
105107
type TextMarshalerFlag struct {
106-
Name string
107-
Value TextMarshaler
108-
Usage string
108+
Name string
109+
Value TextMarshaler
110+
Usage string
111+
EnvVar string
109112
}
110113

111114
func (f TextMarshalerFlag) GetName() string {
112115
return f.Name
113116
}
114117

115118
func (f TextMarshalerFlag) String() string {
116-
return fmt.Sprintf("%s \"%v\"\t%v", prefixedNames(f.Name), f.Value, f.Usage)
119+
return cli.FlagStringer(f)
117120
}
118121

119122
func (f TextMarshalerFlag) Apply(set *flag.FlagSet) {
@@ -134,9 +137,10 @@ func GlobalTextMarshaler(ctx *cli.Context, name string) TextMarshaler {
134137
// BigFlag is a command line flag that accepts 256 bit big integers in decimal or
135138
// hexadecimal syntax.
136139
type BigFlag struct {
137-
Name string
138-
Value *big.Int
139-
Usage string
140+
Name string
141+
Value *big.Int
142+
Usage string
143+
EnvVar string
140144
}
141145

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

165169
func (f BigFlag) String() string {
166-
fmtString := "%s %v\t%v"
167-
if f.Value != nil {
168-
fmtString = "%s \"%v\"\t%v"
169-
}
170-
return fmt.Sprintf(fmtString, prefixedNames(f.Name), f.Value, f.Usage)
170+
return cli.FlagStringer(f)
171171
}
172172

173173
func (f BigFlag) Apply(set *flag.FlagSet) {
@@ -207,14 +207,6 @@ func prefixedNames(fullName string) (prefixed string) {
207207
return
208208
}
209209

210-
func (self DirectoryFlag) GetName() string {
211-
return self.Name
212-
}
213-
214-
func (self *DirectoryFlag) Set(value string) {
215-
self.Value.Value = value
216-
}
217-
218210
// Expands a file path
219211
// 1. replace tilde with users home dir
220212
// 2. expands embedded environment variables

cmd/utils/flags.go

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,15 @@ import (
2121
"crypto/ecdsa"
2222
"errors"
2323
"fmt"
24+
"io"
2425
"io/ioutil"
2526
"math/big"
2627
"os"
2728
"path/filepath"
2829
"strconv"
2930
"strings"
31+
"text/tabwriter"
32+
"text/template"
3033
"time"
3134

3235
"github.com/ethereum/go-ethereum/accounts"
@@ -90,8 +93,8 @@ GLOBAL OPTIONS:
9093
{{range .Flags}}{{.}}
9194
{{end}}{{end}}
9295
`
93-
9496
cli.CommandHelpTemplate = CommandHelpTemplate
97+
cli.HelpPrinter = printHelp
9598
}
9699

97100
// NewApp creates an app with sane defaults.
@@ -105,6 +108,17 @@ func NewApp(gitCommit, gitDate, usage string) *cli.App {
105108
return app
106109
}
107110

111+
func printHelp(out io.Writer, templ string, data interface{}) {
112+
funcMap := template.FuncMap{"join": strings.Join}
113+
t := template.Must(template.New("help").Funcs(funcMap).Parse(templ))
114+
w := tabwriter.NewWriter(out, 38, 8, 2, ' ', 0)
115+
err := t.Execute(w, data)
116+
if err != nil {
117+
panic(err)
118+
}
119+
w.Flush()
120+
}
121+
108122
// These are all the command line flags we support.
109123
// If you add to this list, please remember to include the
110124
// flag in the appropriate command definition.
@@ -117,7 +131,7 @@ var (
117131
DataDirFlag = DirectoryFlag{
118132
Name: "datadir",
119133
Usage: "Data directory for the databases and keystore",
120-
Value: DirectoryString{node.DefaultDataDir()},
134+
Value: DirectoryString(node.DefaultDataDir()),
121135
}
122136
AncientFlag = DirectoryFlag{
123137
Name: "datadir.ancient",
@@ -168,7 +182,7 @@ var (
168182
DocRootFlag = DirectoryFlag{
169183
Name: "docroot",
170184
Usage: "Document Root for HTTPClient file scheme",
171-
Value: DirectoryString{homeDir()},
185+
Value: DirectoryString(homeDir()),
172186
}
173187
ExitWhenSyncedFlag = cli.BoolFlag{
174188
Name: "exitwhensynced",
@@ -291,8 +305,8 @@ var (
291305
}
292306
EthashDatasetDirFlag = DirectoryFlag{
293307
Name: "ethash.dagdir",
294-
Usage: "Directory to store the ethash mining DAGs (default = inside home folder)",
295-
Value: DirectoryString{eth.DefaultConfig.Ethash.DatasetDir},
308+
Usage: "Directory to store the ethash mining DAGs",
309+
Value: DirectoryString(eth.DefaultConfig.Ethash.DatasetDir),
296310
}
297311
EthashDatasetsInMemoryFlag = cli.IntFlag{
298312
Name: "ethash.dagsinmem",

0 commit comments

Comments
 (0)