mirror of
https://github.com/KeqingMoe/argparse.git
synced 2025-07-04 15:14:39 +00:00
Put error message into exception instead of std::cerr
This commit is contained in:
parent
603e87ae69
commit
8dd508d4b6
@ -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
|
|
||||||
if (!mDefaultValue.has_value()) {
|
|
||||||
std::cout << "error: " << mUsedName << ": expected " << mNumArgs << " argument(s). "
|
|
||||||
<< mValues.size() << " provided.\n" << std::endl;
|
<< mValues.size() << " provided.\n" << std::endl;
|
||||||
throw std::runtime_error("wrong number of arguments");
|
throw std::runtime_error(stream.str());
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
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;
|
||||||
|
stream << "error: " << mUsedName << ": expected " << mNumArgs << " argument(s). "
|
||||||
<< mValues.size() << " provided.\n" << std::endl;
|
<< mValues.size() << " provided.\n" << std::endl;
|
||||||
throw std::runtime_error("wrong number of arguments");
|
throw std::runtime_error(stream.str());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user