diff --git a/modules/core/src/command_line_parser.cpp b/modules/core/src/command_line_parser.cpp index 967095941..4ef4f7dee 100644 --- a/modules/core/src/command_line_parser.cpp +++ b/modules/core/src/command_line_parser.cpp @@ -37,7 +37,7 @@ struct CommandLineParser::Impl }; -static String get_type_name(int type) +static const char* get_type_name(int type) { if( type == Param::INT ) return "int"; @@ -78,14 +78,11 @@ static void from_str(const String& str, int type, void* dst) else if( type == Param::STRING ) *(String*)dst = str; else - throw cv::Exception(CV_StsBadArg, "unknown/unsupported parameter type", "", __FILE__, __LINE__); + CV_Error(Error::StsBadArg, "unknown/unsupported parameter type"); if (ss.fail()) { - String err_msg = "can not convert: [" + str + - + "] to [" + get_type_name(type) + "]"; - - throw cv::Exception(CV_StsBadArg, err_msg, "", __FILE__, __LINE__); + CV_Error_(Error::StsBadArg, ("can not convert: [%s] to [%s]", str.c_str(), get_type_name(type))); } } @@ -103,23 +100,27 @@ void CommandLineParser::getByName(const String& name, bool space_delete, int typ if (space_delete) v = impl->cat_string(v); - // it is an error if we just got the default value here - if (v.empty() && type != Param::STRING) - break; + // the key was neither specified nor has it a default value + if(v.empty() && type != Param::STRING) { + impl->error = true; + impl->error_message = impl->error_message + "Missing parameter: '" + name + "'\n"; + return; + } from_str(v, type, dst); return; } } } - impl->error = true; - impl->error_message = impl->error_message + "Missing parameter: '" + name + "'\n"; } catch (Exception& e) { impl->error = true; impl->error_message = impl->error_message + "Parameter '"+ name + "': " + e.err + "\n"; + return; } + + CV_Error_(Error::StsBadArg, ("undeclared key '%s' requested", name.c_str())); } @@ -134,22 +135,25 @@ void CommandLineParser::getByIndex(int index, bool space_delete, int type, void* String v = impl->data[i].def_value; if (space_delete == true) v = impl->cat_string(v); - // it is an error if we just got the default value here - if(v.empty()) - break; - + // the key was neither specified nor has it a default value + if(v.empty() && type != Param::STRING) { + impl->error = true; + impl->error_message = impl->error_message + format("Missing parameter #%d\n", index); + return; + } from_str(v, type, dst); return; } } - impl->error = true; - impl->error_message = impl->error_message + "Missing parameter #" + format("%d", index) + "\n"; } catch(Exception& e) { impl->error = true; impl->error_message = impl->error_message + format("Parameter #%d: ", index) + e.err + "\n"; + return; } + + CV_Error_(Error::StsBadArg, ("undeclared position %d requested", index)); } static bool cmp_params(const CommandLineParserParams & p1, const CommandLineParserParams & p2) @@ -340,6 +344,8 @@ bool CommandLineParser::has(const String& name) const } } } + + CV_Error_(Error::StsBadArg, ("undeclared key '%s' requested", name.c_str())); return false; } diff --git a/modules/core/test/test_utils.cpp b/modules/core/test/test_utils.cpp index f69a063eb..86387eb9d 100644 --- a/modules/core/test/test_utils.cpp +++ b/modules/core/test/test_utils.cpp @@ -15,6 +15,18 @@ static const char * const keys = "{ n unused | | dummy }" ; +TEST(CommandLineParser, testFailure) +{ + const char* argv[] = {"", "-q"}; + const int argc = 2; + cv::CommandLineParser parser(argc, argv, keys); + EXPECT_ANY_THROW(parser.has("q")); + EXPECT_ANY_THROW(parser.get("q")); + EXPECT_ANY_THROW(parser.get(0)); + + parser.get("h"); + EXPECT_FALSE(parser.check()); +} TEST(CommandLineParser, testHas_noValues) { const char* argv[] = {"", "-h", "--info"}; @@ -26,7 +38,6 @@ TEST(CommandLineParser, testHas_noValues) EXPECT_TRUE(parser.has("i")); EXPECT_FALSE(parser.has("n")); EXPECT_FALSE(parser.has("unused")); - EXPECT_FALSE(parser.has("q")); // TODO Throw ??? } TEST(CommandLineParser, testHas_TrueValues) { @@ -39,7 +50,6 @@ TEST(CommandLineParser, testHas_TrueValues) EXPECT_TRUE(parser.has("i")); EXPECT_FALSE(parser.has("n")); EXPECT_FALSE(parser.has("unused")); - EXPECT_FALSE(parser.has("q")); // TODO Throw ??? } TEST(CommandLineParser, testHas_TrueValues1) { @@ -52,7 +62,6 @@ TEST(CommandLineParser, testHas_TrueValues1) EXPECT_TRUE(parser.has("i")); EXPECT_FALSE(parser.has("n")); EXPECT_FALSE(parser.has("unused")); - EXPECT_FALSE(parser.has("q")); // TODO Throw ??? } TEST(CommandLineParser, testHas_FalseValues0) { @@ -65,7 +74,6 @@ TEST(CommandLineParser, testHas_FalseValues0) EXPECT_TRUE(parser.has("i")); EXPECT_FALSE(parser.has("n")); EXPECT_FALSE(parser.has("unused")); - EXPECT_FALSE(parser.has("q")); // TODO Throw ??? } TEST(CommandLineParser, testBoolOption_noArgs)