class CommandLineParser was updated according new requirements
authorKirill Kornyakov <no@email>
Tue, 7 Jun 2011 10:30:40 +0000 (10:30 +0000)
committerKirill Kornyakov <no@email>
Tue, 7 Jun 2011 10:30:40 +0000 (10:30 +0000)
modules/core/include/opencv2/core/core.hpp
modules/core/src/cmdparser.cpp

index 2030ebf..28a1203 100644 (file)
@@ -4174,10 +4174,22 @@ protected:
 
  The class is used for reading command arguments.
  Supports the following syntax:
-   //-k=10  --x 0.001 --inputFile lena.jpg
-   int k = parser.get<int>("--k | -k", -1);
-   double x = parser.get<double>("--x");
-   string inputFile = parser.get<string>("--inputFile");
+   //-k=10 --key --db=-10.11 -key1 argument --inputFile=lena.jpg
+   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");
+   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
 {
@@ -4200,7 +4212,7 @@ class CV_EXPORTS CommandLineParser
         }
 
     protected:
-        std::map<std::string, std::vector<std::string> > data;
+        std::map<std::string, std::string > data;
         std::string getString(const std::string& name) const;
 
         template<typename _Tp>
index b01c8e2..4edfe41 100644 (file)
@@ -19,85 +19,74 @@ vector<string> split_string(const string& str, const string& delimiters)
     return res;
 }
 
-void PreprocessArgs(int _argc, const char* _argv[], int& argc, char**& argv)
+CommandLineParser::CommandLineParser(int argc, const char* argv[])
 {
-    std::vector<std::string> buffer_vector;
-    std::string buffer_string;
-    std::string buffer2_string;
+    std::string cur_name;
+    std::string buffer;
+    std::stringstream str_buff(std::stringstream::in | std::stringstream::out);
+    std::string str_index;
+    std::map<std::string, std::string >::iterator it;
     int find_symbol;
+    int index = 0;
 
-    for (int i = 0; i < _argc; i++)
-    {
-        buffer_string = _argv[i];
-        find_symbol = buffer_string.find('=');
-        if (find_symbol == -1)
-            buffer_vector.push_back(buffer_string);
-        else if (find_symbol == 0 || find_symbol == (int)(buffer_string.length() - 1))
-        {
-            buffer_string.erase(find_symbol, (find_symbol + 1));
-            if(!buffer_string.empty())
-                buffer_vector.push_back(buffer_string);
-        }
-        else
-        {
-            buffer2_string = buffer_string;
-            buffer_string.erase(find_symbol);
-            buffer_vector.push_back(buffer_string);
-            buffer2_string.erase(0, find_symbol + 1);
-            buffer_vector.push_back(buffer2_string);
-
-        }
-    }
 
-    argc = buffer_vector.size();
-    argv = new char* [argc];
-    for (int i=0; i < argc; i++)
+    for(int i = 1; i < argc; i++)
     {
-        argv[i] = new char[buffer_vector[i].length() + 1];
-        memcpy(argv[i], buffer_vector[i].c_str(), buffer_vector[i].length() + 1);
-    }
-}
-
 
-CommandLineParser::CommandLineParser(int _argc, const char* _argv[])
-{
-    std::string cur_name;
-    bool was_pushed=false;
-    int argc;
-    char** argv;
-    PreprocessArgs(_argc, _argv, argc, argv);
-
-    for(int i=1; i < argc; i++) {
-
-            if(!argv[i])
-                    break;
-
-            if( (argv[i][0]== '-') && (strlen(argv[i]) > 1)
-                    &&
-                    ( (argv[i][1] < '0') || (argv[i][1] > '9'))   )
+        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())
             {
-                    if (!cur_name.empty() && !was_pushed) {
-                            data[cur_name].push_back("");
-                    }
-                    cur_name=argv[i];
-                    while (cur_name.find('-') == 0)
+                string str_exception="dublicating parameters for name='" + cur_name + "'";
+                CV_Error(CV_StsParseError, str_exception);
+            }
+                else
+                    data[cur_name] = buffer;
+        }
+            else if (cur_name.find('=') == 0)
+                {
+                    string str_exception="This key is wrong. The key mustn't have '=' like increment' '" + cur_name + "'";
+                    CV_Error(CV_StsParseError, str_exception);
+                }
+            else if(((int)cur_name.find('-') == -1) && ((int)cur_name.find('=') != -1))
+                {
+                    string str_exception="This key must be defined with '--' or '-' increment'" + cur_name + "'";
+                    CV_Error(CV_StsParseError, str_exception);
+                }
+            else if (cur_name.find('=') == (cur_name.length() - 1))
+                {
+                    string str_exception="This key must have argument after '=''" + cur_name + "'";
+                    CV_Error(CV_StsParseError, str_exception);
+                }
+            else
+                {
+                    str_buff<< index;
+                    str_index = str_buff.str();
+                    str_buff.seekp(0);
+                    for(it = data.begin(); it != data.end(); it++)
                     {
-                        cur_name.erase(0,1);
-                    }
-                    was_pushed=false;
-
-                    if (data.find(cur_name) != data.end()) {
+                        if (it->second == cur_name)
+                        {
                             string str_exception="dublicating parameters for name='" + cur_name + "'";
                             CV_Error(CV_StsParseError, str_exception);
+                        }
                     }
-                    continue;
-            }
+                    data[str_index.c_str()] = cur_name;
+                    index++;
+                }
 
-            data[cur_name].push_back(argv[i]);
-            was_pushed=true;
     }
-    if (!cur_name.empty() && !was_pushed)
-            data[cur_name].push_back("");
 }
 
 bool CommandLineParser::has(const std::string& keys) const
@@ -131,7 +120,7 @@ std::string CommandLineParser::getString(const std::string& keys) const
 
     if (found_index<0)
         return string();
-    return data.find(names[found_index])->second[0];
+    return data.find(names[found_index])->second;
 }
 
 template<typename _Tp>