Put error message into exception instead of std::cerr

This commit is contained in:
Stephan van Veen 2019-05-11 12:41:57 +02:00
parent 603e87ae69
commit 8dd508d4b6

View File

@ -110,16 +110,11 @@ public:
*/ */
void validate() const { void validate() const {
if (mIsOptional) { if (mIsOptional) {
if (mIsUsed && mNumArgs > 0) { if (mIsUsed && mValues.size() != mNumArgs && !mDefaultValue.has_value()) {
if (mValues.size() != mNumArgs) { std::stringstream stream;
// All cool if there's a default value to return stream << "error: " << mUsedName << ": expected " << mNumArgs << " argument(s). "
// If no default value, then there's a problem << mValues.size() << " provided.\n" << std::endl;
if (!mDefaultValue.has_value()) { throw std::runtime_error(stream.str());
std::cout << "error: " << mUsedName << ": expected " << mNumArgs << " argument(s). "
<< mValues.size() << " provided.\n" << std::endl;
throw std::runtime_error("wrong number of arguments");
}
}
} }
else { else {
// TODO: check if an implicit value was programmed for this argument // TODO: check if an implicit value was programmed for this argument
@ -127,9 +122,10 @@ public:
} }
else { else {
if (mValues.size() != mNumArgs) { if (mValues.size() != mNumArgs) {
std::cout << "error: " << mUsedName << ": expected " << mNumArgs << " argument(s). " std::stringstream stream;
<< mValues.size() << " provided.\n" << std::endl; stream << "error: " << mUsedName << ": expected " << mNumArgs << " argument(s). "
throw std::runtime_error("wrong number of arguments"); << mValues.size() << " provided.\n" << std::endl;
throw std::runtime_error(stream.str());
} }
} }
} }