Specialization for 'get' method with bool type was added, help and constructor were updated too

This commit is contained in:
itsyplen 2011-06-08 10:18:56 +00:00
parent 35e25b760e
commit a2f5884159
2 changed files with 59 additions and 35 deletions

View File

@ -4178,18 +4178,14 @@ protected:
CommandLineParser parser(argc, argv);
int k = parser.get<int>("k", -1); //these methods also work
double db = parser.get<double>("db"); //with <float> and <unsigned int> type
string key = parser.get<string>("0");
string key1 = parser.get<string>("1");
string argument = parser.get<string>("2");
bool key = parser.get<bool>("key"); <The method return 'true', if 'key' was defined in command line
" and it will return 'false' otherwise.>
bool key1 = parser.get<bool>("key1"); The method return 'true', if 'key' was defined in command line
" and it will return 'false' otherwise.>
string argument = parser.get<string>("0"); <If you need to take argument. It's the first parameter without '-' or '--' increment
and without value. It has index 0. The second parameter of this type will have index 1>
It also works with 'int', 'unsigned int', 'double' and 'float' types.
string inputFile = parser.get<string>("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<typename _Tp>
_Tp get(const std::string& name, const _Tp& default_value = _Tp())
@ -4215,6 +4208,8 @@ class CV_EXPORTS CommandLineParser
std::map<std::string, std::string > data;
std::string getString(const std::string& name) const;
bool has(const std::string& keys) const;
template<typename _Tp>
_Tp analizeValue(const std::string& str);
@ -4233,6 +4228,9 @@ class CV_EXPORTS CommandLineParser
template<typename _Tp>
_Tp analyzeValue(const std::string& str);
};
template<> CV_EXPORTS
bool CommandLineParser::get<bool>(const std::string& name, const bool& default_value);
template<> CV_EXPORTS
std::string CommandLineParser::analyzeValue<std::string>(const std::string& str);

View File

@ -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<int>(\"k\")<If you need to take 'k' value.\n"
"parser.get<int>(\"k\")<If you need to take k value.\n"
" It also works with 'unsigned int', 'double', 'float' and 'string' types>\n"
"parser.get<double>(\"db\", 99.99)<If you need to take 'db' value.\n"
"parser.get<double>(\"db\", 99.99)<If you need to take db value.\n"
" If its value is empty, you will get default value 99.99>\n"
" It also works with 'int', 'unsigned int', 'float' and 'string' types\n"
"parser.get<string>(\"0\")<If you need to take 'key'. It's the first parameter without value\n"
" and it has index 0>\n"
"parser.get<stirng>(\"1\")<If you need to take 'key1'. It's the second parameter without value\n"
" and it has index 1>\n"
"parser.get<stirng>(\"2\")<If you need to take 'argument'. It's the third parameter without value\n"
" and it has index 2>\n\n"
"parser.get<bool>(\"key\")<The method return 'true', if 'key' was defined in command line\n"
" and it will return 'false' otherwise.>\n"
"parser.get<bool>(\"key1\")<The method return 'true', if 'key' was defined in command line\n"
" and it will return 'false' otherwise.>\n"
"parser.get<stirng>(\"0\")<If you need to take argument. It's the first parameter without '-' or '--' increment \n"
" and without value. It has index 0. The second parameter of this type will have index 1>\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++;
@ -216,6 +234,14 @@ static _Tp fromStringNumber(const std::string& str)//the default conversion func
return getData<_Tp>(str);
}
template<>
bool CommandLineParser::get<bool>(const std::string& name, const bool& default_value)
{
if (!has(name))
return false;
return true;
}
template<>
std::string CommandLineParser::analyzeValue<std::string>(const std::string& str)
{