Merge pull request #9428 from csukuangfj:fix-commandline-parser

pull/9451/head
Alexander Alekhin 8 years ago
commit a893b147dc
  1. 4
      modules/core/include/opencv2/core/utility.hpp
  2. 15
      modules/core/src/command_line_parser.cpp
  3. 48
      modules/core/test/test_utils.cpp

@ -822,7 +822,7 @@ public:
This method returns the path to the executable from the command line (`argv[0]`). This method returns the path to the executable from the command line (`argv[0]`).
For example, if the application has been started with such command: For example, if the application has been started with such a command:
@code{.sh} @code{.sh}
$ ./bin/my-executable $ ./bin/my-executable
@endcode @endcode
@ -928,7 +928,7 @@ public:
*/ */
void printMessage() const; void printMessage() const;
/** @brief Print list of errors occured /** @brief Print list of errors occurred
@sa check @sa check
*/ */

@ -69,6 +69,15 @@ static const char* get_type_name(int type)
return "unknown"; return "unknown";
} }
static bool parse_bool(std::string str)
{
std::transform(str.begin(), str.end(), str.begin(), ::tolower);
std::istringstream is(str);
bool b;
is >> (str.size() > 1 ? std::boolalpha : std::noboolalpha) >> b;
return b;
}
static void from_str(const String& str, int type, void* dst) static void from_str(const String& str, int type, void* dst)
{ {
std::stringstream ss(str.c_str()); std::stringstream ss(str.c_str());
@ -78,7 +87,7 @@ static void from_str(const String& str, int type, void* dst)
{ {
std::string temp; std::string temp;
ss >> temp; ss >> temp;
*(bool*) dst = temp == "true"; *(bool*) dst = parse_bool(temp);
} }
else if( type == Param::UNSIGNED_INT ) else if( type == Param::UNSIGNED_INT )
ss >> *(unsigned*)dst; ss >> *(unsigned*)dst;
@ -113,7 +122,7 @@ void CommandLineParser::getByName(const String& name, bool space_delete, int typ
if (space_delete) if (space_delete)
v = cat_string(v); v = cat_string(v);
// the key was neither specified nor has it a default value // the key was neither specified nor has a default value
if((v.empty() && type != Param::STRING) || v == noneValue) { if((v.empty() && type != Param::STRING) || v == noneValue) {
impl->error = true; impl->error = true;
impl->error_message = impl->error_message + "Missing parameter: '" + name + "'\n"; impl->error_message = impl->error_message + "Missing parameter: '" + name + "'\n";
@ -148,7 +157,7 @@ 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 = cat_string(v); if (space_delete == true) v = cat_string(v);
// the key was neither specified nor has it a default value // the key was neither specified nor has a default value
if((v.empty() && type != Param::STRING) || v == noneValue) { if((v.empty() && type != Param::STRING) || v == noneValue) {
impl->error = true; impl->error = true;
impl->error_message = impl->error_message + format("Missing parameter #%d\n", index); impl->error_message = impl->error_message + format("Missing parameter #%d\n", index);

@ -35,8 +35,14 @@ TEST(CommandLineParser, testHas_noValues)
cv::CommandLineParser parser(argc, argv, keys); cv::CommandLineParser parser(argc, argv, keys);
EXPECT_TRUE(parser.has("help")); EXPECT_TRUE(parser.has("help"));
EXPECT_TRUE(parser.has("h")); EXPECT_TRUE(parser.has("h"));
EXPECT_TRUE(parser.get<bool>("help"));
EXPECT_TRUE(parser.get<bool>("h"));
EXPECT_TRUE(parser.has("info")); EXPECT_TRUE(parser.has("info"));
EXPECT_TRUE(parser.has("i")); EXPECT_TRUE(parser.has("i"));
EXPECT_TRUE(parser.get<bool>("info"));
EXPECT_TRUE(parser.get<bool>("i"));
EXPECT_TRUE(parser.get<bool>("true"));
EXPECT_TRUE(parser.get<bool>("t"));
EXPECT_FALSE(parser.has("n")); EXPECT_FALSE(parser.has("n"));
EXPECT_FALSE(parser.has("unused")); EXPECT_FALSE(parser.has("unused"));
} }
@ -47,8 +53,14 @@ TEST(CommandLineParser, testHas_TrueValues)
cv::CommandLineParser parser(argc, argv, keys); cv::CommandLineParser parser(argc, argv, keys);
EXPECT_TRUE(parser.has("help")); EXPECT_TRUE(parser.has("help"));
EXPECT_TRUE(parser.has("h")); EXPECT_TRUE(parser.has("h"));
EXPECT_TRUE(parser.get<bool>("help"));
EXPECT_TRUE(parser.get<bool>("h"));
EXPECT_TRUE(parser.has("info")); EXPECT_TRUE(parser.has("info"));
EXPECT_TRUE(parser.has("i")); EXPECT_TRUE(parser.has("i"));
EXPECT_TRUE(parser.get<bool>("info"));
EXPECT_TRUE(parser.get<bool>("i"));
EXPECT_TRUE(parser.get<bool>("true"));
EXPECT_TRUE(parser.get<bool>("t"));
EXPECT_FALSE(parser.has("n")); EXPECT_FALSE(parser.has("n"));
EXPECT_FALSE(parser.has("unused")); EXPECT_FALSE(parser.has("unused"));
} }
@ -59,8 +71,14 @@ TEST(CommandLineParser, testHas_TrueValues1)
cv::CommandLineParser parser(argc, argv, keys); cv::CommandLineParser parser(argc, argv, keys);
EXPECT_TRUE(parser.has("help")); EXPECT_TRUE(parser.has("help"));
EXPECT_TRUE(parser.has("h")); EXPECT_TRUE(parser.has("h"));
EXPECT_TRUE(parser.get<bool>("help"));
EXPECT_TRUE(parser.get<bool>("h"));
EXPECT_TRUE(parser.has("info")); EXPECT_TRUE(parser.has("info"));
EXPECT_TRUE(parser.has("i")); EXPECT_TRUE(parser.has("i"));
EXPECT_TRUE(parser.get<bool>("info"));
EXPECT_TRUE(parser.get<bool>("i"));
EXPECT_TRUE(parser.get<bool>("true"));
EXPECT_TRUE(parser.get<bool>("t"));
EXPECT_FALSE(parser.has("n")); EXPECT_FALSE(parser.has("n"));
EXPECT_FALSE(parser.has("unused")); EXPECT_FALSE(parser.has("unused"));
} }
@ -71,8 +89,14 @@ TEST(CommandLineParser, testHas_FalseValues0)
cv::CommandLineParser parser(argc, argv, keys); cv::CommandLineParser parser(argc, argv, keys);
EXPECT_TRUE(parser.has("help")); EXPECT_TRUE(parser.has("help"));
EXPECT_TRUE(parser.has("h")); EXPECT_TRUE(parser.has("h"));
EXPECT_FALSE(parser.get<bool>("help"));
EXPECT_FALSE(parser.get<bool>("h"));
EXPECT_TRUE(parser.has("info")); EXPECT_TRUE(parser.has("info"));
EXPECT_TRUE(parser.has("i")); EXPECT_TRUE(parser.has("i"));
EXPECT_FALSE(parser.get<bool>("info"));
EXPECT_FALSE(parser.get<bool>("i"));
EXPECT_TRUE(parser.get<bool>("true"));
EXPECT_TRUE(parser.get<bool>("t"));
EXPECT_FALSE(parser.has("n")); EXPECT_FALSE(parser.has("n"));
EXPECT_FALSE(parser.has("unused")); EXPECT_FALSE(parser.has("unused"));
} }
@ -99,30 +123,38 @@ TEST(CommandLineParser, testBoolOption_noValues)
EXPECT_TRUE(parser.get<bool>("h")); EXPECT_TRUE(parser.get<bool>("h"));
EXPECT_TRUE(parser.get<bool>("info")); EXPECT_TRUE(parser.get<bool>("info"));
EXPECT_TRUE(parser.get<bool>("i")); EXPECT_TRUE(parser.get<bool>("i"));
EXPECT_TRUE(parser.get<bool>("true"));
EXPECT_TRUE(parser.get<bool>("t"));
} }
TEST(CommandLineParser, testBoolOption_TrueValues) TEST(CommandLineParser, testBoolOption_TrueValues)
{ {
const char* argv[] = {"<bin>", "-h=TRUE", "--info=true"}; const char* argv[] = {"<bin>", "-h=TrUe", "-t=1", "--info=true", "-n=truE"};
const int argc = 3; const int argc = 5;
cv::CommandLineParser parser(argc, argv, keys); cv::CommandLineParser parser(argc, argv, keys);
//EXPECT_TRUE(parser.get<bool>("help")); EXPECT_TRUE(parser.get<bool>("help"));
//EXPECT_TRUE(parser.get<bool>("h")); EXPECT_TRUE(parser.get<bool>("h"));
EXPECT_TRUE(parser.get<bool>("info")); EXPECT_TRUE(parser.get<bool>("info"));
EXPECT_TRUE(parser.get<bool>("i")); EXPECT_TRUE(parser.get<bool>("i"));
EXPECT_FALSE(parser.get<bool>("unused")); EXPECT_TRUE(parser.get<bool>("true"));
EXPECT_FALSE(parser.get<bool>("n")); EXPECT_TRUE(parser.get<bool>("t"));
EXPECT_TRUE(parser.get<bool>("unused"));
EXPECT_TRUE(parser.get<bool>("n"));
} }
TEST(CommandLineParser, testBoolOption_FalseValues) TEST(CommandLineParser, testBoolOption_FalseValues)
{ {
const char* argv[] = {"<bin>", "--help=FALSE", "-i=false"}; const char* argv[] = {"<bin>", "--help=FALSE", "-t=FaLsE", "-i=false", "-n=0"};
const int argc = 3; const int argc = 5;
cv::CommandLineParser parser(argc, argv, keys); cv::CommandLineParser parser(argc, argv, keys);
EXPECT_FALSE(parser.get<bool>("help")); EXPECT_FALSE(parser.get<bool>("help"));
EXPECT_FALSE(parser.get<bool>("h")); EXPECT_FALSE(parser.get<bool>("h"));
EXPECT_FALSE(parser.get<bool>("info")); EXPECT_FALSE(parser.get<bool>("info"));
EXPECT_FALSE(parser.get<bool>("i")); EXPECT_FALSE(parser.get<bool>("i"));
EXPECT_FALSE(parser.get<bool>("true"));
EXPECT_FALSE(parser.get<bool>("t"));
EXPECT_FALSE(parser.get<bool>("unused"));
EXPECT_FALSE(parser.get<bool>("n"));
} }

Loading…
Cancel
Save