add ability to combine multiple error strings to Error() function

This commit is contained in:
Aaron Berns 2019-08-20 11:09:53 -04:00
parent ed4ac1876d
commit fffdd82c00

View File

@ -308,12 +308,18 @@ func (e *errRequiredFlags) Error() string {
} }
} }
var allErrors []string
numberOfMissingFlags := len(missingFlagNames) numberOfMissingFlags := len(missingFlagNames)
if numberOfMissingFlags == 1 { if numberOfMissingFlags == 1 {
return fmt.Sprintf("Required flag %q not set", missingFlagNames[0]) allErrors = append(allErrors, fmt.Sprintf("Required flag %q not set", missingFlagNames[0]))
} } else {
joinedMissingFlags := strings.Join(missingFlagNames, ", ") joinedMissingFlags := strings.Join(missingFlagNames, ", ")
return fmt.Sprintf("Required flags %q not set", joinedMissingFlags) allErrors = append(allErrors, fmt.Sprintf("Required flags %q not set", joinedMissingFlags))
}
// handle user defined errors and append
return strings.Join(allErrors, "\n")
} }
func (e *errRequiredFlags) getMissingFlags() map[string]bool { func (e *errRequiredFlags) getMissingFlags() map[string]bool {