Commit e5ece03d authored by Alexander Alekhin's avatar Alexander Alekhin

Merge pull request #5382 from paroj:parserthrow

parents 6f04385a a3888065
...@@ -37,7 +37,7 @@ struct CommandLineParser::Impl ...@@ -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 ) if( type == Param::INT )
return "int"; return "int";
...@@ -78,14 +78,11 @@ static void from_str(const String& str, int type, void* dst) ...@@ -78,14 +78,11 @@ static void from_str(const String& str, int type, void* dst)
else if( type == Param::STRING ) else if( type == Param::STRING )
*(String*)dst = str; *(String*)dst = str;
else else
throw cv::Exception(CV_StsBadArg, "unknown/unsupported parameter type", "", __FILE__, __LINE__); CV_Error(Error::StsBadArg, "unknown/unsupported parameter type");
if (ss.fail()) if (ss.fail())
{ {
String err_msg = "can not convert: [" + str + CV_Error_(Error::StsBadArg, ("can not convert: [%s] to [%s]", str.c_str(), get_type_name(type)));
+ "] to [" + get_type_name(type) + "]";
throw cv::Exception(CV_StsBadArg, err_msg, "", __FILE__, __LINE__);
} }
} }
...@@ -103,23 +100,27 @@ void CommandLineParser::getByName(const String& name, bool space_delete, int typ ...@@ -103,23 +100,27 @@ void CommandLineParser::getByName(const String& name, bool space_delete, int typ
if (space_delete) if (space_delete)
v = impl->cat_string(v); v = impl->cat_string(v);
// it is an error if we just got the default value here // the key was neither specified nor has it a default value
if (v.empty() && type != Param::STRING) if(v.empty() && type != Param::STRING) {
break; impl->error = true;
impl->error_message = impl->error_message + "Missing parameter: '" + name + "'\n";
return;
}
from_str(v, type, dst); from_str(v, type, dst);
return; return;
} }
} }
} }
impl->error = true;
impl->error_message = impl->error_message + "Missing parameter: '" + name + "'\n";
} }
catch (Exception& e) catch (Exception& e)
{ {
impl->error = true; impl->error = true;
impl->error_message = impl->error_message + "Parameter '"+ name + "': " + e.err + "\n"; 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* ...@@ -134,22 +135,25 @@ void CommandLineParser::getByIndex(int index, bool space_delete, int type, void*
String v = impl->data[i].def_value; String v = impl->data[i].def_value;
if (space_delete == true) v = impl->cat_string(v); if (space_delete == true) v = impl->cat_string(v);
// it is an error if we just got the default value here // the key was neither specified nor has it a default value
if(v.empty()) if(v.empty() && type != Param::STRING) {
break; impl->error = true;
impl->error_message = impl->error_message + format("Missing parameter #%d\n", index);
return;
}
from_str(v, type, dst); from_str(v, type, dst);
return; return;
} }
} }
impl->error = true;
impl->error_message = impl->error_message + "Missing parameter #" + format("%d", index) + "\n";
} }
catch(Exception& e) catch(Exception& e)
{ {
impl->error = true; impl->error = true;
impl->error_message = impl->error_message + format("Parameter #%d: ", index) + e.err + "\n"; 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) static bool cmp_params(const CommandLineParserParams & p1, const CommandLineParserParams & p2)
...@@ -340,6 +344,8 @@ bool CommandLineParser::has(const String& name) const ...@@ -340,6 +344,8 @@ bool CommandLineParser::has(const String& name) const
} }
} }
} }
CV_Error_(Error::StsBadArg, ("undeclared key '%s' requested", name.c_str()));
return false; return false;
} }
......
...@@ -15,6 +15,18 @@ static const char * const keys = ...@@ -15,6 +15,18 @@ static const char * const keys =
"{ n unused | | dummy }" "{ n unused | | dummy }"
; ;
TEST(CommandLineParser, testFailure)
{
const char* argv[] = {"<bin>", "-q"};
const int argc = 2;
cv::CommandLineParser parser(argc, argv, keys);
EXPECT_ANY_THROW(parser.has("q"));
EXPECT_ANY_THROW(parser.get<bool>("q"));
EXPECT_ANY_THROW(parser.get<bool>(0));
parser.get<bool>("h");
EXPECT_FALSE(parser.check());
}
TEST(CommandLineParser, testHas_noValues) TEST(CommandLineParser, testHas_noValues)
{ {
const char* argv[] = {"<bin>", "-h", "--info"}; const char* argv[] = {"<bin>", "-h", "--info"};
...@@ -26,7 +38,6 @@ TEST(CommandLineParser, testHas_noValues) ...@@ -26,7 +38,6 @@ TEST(CommandLineParser, testHas_noValues)
EXPECT_TRUE(parser.has("i")); EXPECT_TRUE(parser.has("i"));
EXPECT_FALSE(parser.has("n")); EXPECT_FALSE(parser.has("n"));
EXPECT_FALSE(parser.has("unused")); EXPECT_FALSE(parser.has("unused"));
EXPECT_FALSE(parser.has("q")); // TODO Throw ???
} }
TEST(CommandLineParser, testHas_TrueValues) TEST(CommandLineParser, testHas_TrueValues)
{ {
...@@ -39,7 +50,6 @@ TEST(CommandLineParser, testHas_TrueValues) ...@@ -39,7 +50,6 @@ TEST(CommandLineParser, testHas_TrueValues)
EXPECT_TRUE(parser.has("i")); EXPECT_TRUE(parser.has("i"));
EXPECT_FALSE(parser.has("n")); EXPECT_FALSE(parser.has("n"));
EXPECT_FALSE(parser.has("unused")); EXPECT_FALSE(parser.has("unused"));
EXPECT_FALSE(parser.has("q")); // TODO Throw ???
} }
TEST(CommandLineParser, testHas_TrueValues1) TEST(CommandLineParser, testHas_TrueValues1)
{ {
...@@ -52,7 +62,6 @@ TEST(CommandLineParser, testHas_TrueValues1) ...@@ -52,7 +62,6 @@ TEST(CommandLineParser, testHas_TrueValues1)
EXPECT_TRUE(parser.has("i")); EXPECT_TRUE(parser.has("i"));
EXPECT_FALSE(parser.has("n")); EXPECT_FALSE(parser.has("n"));
EXPECT_FALSE(parser.has("unused")); EXPECT_FALSE(parser.has("unused"));
EXPECT_FALSE(parser.has("q")); // TODO Throw ???
} }
TEST(CommandLineParser, testHas_FalseValues0) TEST(CommandLineParser, testHas_FalseValues0)
{ {
...@@ -65,7 +74,6 @@ TEST(CommandLineParser, testHas_FalseValues0) ...@@ -65,7 +74,6 @@ TEST(CommandLineParser, testHas_FalseValues0)
EXPECT_TRUE(parser.has("i")); EXPECT_TRUE(parser.has("i"));
EXPECT_FALSE(parser.has("n")); EXPECT_FALSE(parser.has("n"));
EXPECT_FALSE(parser.has("unused")); EXPECT_FALSE(parser.has("unused"));
EXPECT_FALSE(parser.has("q")); // TODO Throw ???
} }
TEST(CommandLineParser, testBoolOption_noArgs) TEST(CommandLineParser, testBoolOption_noArgs)
...@@ -129,12 +137,14 @@ TEST(CommandLineParser, testPositional_noArgs) ...@@ -129,12 +137,14 @@ TEST(CommandLineParser, testPositional_noArgs)
const char* argv[] = {"<bin>"}; const char* argv[] = {"<bin>"};
const int argc = 1; const int argc = 1;
cv::CommandLineParser parser(argc, argv, keys2); cv::CommandLineParser parser(argc, argv, keys2);
//EXPECT_FALSE(parser.has("arg1")); EXPECT_TRUE(parser.has("@arg1"));
//EXPECT_FALSE(parser.has("arg2")); EXPECT_FALSE(parser.has("@arg2"));
//EXPECT_EQ("default1", parser.get<String>("arg1")); EXPECT_EQ("default1", parser.get<String>("@arg1"));
EXPECT_EQ("default1", parser.get<String>(0)); EXPECT_EQ("default1", parser.get<String>(0));
//??? EXPECT_ANY_THROW(parser.get<String>("arg2"));
//??? EXPECT_ANY_THROW(parser.get<String>(1)); parser.get<String>("@arg2");
parser.get<String>(1);
EXPECT_TRUE(parser.check());
} }
TEST(CommandLineParser, testPositional_default) TEST(CommandLineParser, testPositional_default)
...@@ -142,10 +152,10 @@ TEST(CommandLineParser, testPositional_default) ...@@ -142,10 +152,10 @@ TEST(CommandLineParser, testPositional_default)
const char* argv[] = {"<bin>", "test1", "test2"}; const char* argv[] = {"<bin>", "test1", "test2"};
const int argc = 3; const int argc = 3;
cv::CommandLineParser parser(argc, argv, keys2); cv::CommandLineParser parser(argc, argv, keys2);
//EXPECT_TRUE(parser.has("arg1")); EXPECT_TRUE(parser.has("@arg1"));
//EXPECT_TRUE(parser.has("arg2")); EXPECT_TRUE(parser.has("@arg2"));
//EXPECT_EQ("test1", parser.get<String>("arg1")); EXPECT_EQ("test1", parser.get<String>("@arg1"));
//EXPECT_EQ("test2", parser.get<String>("arg2")); EXPECT_EQ("test2", parser.get<String>("@arg2"));
EXPECT_EQ("test1", parser.get<String>(0)); EXPECT_EQ("test1", parser.get<String>(0));
EXPECT_EQ("test2", parser.get<String>(1)); EXPECT_EQ("test2", parser.get<String>(1));
} }
...@@ -155,10 +165,10 @@ TEST(CommandLineParser, testPositional_withFlagsBefore) ...@@ -155,10 +165,10 @@ TEST(CommandLineParser, testPositional_withFlagsBefore)
const char* argv[] = {"<bin>", "-h", "test1", "test2"}; const char* argv[] = {"<bin>", "-h", "test1", "test2"};
const int argc = 4; const int argc = 4;
cv::CommandLineParser parser(argc, argv, keys2); cv::CommandLineParser parser(argc, argv, keys2);
//EXPECT_TRUE(parser.has("arg1")); EXPECT_TRUE(parser.has("@arg1"));
//EXPECT_TRUE(parser.has("arg2")); EXPECT_TRUE(parser.has("@arg2"));
//EXPECT_EQ("test1", parser.get<String>("arg1")); EXPECT_EQ("test1", parser.get<String>("@arg1"));
//EXPECT_EQ("test2", parser.get<String>("arg2")); EXPECT_EQ("test2", parser.get<String>("@arg2"));
EXPECT_EQ("test1", parser.get<String>(0)); EXPECT_EQ("test1", parser.get<String>(0));
EXPECT_EQ("test2", parser.get<String>(1)); EXPECT_EQ("test2", parser.get<String>(1));
} }
...@@ -168,10 +178,10 @@ TEST(CommandLineParser, testPositional_withFlagsAfter) ...@@ -168,10 +178,10 @@ TEST(CommandLineParser, testPositional_withFlagsAfter)
const char* argv[] = {"<bin>", "test1", "test2", "-h"}; const char* argv[] = {"<bin>", "test1", "test2", "-h"};
const int argc = 4; const int argc = 4;
cv::CommandLineParser parser(argc, argv, keys2); cv::CommandLineParser parser(argc, argv, keys2);
//EXPECT_TRUE(parser.has("arg1")); EXPECT_TRUE(parser.has("@arg1"));
//EXPECT_TRUE(parser.has("arg2")); EXPECT_TRUE(parser.has("@arg2"));
//EXPECT_EQ("test1", parser.get<String>("arg1")); EXPECT_EQ("test1", parser.get<String>("@arg1"));
//EXPECT_EQ("test2", parser.get<String>("arg2")); EXPECT_EQ("test2", parser.get<String>("@arg2"));
EXPECT_EQ("test1", parser.get<String>(0)); EXPECT_EQ("test1", parser.get<String>(0));
EXPECT_EQ("test2", parser.get<String>(1)); EXPECT_EQ("test2", parser.get<String>(1));
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment