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
+5
View File
@@ -5,6 +5,8 @@ import (
"reflect"
"strconv"
"strings"
"github.com/pkg/errors"
)
// Path records the nodes and parsed values from the current command-line.
@@ -504,6 +506,9 @@ func (c *Context) parseFlag(flags []*Flag, match string) (err error) {
c.scan.Pop()
err := flag.Parse(c.scan, c.getValue(flag.Value))
if err != nil {
if e, ok := errors.Cause(err).(*expectedError); ok && e.token.InferredType().IsAny(FlagToken, ShortFlagToken) {
return errors.Errorf("%s; perhaps try %s=%q?", err, flag.ShortSummary(), e.token)
}
return err
}
c.Path = append(c.Path, &Path{Flag: flag})