Retain entered flag spellings in parser failures
This commit is contained in:
@@ -77,7 +77,7 @@ func (session *Session) Parse(argv []string) commandmeta.Result {
|
||||
}
|
||||
|
||||
remaining := append(commandpath.CloneTokens(unconsumed), tail...)
|
||||
interpreted, spellings := interpretFlagTokens(remaining, selectedNode.Flags, session.flagAliases)
|
||||
interpreted, spellings, interpretationFailure := interpretFlagTokens(remaining, selectedNode.Flags, session.flagAliases)
|
||||
for _, flag := range selectedNode.Flags {
|
||||
if value := selected.Flags().Lookup(flag.Name); value != nil {
|
||||
value.Value.(*rawValue).queueSpellings(spellings[flag.Name])
|
||||
@@ -89,6 +89,12 @@ func (session *Session) Parse(argv []string) commandmeta.Result {
|
||||
if err := selected.ParseFlags(interpreted); err != nil {
|
||||
return failure(result, err.Error())
|
||||
}
|
||||
// Parse the completed prefix first so an earlier backend failure keeps its
|
||||
// precedence over a missing value discovered while interpreting aliases.
|
||||
if interpretationFailure != nil {
|
||||
result.Failure = interpretationFailure
|
||||
return result
|
||||
}
|
||||
operands := selected.Flags().Args()
|
||||
|
||||
result.Operands = append([]string(nil), operands...)
|
||||
@@ -473,10 +479,14 @@ func (v *rawValue) spellingsCopy() []string { return append([]string(nil), v.spe
|
||||
|
||||
func (v *rawValue) lastBool() (bool, error) {
|
||||
var value bool
|
||||
for _, raw := range v.values {
|
||||
for index, raw := range v.values {
|
||||
parsed, err := strconv.ParseBool(raw)
|
||||
if err != nil {
|
||||
return false, fmt.Errorf("invalid boolean value %q for --help", raw)
|
||||
spelling := "--help"
|
||||
if index < len(v.spellings) {
|
||||
spelling = v.spellings[index]
|
||||
}
|
||||
return false, fmt.Errorf("invalid boolean value %q for %s", raw, spelling)
|
||||
}
|
||||
value = parsed
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user