From 2ac6d24520fb1c4bc9ff86ce9767e60fc7ff97fb Mon Sep 17 00:00:00 2001 From: Riyyi Date: Mon, 13 Sep 2021 16:11:12 +0200 Subject: [PATCH] Util: Add type error messages to ArgParser option arguments --- src/util/argparser.cpp | 45 +++++++++++++++++++++++++++++------------- src/util/argparser.h | 1 + 2 files changed, 32 insertions(+), 14 deletions(-) diff --git a/src/util/argparser.cpp b/src/util/argparser.cpp index 5392328..53c19b6 100644 --- a/src/util/argparser.cpp +++ b/src/util/argparser.cpp @@ -40,17 +40,25 @@ void ArgParser::printError(const char* parameter, Error error, bool longName) else if (error == Error::OptionDoesntAllowArgument) { 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) { if (longName) { - printf("%s: option '--%s' requires an argument", m_name, parameter); + printf("%s: option '--%s' requires an argument\n", m_name, parameter); } else { 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) { 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()) { printError(c, Error::OptionInvalid); - result = false; if (m_exitOnFirstError) { - return result; + return false; } } 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()) { foundOption->error = Error::OptionRequiresArgument; printError(c, Error::OptionRequiresArgument); - result = false; - if (m_exitOnFirstError) { - return result; - } } else if (!value.empty()) { result = foundOption->acceptValue(value.data()); + if (!result) { + printError(c, Error::OptionInvalidArgumentType); + } } else if (next[0] == '-') { foundOption->error = Error::OptionRequiresArgument; printError(c, Error::OptionRequiresArgument); - result = false; - if (m_exitOnFirstError) { - return result; - } } else { result = foundOption->acceptValue(next.data()); m_optionIndex++; + if (!result) { + printError(c, Error::OptionInvalidArgumentType); + } } break; @@ -123,6 +128,9 @@ bool ArgParser::parseShortOption(std::string_view option, std::string_view next) value = option.substr(i + 1); if (!value.empty()) { result = foundOption->acceptValue(value.data()); + if (!result) { + printError(c, Error::OptionInvalidArgumentType); + } break; } } @@ -165,9 +173,15 @@ bool ArgParser::parseLongOption(std::string_view option, std::string_view next) } else if (foundOption->requiresArgument == Required::Yes) { result = foundOption->acceptValue(value.data()); + if (!result) { + printError(name.data(), Error::OptionInvalidArgumentType); + } } else if (foundOption->requiresArgument == Required::Optional) { result = foundOption->acceptValue(value.data()); + if (!result) { + printError(name.data(), Error::OptionInvalidArgumentType); + } } } else if (!next.empty() && foundOption->requiresArgument == Required::Yes) { @@ -180,6 +194,9 @@ bool ArgParser::parseLongOption(std::string_view option, std::string_view next) else { result = foundOption->acceptValue(next.data()); m_optionIndex++; + if (!result) { + printError(name.data(), Error::OptionInvalidArgumentType); + } } } else if (foundOption->requiresArgument == Required::Yes) { diff --git a/src/util/argparser.h b/src/util/argparser.h index 3518c5d..1e08d65 100644 --- a/src/util/argparser.h +++ b/src/util/argparser.h @@ -28,6 +28,7 @@ public: OptionUnrecognized, // For long options OptionDoesntAllowArgument, OptionRequiresArgument, + OptionInvalidArgumentType, ArgumentExtraOperand, ArgumentInvalidType, };