Merge pull request #10547 from duglin/Issue10214

Pretty the help text
This commit is contained in:
Jessie Frazelle 2015-02-04 11:31:18 -08:00
commit 193aa34569

View file

@ -86,6 +86,7 @@ import (
"fmt" "fmt"
"io" "io"
"os" "os"
"runtime"
"sort" "sort"
"strconv" "strconv"
"strings" "strings"
@ -503,6 +504,10 @@ func Set(name, value string) error {
// otherwise, the default values of all defined flags in the set. // otherwise, the default values of all defined flags in the set.
func (f *FlagSet) PrintDefaults() { func (f *FlagSet) PrintDefaults() {
writer := tabwriter.NewWriter(f.Out(), 20, 1, 3, ' ', 0) writer := tabwriter.NewWriter(f.Out(), 20, 1, 3, ' ', 0)
var home string
if runtime.GOOS != "windows" {
home = os.Getenv("HOME")
}
f.VisitAll(func(flag *Flag) { f.VisitAll(func(flag *Flag) {
format := " -%s=%s" format := " -%s=%s"
if _, ok := flag.Value.(*stringValue); ok { if _, ok := flag.Value.(*stringValue); ok {
@ -516,7 +521,13 @@ func (f *FlagSet) PrintDefaults() {
} }
} }
if len(names) > 0 { if len(names) > 0 {
fmt.Fprintf(writer, format, strings.Join(names, ", -"), flag.DefValue) val := flag.DefValue
if home != "" && strings.HasPrefix(val, home) {
val = "~" + val[len(home):]
}
fmt.Fprintf(writer, format, strings.Join(names, ", -"), val)
for i, line := range strings.Split(flag.Usage, "\n") { for i, line := range strings.Split(flag.Usage, "\n") {
if i != 0 { if i != 0 {
line = " " + line line = " " + line