Commit 0598f33a authored by Vadim Pisarevsky's avatar Vadim Pisarevsky

refactored command line parser, fixed the docs

parent b16b50d6
...@@ -58,15 +58,15 @@ The sample below demonstrates how to use CommandLineParser: ...@@ -58,15 +58,15 @@ The sample below demonstrates how to use CommandLineParser:
} }
int N = parser.get<int>("N"); int N = parser.get<int>("N");
double fps = parser.get<double>parser("fps"); double fps = parser.get<double>("fps");
std::string path = parser.get<std::string>("path"); std::string path = parser.get<std::string>("path");
use_time_stamp = parserer.has("timestamp"); use_time_stamp = parser.has("timestamp");
std::string img1 = parser.get<string>(1); std::string img1 = parser.get<string>(0);
std::string img2 = parser.get<string>(2); std::string img2 = parser.get<string>(1);
int repeat = parser.get<int>(3); int repeat = parser.get<int>(2);
if (!parser.check()) if (!parser.check())
{ {
......
...@@ -221,7 +221,7 @@ CV_EXPORTS void setNumThreads(int nthreads); ...@@ -221,7 +221,7 @@ CV_EXPORTS void setNumThreads(int nthreads);
CV_EXPORTS int getNumThreads(); CV_EXPORTS int getNumThreads();
CV_EXPORTS int getThreadNum(); CV_EXPORTS int getThreadNum();
CV_EXPORTS_W const std::string& getBuildInformation(); CV_EXPORTS_W const string& getBuildInformation();
//! Returns the number of ticks. //! Returns the number of ticks.
...@@ -4434,7 +4434,7 @@ protected: ...@@ -4434,7 +4434,7 @@ protected:
struct CV_EXPORTS Param struct CV_EXPORTS Param
{ {
enum { INT=0, BOOLEAN=1, REAL=2, STRING=3, MAT=4, MAT_VECTOR=5, ALGORITHM=6 }; enum { INT=0, BOOLEAN=1, REAL=2, STRING=3, MAT=4, MAT_VECTOR=5, ALGORITHM=6, FLOAT=7, UNSIGNED_INT=8, UINT64=9 };
Param(); Param();
Param(int _type, bool _readonly, int _offset, Param(int _type, bool _readonly, int _offset,
...@@ -4505,142 +4505,73 @@ template<> struct ParamType<Algorithm> ...@@ -4505,142 +4505,73 @@ template<> struct ParamType<Algorithm>
enum { type = Param::ALGORITHM }; enum { type = Param::ALGORITHM };
}; };
// The CommandLineParser class is designed for command line arguments parsing template<> struct ParamType<float>
class CV_EXPORTS CommandLineParserParams
{ {
public: typedef float const_param_type;
std::string help_message; typedef float member_type;
std::string def_value;
std::vector<std::string> keys; enum { type = Param::FLOAT };
int number;
}; };
template <typename T> template<> struct ParamType<unsigned>
std::string get_type_name() { return "UNKNOW"; } {
typedef unsigned const_param_type;
typedef unsigned member_type;
bool cmp_params(const CommandLineParserParams & p1, const CommandLineParserParams & p2); enum { type = Param::UNSIGNED_INT };
};
template<typename T> template<> struct ParamType<uint64>
T from_str(const std::string & str)
{ {
T value; typedef uint64 const_param_type;
std::stringstream ss(str); typedef uint64 member_type;
ss >> value;
if (ss.fail()) enum { type = Param::UINT64 };
{ };
std::string err_msg =
std::string("can not convert: [")
+ str
+ std::string("] to [")
+ get_type_name<T>()
+ std::string("]");
CV_Error(CV_StsBadArg, err_msg);
}
return value;
}
template<> std::string from_str(const std::string & str);
template<typename T> // The CommandLineParser class is designed for command line arguments parsing
std::string to_str(T value)
{
std::ostringstream os;
os << value;
return os.str();
}
class CV_EXPORTS CommandLineParser class CV_EXPORTS CommandLineParser
{ {
public: public:
CommandLineParser(int argc, const char * const argv[], const std::string keys); CommandLineParser(int argc, const char* const argv[], const string& keys);
CommandLineParser(const CommandLineParser& parser);
CommandLineParser& operator = (const CommandLineParser& parser);
std::string getPathToApplication(); string getPathToApplication() const;
template <typename T> template <typename T>
T get(const std::string& name, bool space_delete = true) T get(const string& name, bool space_delete = true) const
{
try
{
for (size_t i = 0; i < data.size(); i++)
{ {
for (size_t j = 0; j < data[i].keys.size(); j++) T val = T();
{ getByName(name, space_delete, ParamType<T>::type, (void*)&val);
if (name.compare(data[i].keys[j]) == 0) return val;
{
std::string v = data[i].def_value;
if (space_delete == true) v = cat_string(v);
return from_str<T>(v);
}
}
}
error = true;
error_message += "Unknown parametes " + name + "\n";
}
catch (std::exception& e)
{
error = true;
error_message += "Exception: " + std::string(e.what()) + "\n";
}
return T();
} }
template <typename T> template <typename T>
T get(int index, bool space_delete = true) T get(int index, bool space_delete = true) const
{
try
{
for (size_t i = 0; i < data.size(); i++)
{
if (data[i].number == index - 1)
{
std::string v = data[i].def_value;
if (space_delete == true) v = cat_string(v);
return from_str<T>(v);
}
}
error = true;
error_message += "Unknown parametes #" + to_str<int>(index) + "\n";
}
catch(std::exception & e)
{ {
error = true; T val = T();
error_message += "Exception: " + std::string(e.what()) + "\n"; getByIndex(index, space_delete, ParamType<T>::type, (void*)&val);
return val;
} }
return T();
}
bool has(const std::string& name);
bool check();
void about(std::string message);
void printMessage();
void printErrors();
protected: bool has(const string& name) const;
bool error;
std::string error_message;
std::string about_message;
std::string path_to_app;
std::string app_name;
std::vector<CommandLineParserParams> data; bool check() const;
std::vector<std::string> split_range_string(std::string str, char fs, char ss); void about(const string& message);
std::vector<std::string> split_string(std::string str, char symbol = ' ', bool create_empty_item = false);
std::string cat_string(std::string str);
void apply_params(std::string key, std::string value); void printMessage() const;
void apply_params(int i, std::string value); void printErrors() const;
void sort_params(); protected:
void getByName(const string& name, bool space_delete, int type, void* dst) const;
void getByIndex(int index, bool space_delete, int type, void* dst) const;
struct Impl;
Impl* impl;
}; };
/////////////////////////////// Parallel Primitives ////////////////////////////////// /////////////////////////////// Parallel Primitives //////////////////////////////////
......
This diff is collapsed.
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment