summaryrefslogtreecommitdiff
path: root/usage.go
diff options
context:
space:
mode:
authorAlex Flint <[email protected]>2019-10-21 23:40:36 -0700
committerGitHub <[email protected]>2019-10-21 23:40:36 -0700
commitc0c7a3ba8a1854cd85e65cca4f0e2028698a0738 (patch)
tree4f107812eb75ca8fc0c9f11ca199205b9ef31cd9 /usage.go
parent873f3c2cf4ec11ac4cab84a6ebaf61f695ab8b68 (diff)
parente0fc08f7ad001371541770efcc43cf840288fee8 (diff)
Merge pull request #91 from alexflint/defaultsv1.2.0
Allow default values in struct tags
Diffstat (limited to 'usage.go')
-rw-r--r--usage.go36
1 files changed, 6 insertions, 30 deletions
diff --git a/usage.go b/usage.go
index 33b6184..43db703 100644
--- a/usage.go
+++ b/usage.go
@@ -1,11 +1,9 @@
package arg
import (
- "encoding"
"fmt"
"io"
"os"
- "reflect"
"strings"
)
@@ -94,7 +92,7 @@ func (p *Parser) writeUsageForCommand(w io.Writer, cmd *command) {
fmt.Fprint(w, "\n")
}
-func printTwoCols(w io.Writer, left, help string, defaultVal *string) {
+func printTwoCols(w io.Writer, left, help string, defaultVal string) {
lhs := " " + left
fmt.Fprint(w, lhs)
if help != "" {
@@ -105,8 +103,8 @@ func printTwoCols(w io.Writer, left, help string, defaultVal *string) {
}
fmt.Fprint(w, help)
}
- if defaultVal != nil {
- fmt.Fprintf(w, " [default: %s]", *defaultVal)
+ if defaultVal != "" {
+ fmt.Fprintf(w, " [default: %s]", defaultVal)
}
fmt.Fprint(w, "\n")
}
@@ -136,7 +134,7 @@ func (p *Parser) writeHelpForCommand(w io.Writer, cmd *command) {
if len(positionals) > 0 {
fmt.Fprint(w, "\nPositional arguments:\n")
for _, spec := range positionals {
- printTwoCols(w, strings.ToUpper(spec.long), spec.help, nil)
+ printTwoCols(w, strings.ToUpper(spec.long), spec.help, "")
}
}
@@ -165,7 +163,7 @@ func (p *Parser) writeHelpForCommand(w io.Writer, cmd *command) {
if len(cmd.subcommands) > 0 {
fmt.Fprint(w, "\nCommands:\n")
for _, subcmd := range cmd.subcommands {
- printTwoCols(w, subcmd.name, subcmd.help, nil)
+ printTwoCols(w, subcmd.name, subcmd.help, "")
}
}
}
@@ -175,29 +173,7 @@ func (p *Parser) printOption(w io.Writer, spec *spec) {
if spec.short != "" {
left += ", " + synopsis(spec, "-"+spec.short)
}
-
- // If spec.dest is not the zero value then a default value has been added.
- var v reflect.Value
- if len(spec.dest.fields) > 0 {
- v = p.val(spec.dest)
- }
-
- var defaultVal *string
- 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() {
- if scalar, ok := v.Interface().(encoding.TextMarshaler); ok {
- if value, err := scalar.MarshalText(); err != nil {
- defaultVal = ptrTo(fmt.Sprintf("error: %v", err))
- } else {
- defaultVal = ptrTo(fmt.Sprintf("%v", string(value)))
- }
- } else {
- defaultVal = ptrTo(fmt.Sprintf("%v", v))
- }
- }
- }
- printTwoCols(w, left, spec.help, defaultVal)
+ printTwoCols(w, left, spec.help, spec.defaultVal)
}
func synopsis(spec *spec, form string) string {