From a2f5884159deab84641145062e080e6011034a6b Mon Sep 17 00:00:00 2001 From: itsyplen Date: Wed, 8 Jun 2011 10:18:56 +0000 Subject: [PATCH] Specialization for 'get' method with bool type was added, help and constructor were updated too --- modules/core/include/opencv2/core/core.hpp | 26 ++++++------ modules/core/src/cmdparser.cpp | 68 +++++++++++++++++++++--------- 2 files changed, 59 insertions(+), 35 deletions(-) diff --git a/modules/core/include/opencv2/core/core.hpp b/modules/core/include/opencv2/core/core.hpp index 0bde697..73e11ee 100644 --- a/modules/core/include/opencv2/core/core.hpp +++ b/modules/core/include/opencv2/core/core.hpp @@ -4178,18 +4178,14 @@ protected: CommandLineParser parser(argc, argv); int k = parser.get("k", -1); //these methods also work double db = parser.get("db"); //with and type - string key = parser.get("0"); - string key1 = parser.get("1"); - string argument = parser.get("2"); + bool key = parser.get("key"); + bool key1 = parser.get("key1"); The method return 'true', if 'key' was defined in command line + " and it will return 'false' otherwise.> + string argument = parser.get("0"); + It also works with 'int', 'unsigned int', 'double' and 'float' types. string inputFile = parser.get("inputFile"); - - If parameter must to have some value, you have to define it using '--' or '-' increment - and assign is a value through '='. For example like this: --key=120 or -file_name=lena.jpg - If parameter doesn't has any value, you can define it with '--' or '-' increment and without it. - In this case you have to select it index in command line, if you whant to get it. - Only keys without any value have it personal index.Index starts from zero. - For example, see the string with arguments above: --key has index 0, -key has index 1, argument has index 2 - other keys have some values and they don't have index. */ class CV_EXPORTS CommandLineParser { @@ -4198,9 +4194,6 @@ class CV_EXPORTS CommandLineParser //! the default constructor CommandLineParser(int argc, const char* argv[]); - //! allows to check if parameter is given - bool has(const std::string& keys) const; - //! get parameter, if parameter is not given get default parameter template _Tp get(const std::string& name, const _Tp& default_value = _Tp()) @@ -4215,6 +4208,8 @@ class CV_EXPORTS CommandLineParser std::map data; std::string getString(const std::string& name) const; + bool has(const std::string& keys) const; + template _Tp analizeValue(const std::string& str); @@ -4234,6 +4229,9 @@ class CV_EXPORTS CommandLineParser _Tp analyzeValue(const std::string& str); }; template<> CV_EXPORTS + bool CommandLineParser::get(const std::string& name, const bool& default_value); + + template<> CV_EXPORTS std::string CommandLineParser::analyzeValue(const std::string& str); template<> CV_EXPORTS diff --git a/modules/core/src/cmdparser.cpp b/modules/core/src/cmdparser.cpp index 99ab413..cf6f8d2 100644 --- a/modules/core/src/cmdparser.cpp +++ b/modules/core/src/cmdparser.cpp @@ -15,17 +15,18 @@ void helpParser() "Usage: \n" " Imagine that the input parameters are next:\n" " -k=10 --key --db=-10.11 -key1 argument --inputFile=lena.jpg\n" - "parser.get(\"k\")(\"k\")\n" - "parser.get(\"db\", 99.99)(\"db\", 99.99)\n" " It also works with 'int', 'unsigned int', 'float' and 'string' types\n" - "parser.get(\"0\")\n" - "parser.get(\"1\")\n" - "parser.get(\"2\")\n\n" + "parser.get(\"key\")\n" + "parser.get(\"key1\")\n" + "parser.get(\"0\")\n" + " It also works with 'int', 'unsigned int', 'double' and 'float' types\n\n" ); } @@ -110,25 +111,42 @@ CommandLineParser::CommandLineParser(int argc, const char* argv[]) data.clear(); break; } - else + else if ((cur_name.find('-') == 0) && ((cur_name[1] < '0') || (cur_name[1] > '9')) ) { - str_buff << index; - while (cur_name.find('-') == 0) + while (cur_name.find('-') == 0) cur_name.erase(0,1); - for(it = data.begin(); it != data.end(); it++) - { - if (it->second == cur_name) + for(it = data.begin(); it != data.end(); it++) { - printf("CommandLineParser constructor found dublicating parameters for name=%s\n" - , cur_name.c_str()); - printf("Constructor will not continue its work since this moment.\n" - "Please enter parameters without dublicates\n"); - helpParser(); - data.clear(); - break; + if (it->first == cur_name) + { + printf("CommandLineParser constructor found dublicating parameters for name=%s\n" + , cur_name.c_str()); + printf("Constructor will not continue its work since this moment.\n" + "Please enter parameters without dublicates\n"); + helpParser(); + data.clear(); + break; + } } + data[cur_name] = "true"; } + else + { + str_buff << index; + for(it = data.begin(); it != data.end(); it++) + { + if (it->second == cur_name) + { + printf("CommandLineParser constructor found dublicating parameters for name=%s\n" + , cur_name.c_str()); + printf("Constructor will not continue its work since this moment.\n" + "Please enter parameters without dublicates\n"); + helpParser(); + data.clear(); + break; + } + } data[str_buff.str()] = cur_name; str_buff.seekp(0); index++; @@ -217,6 +235,14 @@ static _Tp fromStringNumber(const std::string& str)//the default conversion func } template<> +bool CommandLineParser::get(const std::string& name, const bool& default_value) +{ + if (!has(name)) + return false; + return true; +} + +template<> std::string CommandLineParser::analyzeValue(const std::string& str) { return str; -- 2.7.4