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:
}
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");
use_time_stamp = parserer.has("timestamp");
use_time_stamp = parser.has("timestamp");
std::string img1 = parser.get<string>(1);
std::string img2 = parser.get<string>(2);
std::string img1 = parser.get<string>(0);
std::string img2 = parser.get<string>(1);
int repeat = parser.get<int>(3);
int repeat = parser.get<int>(2);
if (!parser.check())
{
......
......@@ -221,7 +221,7 @@ CV_EXPORTS void setNumThreads(int nthreads);
CV_EXPORTS int getNumThreads();
CV_EXPORTS int getThreadNum();
CV_EXPORTS_W const std::string& getBuildInformation();
CV_EXPORTS_W const string& getBuildInformation();
//! Returns the number of ticks.
......@@ -4434,7 +4434,7 @@ protected:
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(int _type, bool _readonly, int _offset,
......@@ -4505,142 +4505,73 @@ template<> struct ParamType<Algorithm>
enum { type = Param::ALGORITHM };
};
// The CommandLineParser class is designed for command line arguments parsing
class CV_EXPORTS CommandLineParserParams
template<> struct ParamType<float>
{
public:
std::string help_message;
std::string def_value;
std::vector<std::string> keys;
int number;
typedef float const_param_type;
typedef float member_type;
enum { type = Param::FLOAT };
};
template <typename T>
std::string get_type_name() { return "UNKNOW"; }
template<> struct ParamType<unsigned>
{
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>
T from_str(const std::string & str)
template<> struct ParamType<uint64>
{
T value;
std::stringstream ss(str);
ss >> value;
typedef uint64 const_param_type;
typedef uint64 member_type;
if (ss.fail())
{
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;
}
enum { type = Param::UINT64 };
};
template<> std::string from_str(const std::string & str);
template<typename T>
std::string to_str(T value)
{
std::ostringstream os;
os << value;
return os.str();
}
// The CommandLineParser class is designed for command line arguments parsing
class CV_EXPORTS CommandLineParser
{
public:
CommandLineParser(int argc, const char * const argv[], const std::string keys);
public:
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>
T get(const std::string& name, bool space_delete = true)
{
try
{
for (size_t i = 0; i < data.size(); i++)
T get(const string& name, bool space_delete = true) const
{
for (size_t j = 0; j < data[i].keys.size(); j++)
{
if (name.compare(data[i].keys[j]) == 0)
{
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();
T val = T();
getByName(name, space_delete, ParamType<T>::type, (void*)&val);
return val;
}
template <typename T>
T get(int index, bool space_delete = true)
{
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)
T get(int index, bool space_delete = true) const
{
error = true;
error_message += "Exception: " + std::string(e.what()) + "\n";
T val = T();
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 error;
std::string error_message;
std::string about_message;
std::string path_to_app;
std::string app_name;
bool has(const string& name) const;
std::vector<CommandLineParserParams> data;
bool check() const;
std::vector<std::string> split_range_string(std::string str, char fs, char ss);
std::vector<std::string> split_string(std::string str, char symbol = ' ', bool create_empty_item = false);
std::string cat_string(std::string str);
void about(const string& message);
void apply_params(std::string key, std::string value);
void apply_params(int i, std::string value);
void printMessage() const;
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 //////////////////////////////////
......
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