From 97ec91ad67be62ff29ad861ef4b47b5b4c6c339a Mon Sep 17 00:00:00 2001 From: KUANG Fangjun Date: Mon, 21 Aug 2017 11:26:22 +0200 Subject: [PATCH] fix cv::CommandLineParser. It should handle bool value not only of "true" but also of "TRUE" and "True". --- modules/core/include/opencv2/core/utility.hpp | 4 +- modules/core/src/command_line_parser.cpp | 15 ++++-- modules/core/test/test_utils.cpp | 48 +++++++++++++++---- 3 files changed, 54 insertions(+), 13 deletions(-) diff --git a/modules/core/include/opencv2/core/utility.hpp b/modules/core/include/opencv2/core/utility.hpp index 41ccb4b88c..b3180f54f5 100644 --- a/modules/core/include/opencv2/core/utility.hpp +++ b/modules/core/include/opencv2/core/utility.hpp @@ -808,7 +808,7 @@ public: 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} $ ./bin/my-executable @endcode @@ -914,7 +914,7 @@ public: */ void printMessage() const; - /** @brief Print list of errors occured + /** @brief Print list of errors occurred @sa check */ diff --git a/modules/core/src/command_line_parser.cpp b/modules/core/src/command_line_parser.cpp index 2b67ce825c..02bd04a9d0 100644 --- a/modules/core/src/command_line_parser.cpp +++ b/modules/core/src/command_line_parser.cpp @@ -69,6 +69,15 @@ static const char* get_type_name(int type) 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) { std::stringstream ss(str.c_str()); @@ -78,7 +87,7 @@ static void from_str(const String& str, int type, void* dst) { std::string temp; ss >> temp; - *(bool*) dst = temp == "true"; + *(bool*) dst = parse_bool(temp); } else if( type == Param::UNSIGNED_INT ) ss >> *(unsigned*)dst; @@ -113,7 +122,7 @@ void CommandLineParser::getByName(const String& name, bool space_delete, int typ if (space_delete) 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) { impl->error = true; 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; 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) { impl->error = true; impl->error_message = impl->error_message + format("Missing parameter #%d\n", index); diff --git a/modules/core/test/test_utils.cpp b/modules/core/test/test_utils.cpp index 8ff76af661..4740a24d11 100644 --- a/modules/core/test/test_utils.cpp +++ b/modules/core/test/test_utils.cpp @@ -35,8 +35,14 @@ TEST(CommandLineParser, testHas_noValues) cv::CommandLineParser parser(argc, argv, keys); EXPECT_TRUE(parser.has("help")); EXPECT_TRUE(parser.has("h")); + EXPECT_TRUE(parser.get("help")); + EXPECT_TRUE(parser.get("h")); EXPECT_TRUE(parser.has("info")); EXPECT_TRUE(parser.has("i")); + EXPECT_TRUE(parser.get("info")); + EXPECT_TRUE(parser.get("i")); + EXPECT_TRUE(parser.get("true")); + EXPECT_TRUE(parser.get("t")); EXPECT_FALSE(parser.has("n")); EXPECT_FALSE(parser.has("unused")); } @@ -47,8 +53,14 @@ TEST(CommandLineParser, testHas_TrueValues) cv::CommandLineParser parser(argc, argv, keys); EXPECT_TRUE(parser.has("help")); EXPECT_TRUE(parser.has("h")); + EXPECT_TRUE(parser.get("help")); + EXPECT_TRUE(parser.get("h")); EXPECT_TRUE(parser.has("info")); EXPECT_TRUE(parser.has("i")); + EXPECT_TRUE(parser.get("info")); + EXPECT_TRUE(parser.get("i")); + EXPECT_TRUE(parser.get("true")); + EXPECT_TRUE(parser.get("t")); EXPECT_FALSE(parser.has("n")); EXPECT_FALSE(parser.has("unused")); } @@ -59,8 +71,14 @@ TEST(CommandLineParser, testHas_TrueValues1) cv::CommandLineParser parser(argc, argv, keys); EXPECT_TRUE(parser.has("help")); EXPECT_TRUE(parser.has("h")); + EXPECT_TRUE(parser.get("help")); + EXPECT_TRUE(parser.get("h")); EXPECT_TRUE(parser.has("info")); EXPECT_TRUE(parser.has("i")); + EXPECT_TRUE(parser.get("info")); + EXPECT_TRUE(parser.get("i")); + EXPECT_TRUE(parser.get("true")); + EXPECT_TRUE(parser.get("t")); EXPECT_FALSE(parser.has("n")); EXPECT_FALSE(parser.has("unused")); } @@ -71,8 +89,14 @@ TEST(CommandLineParser, testHas_FalseValues0) cv::CommandLineParser parser(argc, argv, keys); EXPECT_TRUE(parser.has("help")); EXPECT_TRUE(parser.has("h")); + EXPECT_FALSE(parser.get("help")); + EXPECT_FALSE(parser.get("h")); EXPECT_TRUE(parser.has("info")); EXPECT_TRUE(parser.has("i")); + EXPECT_FALSE(parser.get("info")); + EXPECT_FALSE(parser.get("i")); + EXPECT_TRUE(parser.get("true")); + EXPECT_TRUE(parser.get("t")); EXPECT_FALSE(parser.has("n")); EXPECT_FALSE(parser.has("unused")); } @@ -99,30 +123,38 @@ TEST(CommandLineParser, testBoolOption_noValues) EXPECT_TRUE(parser.get("h")); EXPECT_TRUE(parser.get("info")); EXPECT_TRUE(parser.get("i")); + EXPECT_TRUE(parser.get("true")); + EXPECT_TRUE(parser.get("t")); } TEST(CommandLineParser, testBoolOption_TrueValues) { - const char* argv[] = {"", "-h=TRUE", "--info=true"}; - const int argc = 3; + const char* argv[] = {"", "-h=TrUe", "-t=1", "--info=true", "-n=truE"}; + const int argc = 5; cv::CommandLineParser parser(argc, argv, keys); - //EXPECT_TRUE(parser.get("help")); - //EXPECT_TRUE(parser.get("h")); + EXPECT_TRUE(parser.get("help")); + EXPECT_TRUE(parser.get("h")); EXPECT_TRUE(parser.get("info")); EXPECT_TRUE(parser.get("i")); - EXPECT_FALSE(parser.get("unused")); - EXPECT_FALSE(parser.get("n")); + EXPECT_TRUE(parser.get("true")); + EXPECT_TRUE(parser.get("t")); + EXPECT_TRUE(parser.get("unused")); + EXPECT_TRUE(parser.get("n")); } TEST(CommandLineParser, testBoolOption_FalseValues) { - const char* argv[] = {"", "--help=FALSE", "-i=false"}; - const int argc = 3; + const char* argv[] = {"", "--help=FALSE", "-t=FaLsE", "-i=false", "-n=0"}; + const int argc = 5; cv::CommandLineParser parser(argc, argv, keys); EXPECT_FALSE(parser.get("help")); EXPECT_FALSE(parser.get("h")); EXPECT_FALSE(parser.get("info")); EXPECT_FALSE(parser.get("i")); + EXPECT_FALSE(parser.get("true")); + EXPECT_FALSE(parser.get("t")); + EXPECT_FALSE(parser.get("unused")); + EXPECT_FALSE(parser.get("n")); }