diff options
| author | Wlazlo, Matt <[email protected]> | 2018-04-13 14:46:24 +1000 |
|---|---|---|
| committer | Wlazlo, Matt <[email protected]> | 2018-04-13 14:46:24 +1000 |
| commit | d4cc703210ff08d7bc4f73bfbec1e51eda29a5a4 (patch) | |
| tree | 3e554efc7cbda3dd03ae15a73bb4a54782fc23fb /usage.go | |
| parent | 0cc8e30fd64c8c71d094be299ad424da93ef9aed (diff) | |
Custom parsers implementing encoding.TextMarshaler() can have default values printed via --help
Diffstat (limited to 'usage.go')
| -rw-r--r-- | usage.go | 11 |
1 files changed, 10 insertions, 1 deletions
@@ -6,6 +6,7 @@ import ( "os" "reflect" "strings" + "encoding" ) // the width of the left column @@ -134,7 +135,15 @@ func printOption(w io.Writer, spec *spec) { if v.IsValid() { z := reflect.Zero(v.Type()) if (v.Type().Comparable() && z.Type().Comparable() && v.Interface() != z.Interface()) || v.Kind() == reflect.Slice && !v.IsNil() { - fmt.Fprintf(w, " [default: %v]", v) + if scalar, ok := v.Interface().(encoding.TextMarshaler); ok { + if value, err := scalar.MarshalText(); err != nil { + fmt.Fprintf(w, " [default: error: %v]", err) + } else { + fmt.Fprintf(w, " [default: %v]", string(value)) + } + } else { + fmt.Fprintf(w, " [default: %v]", v) + } } } fmt.Fprint(w, "\n") |
