Move validation logic into Argument class itself

This commit is contained in:
Stephan van Veen 2019-05-11 12:36:32 +02:00
parent 7c938b1f2b
commit 603e87ae69

View File

@ -105,6 +105,36 @@ public:
return *this; return *this;
} }
/*
* @throws std::runtime_error if argument values are not valid
*/
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");
}
}
}
else {
// TODO: check if an implicit value was programmed for this argument
}
}
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");
}
}
}
template <typename T> template <typename T>
bool operator!=(const T& aRhs) const { bool operator!=(const T& aRhs) const {
return !(*this == aRhs); return !(*this == aRhs);
@ -602,35 +632,17 @@ class ArgumentParser {
* @throws std::runtime_error in case of any invalid argument * @throws std::runtime_error in case of any invalid argument
*/ */
void parse_args_validate() { void parse_args_validate() {
// Check if all positional arguments are parsed try {
for (const auto& tArgument : mPositionalArguments) { // Check if all positional arguments are parsed
if (tArgument->mValues.size() != tArgument->mNumArgs) { std::for_each(std::begin(mPositionalArguments),
std::stringstream stream; std::end(mPositionalArguments),
stream << "error: " << tArgument->mUsedName << ": expected " std::mem_fn(&Argument::validate));
<< tArgument->mNumArgs << (tArgument->mNumArgs == 1 ? " argument. " : " arguments. ") // Check if all user-provided optional argument values are parsed correctly
<< tArgument->mValues.size() << " provided.\n" << std::endl; std::for_each(std::begin(mOptionalArguments),
throw std::runtime_error(stream.str()); std::end(mOptionalArguments),
} std::mem_fn(&Argument::validate));
} } catch (const std::runtime_error& err) {
throw err;
// Check if all user-provided optional argument values are parsed correctly
for (const auto& tArgument : mOptionalArguments) {
if (tArgument->mIsUsed && tArgument->mNumArgs > 0) {
if (tArgument->mValues.size() != tArgument->mNumArgs) {
// All cool if there's a default value to return
// If no default value, then there's a problem
if (!tArgument->mDefaultValue.has_value()) {
std::stringstream stream;
stream << "error: " << tArgument->mUsedName << ": expected "
<< tArgument->mNumArgs << (tArgument->mNumArgs == 1 ? " argument. " : " arguments. ")
<< tArgument->mValues.size() << " provided.\n" << std::endl;
throw std::runtime_error(stream.str());
}
}
}
else {
// TODO: check if an implicit value was programmed for this argument
}
} }
} }