diff --git a/apps/interactive-calibration/main.cpp b/apps/interactive-calibration/main.cpp index 1d67514601..c1b4ba2255 100644 --- a/apps/interactive-calibration/main.cpp +++ b/apps/interactive-calibration/main.cpp @@ -122,8 +122,11 @@ int main(int argc, char** argv) std::cout << consoleHelp << std::endl; parametersController paramsController; - if(!paramsController.loadFromParser(parser)) + if(!paramsController.loadFromParser(parser) || !parser.check()) + { + parser.printErrors(); return 0; + } captureParameters capParams = paramsController.getCaptureParameters(); internalParameters intParams = paramsController.getInternalParameters(); diff --git a/modules/core/src/command_line_parser.cpp b/modules/core/src/command_line_parser.cpp index a83cb3166d..12eb2a8a67 100644 --- a/modules/core/src/command_line_parser.cpp +++ b/modules/core/src/command_line_parser.cpp @@ -76,9 +76,13 @@ static bool parse_bool(std::string str) { std::transform(str.begin(), str.end(), str.begin(), details::char_tolower); std::istringstream is(str); - bool b; - is >> (str.size() > 1 ? std::boolalpha : std::noboolalpha) >> b; - return b; + bool value = false; + is >> (str.size() > 1 ? std::boolalpha : std::noboolalpha) >> value; + + if (is.fail()) + CV_Error_(Error::StsBadArg, ("can not convert: [%s] to [bool]", str.c_str())); + + return value; } static void from_str(const String& str, Param type, void* dst) diff --git a/modules/core/test/test_utils.cpp b/modules/core/test/test_utils.cpp index 13720c2b00..6c6e6932d3 100644 --- a/modules/core/test/test_utils.cpp +++ b/modules/core/test/test_utils.cpp @@ -165,6 +165,28 @@ TEST(CommandLineParser, testBoolOption_FalseValues) EXPECT_FALSE(parser.get("n")); } +TEST(CommandLineParser, testBoolOption_InvalidValue) +{ + const char* argv[] = {"", "--info=invalid"}; + const int argc = 2; + cv::CommandLineParser parser(argc, argv, keys); + + parser.get("info"); + + EXPECT_FALSE(parser.check()); +} + +TEST(CommandLineParser, testBoolOption_InvalidNumericValue) +{ + const char* argv[] = {"", "--info=2"}; + const int argc = 2; + cv::CommandLineParser parser(argc, argv, keys); + + parser.get("info"); + + EXPECT_FALSE(parser.check()); +} + static const char * const keys2 = "{ h help | | print help }"