summaryrefslogtreecommitdiff
path: root/usage.go
diff options
context:
space:
mode:
authorWlazlo, Matt <[email protected]>2018-04-13 14:46:24 +1000
committerWlazlo, Matt <[email protected]>2018-04-13 14:46:24 +1000
commitd4cc703210ff08d7bc4f73bfbec1e51eda29a5a4 (patch)
tree3e554efc7cbda3dd03ae15a73bb4a54782fc23fb /usage.go
parent0cc8e30fd64c8c71d094be299ad424da93ef9aed (diff)
Custom parsers implementing encoding.TextMarshaler() can have default values printed via --help
Diffstat (limited to 'usage.go')
-rw-r--r--usage.go11
1 files changed, 10 insertions, 1 deletions
diff --git a/usage.go b/usage.go
index 4652b36..656ee9a 100644
--- a/usage.go
+++ b/usage.go
@@ -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")