From 8dd508d4b629d5757d02c8f332c1d902ec23d849 Mon Sep 17 00:00:00 2001 From: Stephan van Veen Date: Sat, 11 May 2019 12:41:57 +0200 Subject: [PATCH] Put error message into exception instead of std::cerr --- include/argparse.hpp | 22 +++++++++------------- 1 file changed, 9 insertions(+), 13 deletions(-) diff --git a/include/argparse.hpp b/include/argparse.hpp index f55d141..c8dfdc6 100644 --- a/include/argparse.hpp +++ b/include/argparse.hpp @@ -110,16 +110,11 @@ public: */ void validate() const { if (mIsOptional) { - if (mIsUsed && mNumArgs > 0) { - if (mValues.size() != mNumArgs) { - // All cool if there's a default value to return - // 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; - throw std::runtime_error("wrong number of arguments"); - } - } + if (mIsUsed && mValues.size() != mNumArgs && !mDefaultValue.has_value()) { + std::stringstream stream; + stream << "error: " << mUsedName << ": expected " << mNumArgs << " argument(s). " + << mValues.size() << " provided.\n" << std::endl; + throw std::runtime_error(stream.str()); } else { // TODO: check if an implicit value was programmed for this argument @@ -127,9 +122,10 @@ public: } else { if (mValues.size() != mNumArgs) { - std::cout << "error: " << mUsedName << ": expected " << mNumArgs << " argument(s). " - << mValues.size() << " provided.\n" << std::endl; - throw std::runtime_error("wrong number of arguments"); + std::stringstream stream; + stream << "error: " << mUsedName << ": expected " << mNumArgs << " argument(s). " + << mValues.size() << " provided.\n" << std::endl; + throw std::runtime_error(stream.str()); } } }