Util: Add type error messages to ArgParser option arguments

This commit is contained in:
Riyyi
2021-09-13 16:19:49 +02:00
parent 648c0f8c85
commit 2ac6d24520
2 changed files with 32 additions and 14 deletions
+31 -14
View File
@@ -40,17 +40,25 @@ void ArgParser::printError(const char* parameter, Error error, bool longName)
else if (error == Error::OptionDoesntAllowArgument) { else if (error == Error::OptionDoesntAllowArgument) {
printf("%s: option '--%s' doesn't allow an argument\n", m_name, parameter); printf("%s: option '--%s' doesn't allow an argument\n", m_name, parameter);
} }
else if (error == Error::ArgumentExtraOperand) {
printf("%s: extra operand '%s'\n", m_name, parameter);
}
else if (error == Error::OptionRequiresArgument) { else if (error == Error::OptionRequiresArgument) {
if (longName) { if (longName) {
printf("%s: option '--%s' requires an argument", m_name, parameter); printf("%s: option '--%s' requires an argument\n", m_name, parameter);
} }
else { else {
printf("%s: option requires an argument -- '%s'\n", m_name, parameter); printf("%s: option requires an argument -- '%s'\n", m_name, parameter);
} }
} }
else if (error == Error::OptionInvalidArgumentType) {
if (longName) {
printf("%s: option '--%s' invalid type\n", m_name, parameter);
}
else {
printf("%s: option invalid type -- '%s'\n", m_name, parameter);
}
}
else if (error == Error::ArgumentExtraOperand) {
printf("%s: extra operand '%s'\n", m_name, parameter);
}
else if (error == Error::ArgumentInvalidType) { else if (error == Error::ArgumentInvalidType) {
printf("%s: invalid argument type '%s'\n", m_name, parameter); printf("%s: invalid argument type '%s'\n", m_name, parameter);
} }
@@ -80,9 +88,8 @@ bool ArgParser::parseShortOption(std::string_view option, std::string_view next)
if (foundOption == m_options.cend()) { if (foundOption == m_options.cend()) {
printError(c, Error::OptionInvalid); printError(c, Error::OptionInvalid);
result = false;
if (m_exitOnFirstError) { if (m_exitOnFirstError) {
return result; return false;
} }
} }
else if (foundOption->requiresArgument == Required::No) { else if (foundOption->requiresArgument == Required::No) {
@@ -94,27 +101,25 @@ bool ArgParser::parseShortOption(std::string_view option, std::string_view next)
if (value.empty() && next.empty()) { if (value.empty() && next.empty()) {
foundOption->error = Error::OptionRequiresArgument; foundOption->error = Error::OptionRequiresArgument;
printError(c, Error::OptionRequiresArgument); printError(c, Error::OptionRequiresArgument);
result = false; result = false;
if (m_exitOnFirstError) {
return result;
}
} }
else if (!value.empty()) { else if (!value.empty()) {
result = foundOption->acceptValue(value.data()); result = foundOption->acceptValue(value.data());
if (!result) {
printError(c, Error::OptionInvalidArgumentType);
}
} }
else if (next[0] == '-') { else if (next[0] == '-') {
foundOption->error = Error::OptionRequiresArgument; foundOption->error = Error::OptionRequiresArgument;
printError(c, Error::OptionRequiresArgument); printError(c, Error::OptionRequiresArgument);
result = false; result = false;
if (m_exitOnFirstError) {
return result;
}
} }
else { else {
result = foundOption->acceptValue(next.data()); result = foundOption->acceptValue(next.data());
m_optionIndex++; m_optionIndex++;
if (!result) {
printError(c, Error::OptionInvalidArgumentType);
}
} }
break; break;
@@ -123,6 +128,9 @@ bool ArgParser::parseShortOption(std::string_view option, std::string_view next)
value = option.substr(i + 1); value = option.substr(i + 1);
if (!value.empty()) { if (!value.empty()) {
result = foundOption->acceptValue(value.data()); result = foundOption->acceptValue(value.data());
if (!result) {
printError(c, Error::OptionInvalidArgumentType);
}
break; break;
} }
} }
@@ -165,9 +173,15 @@ bool ArgParser::parseLongOption(std::string_view option, std::string_view next)
} }
else if (foundOption->requiresArgument == Required::Yes) { else if (foundOption->requiresArgument == Required::Yes) {
result = foundOption->acceptValue(value.data()); result = foundOption->acceptValue(value.data());
if (!result) {
printError(name.data(), Error::OptionInvalidArgumentType);
}
} }
else if (foundOption->requiresArgument == Required::Optional) { else if (foundOption->requiresArgument == Required::Optional) {
result = foundOption->acceptValue(value.data()); result = foundOption->acceptValue(value.data());
if (!result) {
printError(name.data(), Error::OptionInvalidArgumentType);
}
} }
} }
else if (!next.empty() && foundOption->requiresArgument == Required::Yes) { else if (!next.empty() && foundOption->requiresArgument == Required::Yes) {
@@ -180,6 +194,9 @@ bool ArgParser::parseLongOption(std::string_view option, std::string_view next)
else { else {
result = foundOption->acceptValue(next.data()); result = foundOption->acceptValue(next.data());
m_optionIndex++; m_optionIndex++;
if (!result) {
printError(name.data(), Error::OptionInvalidArgumentType);
}
} }
} }
else if (foundOption->requiresArgument == Required::Yes) { else if (foundOption->requiresArgument == Required::Yes) {
+1
View File
@@ -28,6 +28,7 @@ public:
OptionUnrecognized, // For long options OptionUnrecognized, // For long options
OptionDoesntAllowArgument, OptionDoesntAllowArgument,
OptionRequiresArgument, OptionRequiresArgument,
OptionInvalidArgumentType,
ArgumentExtraOperand, ArgumentExtraOperand,
ArgumentInvalidType, ArgumentInvalidType,
}; };