cmd: customize cli.HelpPrinter to fix alignment across sections

this copies cli.printHelp but changes minwidth to 40
This commit is contained in:
Sjon Hortensius 2019-08-13 14:36:30 +02:00
parent aaf29095bb
commit 2343d95237

View file

@ -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"
@ -96,6 +99,26 @@ GLOBAL OPTIONS:
// NewApp creates an app with sane defaults. // NewApp creates an app with sane defaults.
func NewApp(gitCommit, gitDate, usage string) *cli.App { func NewApp(gitCommit, gitDate, usage string) *cli.App {
// Setup a global printer to fix alignment across commands
cli.HelpPrinter = func(out io.Writer, templ string, data interface{}) {
funcMap := template.FuncMap{
"join": strings.Join,
}
w := tabwriter.NewWriter(out, 40, 8, 2, ' ', 0)
t := template.Must(template.New("help").Funcs(funcMap).Parse(templ))
err := t.Execute(w, data)
if err != nil {
// If the writer is closed, t.Execute will fail, and there's nothing
// we can do to recover.
if os.Getenv("CLI_TEMPLATE_ERROR_DEBUG") != "" {
log.Crit("CLI TEMPLATE ERROR: %#v\n", err)
}
return
}
w.Flush()
}
app := cli.NewApp() app := cli.NewApp()
app.Name = filepath.Base(os.Args[0]) app.Name = filepath.Base(os.Args[0])
app.Author = "" app.Author = ""