mirror of
https://github.com/KeqingMoe/argparse.git
synced 2025-07-04 15:14:39 +00:00
Move validation logic into Argument class itself
This commit is contained in:
parent
7c938b1f2b
commit
603e87ae69
@ -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() {
|
||||||
|
try {
|
||||||
// Check if all positional arguments are parsed
|
// Check if all positional arguments are parsed
|
||||||
for (const auto& tArgument : mPositionalArguments) {
|
std::for_each(std::begin(mPositionalArguments),
|
||||||
if (tArgument->mValues.size() != tArgument->mNumArgs) {
|
std::end(mPositionalArguments),
|
||||||
std::stringstream stream;
|
std::mem_fn(&Argument::validate));
|
||||||
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());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Check if all user-provided optional argument values are parsed correctly
|
// Check if all user-provided optional argument values are parsed correctly
|
||||||
for (const auto& tArgument : mOptionalArguments) {
|
std::for_each(std::begin(mOptionalArguments),
|
||||||
if (tArgument->mIsUsed && tArgument->mNumArgs > 0) {
|
std::end(mOptionalArguments),
|
||||||
if (tArgument->mValues.size() != tArgument->mNumArgs) {
|
std::mem_fn(&Argument::validate));
|
||||||
// All cool if there's a default value to return
|
} catch (const std::runtime_error& err) {
|
||||||
// If no default value, then there's a problem
|
throw err;
|
||||||
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
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user