Produce a more useful error when flag-like values are used for flag values.

eg.  myapp: error: --log-level: expected string value but got "--foo" (long flag); perhaps try --log-level="--foo"?
This commit is contained in:
Alec Thomas
2019-06-21 10:23:09 +10:00
parent 1076f5ee1f
commit 95de7d2f0d
4 changed files with 28 additions and 14 deletions
+4 -2
View File
@@ -7,6 +7,8 @@ import (
"reflect"
"strconv"
"strings"
"github.com/pkg/errors"
)
// A Visitable component in the model.
@@ -289,7 +291,7 @@ func (v *Value) Parse(scan *Scanner, target reflect.Value) (err error) {
if rerr := recover(); rerr != nil {
switch rerr := rerr.(type) {
case Error:
err = fmt.Errorf("%s: %s", v.ShortSummary(), rerr)
err = errors.Wrap(rerr, v.ShortSummary())
default:
panic(fmt.Sprintf("mapper %T failed to apply to %s: %s", v.Mapper, v.Summary(), rerr))
}
@@ -297,7 +299,7 @@ func (v *Value) Parse(scan *Scanner, target reflect.Value) (err error) {
}()
err = v.Mapper.Decode(&DecodeContext{Value: v, Scan: scan}, target)
if err != nil {
return fmt.Errorf("%s: %s", v.ShortSummary(), err)
return errors.Wrap(err, v.ShortSummary())
}
v.Set = true
return nil