1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
#include "precomp.hpp"
using namespace std;
using namespace cv;
void helpParser()
{
printf("\nThe CommandLineParser class is designed for command line arguments parsing\n"
"Supported syntax: \n"
" --key1=arg1 or --key3 <The keys with '--' can have argument.\n"
"If it has argument, you should assign it through '=' sign> \n"
" -key2=arg2 or -key2 <The keys witn '-' can have argument \n"
"If it has argument, you should assign it through '=' sign> \n"
" key3 <This key can't has any parameter> \n"
"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"
" 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"
" 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<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"
);
}
vector<string> split_string(const string& str, const string& delimiters)
{
vector<string> res;
string::size_type lastPos = str.find_first_not_of(delimiters, 0);
string::size_type pos = str.find_first_of(delimiters, lastPos);
while (string::npos != pos || string::npos != lastPos)
{
res.push_back(str.substr(lastPos, pos - lastPos));
lastPos = str.find_first_not_of(delimiters, pos);
pos = str.find_first_of(delimiters, lastPos);
}
return res;
}
CommandLineParser::CommandLineParser(int argc, const char* argv[])
{
std::string cur_name;
std::string buffer;
std::stringstream str_buff(std::stringstream::in | std::stringstream::out);
std::map<std::string, std::string >::iterator it;
int find_symbol;
int index = 0;
for(int i = 1; i < argc; i++)
{
if(!argv[i])
break;
cur_name = argv[i];
if((cur_name.find('-') == 0) && ((int)cur_name.find('=') != -1) &&
(cur_name.find('=') != (cur_name.length() - 1)))
{
while (cur_name.find('-') == 0)
cur_name.erase(0,1);
buffer = cur_name;
find_symbol = (int)cur_name.find('=');
cur_name.erase(find_symbol);
buffer.erase(0, find_symbol + 1);
if (data.find(cur_name) != data.end())
{
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;
}
else
data[cur_name] = buffer;
}
else if (cur_name.find('=') == 0)
{
printf("The next key is wrong: key= %s\n", cur_name.c_str());
printf("Constructor will not continue its work since this moment.\n"
"Please enter parameters without any mistakes\n");
helpParser();
data.clear();
break;
}
else if(((int)cur_name.find('-') == -1) && ((int)cur_name.find('=') != -1))
{
printf("The next key must be defined with '--' or '-' increment: key= %s\n", cur_name.c_str());
printf("Constructor will not continue its work since this moment.\n"
"Please enter parameters without any mistakes\n");
helpParser();
data.clear();
break;
}
else if (cur_name.find('=') == (cur_name.length() - 1))
{
printf("The next key must have argument after '=': key= %s\n", cur_name.c_str());
printf("Constructor will not continue its work since this moment.\n"
"Please enter parameters without any mistakes\n");
helpParser();
data.clear();
break;
}
else if ((cur_name.find('-') == 0) && ((cur_name[1] < '0') || (cur_name[1] > '9')) )
{
while (cur_name.find('-') == 0)
cur_name.erase(0,1);
for(it = data.begin(); it != data.end(); it++)
{
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++;
}
}
}
bool CommandLineParser::has(const std::string& keys) const
{
vector<string> names=split_string(keys, " |");
for(size_t j=0; j < names.size(); j++) {
if (data.find(names[j])!=data.end())
return true;
}
return false;
}
std::string CommandLineParser::getString(const std::string& keys) const
{
vector<string> names=split_string(keys, " |");
int found_index=-1;
for(size_t j=0; j < names.size(); j++) {
const string& cur_name=names[j];
bool is_cur_found=has(cur_name);
if (is_cur_found && (found_index >= 0)) {
string str_exception="dublicating parameters for "
"name='" + names[found_index] + "' and name='"+cur_name+"'";
CV_Error(CV_StsParseError, str_exception);
}
if (is_cur_found)
found_index=(int)j;
}
if (found_index<0)
return string();
return data.find(names[found_index])->second;
}
template<typename _Tp>
_Tp CommandLineParser::fromStringNumber(const std::string& str) //the default conversion function for numbers
{
if (str.empty())
CV_Error(CV_StsParseError, "Empty string cannot be converted to a number");
const char* c_str=str.c_str();
if((!isdigit(c_str[0]))
&&
(
(c_str[0]!='-') || (strlen(c_str) <= 1) || ( !isdigit(c_str[1]) )
)
)
{
CV_Error(CV_StsParseError, "The string '"+ str +"' cannot be converted to a number");
}
return getData<_Tp>(str);
}
template<typename _Tp>
static _Tp getData(const std::string& str)
{
_Tp res;
std::stringstream s1(str);
s1 >> res;
return res;
}
template<typename _Tp>
static _Tp fromStringNumber(const std::string& str)//the default conversion function for numbers
{
if (str.empty())
CV_Error(CV_StsParseError, "Empty string cannot be converted to a number");
const char* c_str=str.c_str();
if( !isdigit(c_str[0]) &&
(c_str[0] != '-' || strlen(c_str) <= 1 || !isdigit(c_str[1]) ))
CV_Error(CV_StsParseError, "The string '"+ str +"' cannot be converted to a number");
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)
{
return str;
}
template<>
int CommandLineParser::analyzeValue<int>(const std::string& str)
{
return fromStringNumber<int>(str);
}
template<>
unsigned int CommandLineParser::analyzeValue<unsigned int>(const std::string& str)
{
return fromStringNumber<unsigned int>(str);
}
template<>
float CommandLineParser::analyzeValue<float>(const std::string& str)
{
return fromStringNumber<float>(str);
}
template<>
double CommandLineParser::analyzeValue<double>(const std::string& str)
{
return fromStringNumber<double>(str);
}