Skip to content

Commit

Permalink
add new version of CommandLineParser. add empty docs
Browse files Browse the repository at this point in the history
  • Loading branch information
AoD314 committed Sep 7, 2012
1 parent 6a112aa commit 54a202b
Show file tree
Hide file tree
Showing 26 changed files with 851 additions and 661 deletions.
101 changes: 101 additions & 0 deletions modules/core/doc/command_line_parser.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
Command Line Parser
===================

.. highlight:: cpp

CommandLineParser
--------
.. ocv:class:: CommandLineParser
The CommandLineParser class is designed for command line arguments parsing


.. ocv:function:: CommandLineParser::CommandLineParser(int argc, const char * const argv[], const std::string keys)
:param argc:
:param argv:
:param keys:

.. ocv:function:: T CommandLineParser::get<T>(const std::string& name, bool space_delete = true)
:param name:
:param space_delete:

.. ocv:function:: T CommandLineParser::get<T>(int index, bool space_delete = true)
:param index:
:param space_delete:

.. ocv:function:: bool CommandLineParser::has(const std::string& name)
:param name:

.. ocv:function:: bool CommandLineParser::check()
.. ocv:function:: void CommandLineParser::about(std::string message)
:param message:

.. ocv:function:: void CommandLineParser::printMessage()
.. ocv:function:: void CommandLineParser::printErrors()
.. ocv:function:: std::string CommandLineParser::getPathToApplication()
The sample below demonstrates how to use CommandLineParser:

::

CommandLineParser parser(argc, argv, keys);
parser.about("Application name v1.0.0");

if (parser.has("help"))
{
parser.printMessage();
return 0;
}

int N = parser.get<int>("N");
double fps = parser.get<double>parser("fps");
std::string path = parser.get<std::string>("path");

use_time_stamp = parserer.has("timestamp");

std::string img1 = parser.get<string>(1);
std::string img2 = parser.get<string>(2);

int repeat = parser.get<int>(3);

if (!parser.check())
{
parser.printErrors();
return 0;
}

Syntax:

::

const std::string keys =
"{help h usage ? | | print this message }"
"{@image1 | | image1 for compare }"
"{@image2 | | image2 for compare }"
"{@repeat |1 | number }"
"{path |. | path to file }"
"{fps | -1.0 | fps for output video }"
"{N count |100 | count of objects }"
"{ts timestamp | | use time stamp }"
;

Use:

::

# ./app -N=200 1.png 2.jpg 19 -ts

# ./app -fps=aaa
ERRORS:
Exception: can not convert: [aaa] to [double]

1 change: 1 addition & 0 deletions modules/core/doc/core.rst
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ core. The Core Functionality
:maxdepth: 2

basic_structures
command_line_parser
old_basic_structures
dynamic_structures
operations_on_arrays
Expand Down
202 changes: 116 additions & 86 deletions modules/core/include/opencv2/core/core.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4505,113 +4505,143 @@ template<> struct ParamType<Algorithm>
enum { type = Param::ALGORITHM };
};

// The CommandLineParser class is designed for command line arguments parsing

/*!
"\nThe CommandLineParser class is designed for command line arguments parsing\n"
"Keys map: \n"
"Before you start to work with CommandLineParser you have to create a map for keys.\n"
" It will look like this\n"
" const char* keys =\n"
" {\n"
" { s| string| 123asd |string parameter}\n"
" { d| digit | 100 |digit parameter }\n"
" { c|noCamera|false |without camera }\n"
" { 1| |some text|help }\n"
" { 2| |333 |another help }\n"
" };\n"
"Usage syntax: \n"
" \"{\" - start of parameter string.\n"
" \"}\" - end of parameter string\n"
" \"|\" - separator between short name, full name, default value and help\n"
"Supported syntax: \n"
" --key1=arg1 <If a key with '--' must has an argument\n"
" you have to assign it through '=' sign.> \n"
"<If the key with '--' doesn't have any argument, it means that it is a bool key>\n"
" -key2=arg2 <If a key with '-' must has an argument \n"
" you have to assign it through '=' sign.> \n"
"If the key with '-' doesn't have any argument, it means that it is a bool key\n"
" key3 <This key can't has any parameter> \n"
"Usage: \n"
" Imagine that the input parameters are next:\n"
" -s=string_value --digit=250 --noCamera lena.jpg 10000\n"
" CommandLineParser parser(argc, argv, keys) - create a parser object\n"
" parser.get<string>(\"s\" or \"string\") will return you first parameter value\n"
" parser.get<string>(\"s\", false or \"string\", false) will return you first parameter value\n"
" without spaces in end and begin\n"
" parser.get<int>(\"d\" or \"digit\") will return you second parameter value.\n"
" It also works with 'unsigned int', 'double', and 'float' types>\n"
" parser.get<bool>(\"c\" or \"noCamera\") will return you true .\n"
" If you enter this key in commandline>\n"
" It return you false otherwise.\n"
" parser.get<string>(\"1\") will return you the first argument without parameter (lena.jpg) \n"
" parser.get<int>(\"2\") will return you the second argument without parameter (10000)\n"
" It also works with 'unsigned int', 'double', and 'float' types \n"
*/
class CV_EXPORTS CommandLineParser
class CV_EXPORTS CommandLineParserParams
{
public:
std::string help_message;
std::string def_value;
std::vector<std::string> keys;
int number;
};

//! the default constructor
CommandLineParser(int argc, const char* const argv[], const char* key_map);
template <typename T>
std::string get_type_name() { return "UNKNOW"; }

bool cmp_params(const CommandLineParserParams & p1, const CommandLineParserParams & p2);

template<typename T>
T from_str(const std::string & str)
{
T value;
std::stringstream ss(str);
ss >> value;

//! get parameter, you can choose: delete spaces in end and begin or not
template<typename _Tp>
_Tp get(const std::string& name, bool space_delete=true)
if (ss.fail())
{
if (!has(name))
{
return _Tp();
}
std::string str = getString(name);
return analyzeValue<_Tp>(str, space_delete);
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);
}

//! print short name, full name, current value and help for all params
void printParams();
return value;
}

protected:
std::map<std::string, std::vector<std::string> > data;
std::string getString(const std::string& name);
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();
}

bool has(const std::string& keys);
class CV_EXPORTS CommandLineParser
{
public:
CommandLineParser(int argc, const char * const argv[], const std::string keys);

template<typename _Tp>
_Tp analyzeValue(const std::string& str, bool space_delete=false);
std::string getPathToApplication();

template<typename _Tp>
static _Tp getData(const std::string& str)
{
_Tp res;
std::stringstream s1(str);
s1 >> res;
return res;
}
template <typename T>
T get(const std::string& name, bool space_delete = true)
{
try
{
for (size_t i = 0; i < data.size(); i++)
{
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();
}

template<typename _Tp>
_Tp fromStringNumber(const std::string& str);//the default conversion function for numbers
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)
{
error = true;
error_message += "Exception: " + std::string(e.what()) + "\n";
}
return T();
}

};
bool has(const std::string& name);

template<> CV_EXPORTS
bool CommandLineParser::get<bool>(const std::string& name, bool space_delete);
bool check();

template<> CV_EXPORTS
std::string CommandLineParser::analyzeValue<std::string>(const std::string& str, bool space_delete);
void about(std::string message);

template<> CV_EXPORTS
int CommandLineParser::analyzeValue<int>(const std::string& str, bool space_delete);
void printMessage();
void printErrors();

protected:
bool error;
std::string error_message;
std::string about_message;

template<> CV_EXPORTS
unsigned int CommandLineParser::analyzeValue<unsigned int>(const std::string& str, bool space_delete);
std::string path_to_app;
std::string app_name;

template<> CV_EXPORTS
uint64 CommandLineParser::analyzeValue<uint64>(const std::string& str, bool space_delete);
std::vector<CommandLineParserParams> data;

template<> CV_EXPORTS
float CommandLineParser::analyzeValue<float>(const std::string& str, bool space_delete);
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);

template<> CV_EXPORTS
double CommandLineParser::analyzeValue<double>(const std::string& str, bool space_delete);
void apply_params(std::string key, std::string value);
void apply_params(int i, std::string value);

void sort_params();

};

/////////////////////////////// Parallel Primitives //////////////////////////////////

Expand Down
Loading

0 comments on commit 54a202b

Please sign in to comment.