diff --git a/mflag/flag.go b/mflag/flag.go index 94cb5b7..cbdd5ea 100644 --- a/mflag/flag.go +++ b/mflag/flag.go @@ -86,6 +86,7 @@ import ( "fmt" "io" "os" + "runtime" "sort" "strconv" "strings" @@ -503,6 +504,10 @@ func Set(name, value string) error { // otherwise, the default values of all defined flags in the set. func (f *FlagSet) PrintDefaults() { 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) { format := " -%s=%s" if _, ok := flag.Value.(*stringValue); ok { @@ -516,7 +521,13 @@ func (f *FlagSet) PrintDefaults() { } } 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") { if i != 0 { line = " " + line