|
|
@ -7,6 +7,8 @@ |
|
|
|
#ifndef __ARGS__ |
|
|
|
#ifndef __ARGS__ |
|
|
|
#define __ARGS__ |
|
|
|
#define __ARGS__ |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#include <mrw/iomanip> |
|
|
|
|
|
|
|
|
|
|
|
#include <vector> |
|
|
|
#include <vector> |
|
|
|
#include <map> |
|
|
|
#include <map> |
|
|
|
#include <memory> |
|
|
|
#include <memory> |
|
|
@ -15,10 +17,11 @@ |
|
|
|
#include <sstream> |
|
|
|
#include <sstream> |
|
|
|
#include <stdexcept> |
|
|
|
#include <stdexcept> |
|
|
|
#include <numeric> |
|
|
|
#include <numeric> |
|
|
|
#include <cstdlib> |
|
|
|
#include <cstdlib> // exit() |
|
|
|
|
|
|
|
|
|
|
|
// check if code is compiled with a new C++11 compiler |
|
|
|
// check if code is compiled with a new C++11 compiler |
|
|
|
// otherwise there is a fallback wich makes everything much more compliacted |
|
|
|
// otherwise there is a fallback wich makes everything much more compliacted |
|
|
|
|
|
|
|
#ifndef ARGS__OLD_PRE11_COMPILER |
|
|
|
#if __cplusplus < 201103L |
|
|
|
#if __cplusplus < 201103L |
|
|
|
/// Code is compiled with an old non C++11 standard compliant compiler |
|
|
|
/// Code is compiled with an old non C++11 standard compliant compiler |
|
|
|
/** There are workarounds for old non C++11 compatible |
|
|
|
/** There are workarounds for old non C++11 compatible |
|
|
@ -33,6 +36,13 @@ |
|
|
|
#warning this is deprecated and will be removed in future releases |
|
|
|
#warning this is deprecated and will be removed in future releases |
|
|
|
#warning refere to the library documentation for more details |
|
|
|
#warning refere to the library documentation for more details |
|
|
|
#endif |
|
|
|
#endif |
|
|
|
|
|
|
|
#endif |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** @page oldcompiler Workaround for old non C++11 compilers |
|
|
|
|
|
|
|
... to be documented |
|
|
|
|
|
|
|
@note Old compilers are automatically detected and the flag |
|
|
|
|
|
|
|
@refs ARGS__OLD_PRE11_COMPILER is set. |
|
|
|
|
|
|
|
*/ |
|
|
|
|
|
|
|
|
|
|
|
#ifdef ARGS__OLD_PRE11_COMPILER |
|
|
|
#ifdef ARGS__OLD_PRE11_COMPILER |
|
|
|
#include <boost/shared_ptr.hpp> |
|
|
|
#include <boost/shared_ptr.hpp> |
|
|
@ -46,393 +56,333 @@ namespace std { |
|
|
|
}; |
|
|
|
}; |
|
|
|
#endif |
|
|
|
#endif |
|
|
|
|
|
|
|
|
|
|
|
/** @page oldcompiler Workaround for old non C++11 compilers |
|
|
|
namespace mrw { |
|
|
|
... to be documented |
|
|
|
/// Cool and easy evaluation of commandline arguments in C++11. |
|
|
|
@note Old compilers are automatically detected and the flag |
|
|
|
/** Evaluating command line arguments is extremely easy with this library: |
|
|
|
@refs ARGS__OLD_PRE11_COMPILER is set. |
|
|
|
@begincode |
|
|
|
*/ |
|
|
|
void test_func(); // bound to option --test |
|
|
|
|
|
|
|
|
|
|
|
/// Cool and easy evaluation of commandline arguments in C++11. |
|
|
|
int main(int argc, char** argv) try { |
|
|
|
/** Evaluating command line arguments is extremely easy with this library: |
|
|
|
|
|
|
|
@begincode |
|
|
|
// option variables |
|
|
|
void test_func(); // bound to option --test |
|
|
|
bool flag; // bound to option --flag |
|
|
|
|
|
|
|
int r(5); // bound to option --repeat |
|
|
|
int main(int argc, char** argv) try { |
|
|
|
std::string txt("Hello World"); // bound to option --text |
|
|
|
|
|
|
|
int x(0), y(0); // bound to option --coord |
|
|
|
// option variables |
|
|
|
|
|
|
|
bool flag; // bound to option --flag |
|
|
|
|
|
|
|
int r(5); // bound to option --repeat |
|
|
|
|
|
|
|
std::string txt("Hello World"); // bound to option --text |
|
|
|
|
|
|
|
int x(0), y(0); // bound to option --coord |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// bind and evaluate options |
|
|
|
// bind and evaluate options |
|
|
|
args::init(argc, argv, "This is an example for argument processing.", { |
|
|
|
args::parse(argc, argv, "This is an example for argument processing.", |
|
|
|
{"h", "help", "show this help", {args::help(), args::exit()}}, |
|
|
|
{ |
|
|
|
{"r", "repeat", "number of repetitions", {args::param(r, "number")}}, |
|
|
|
{"h", "help", "show this help", {args::help(), args::exit()}}, |
|
|
|
{"f", "flag", "check a flag", {args::param(flag)}}, |
|
|
|
{"r", "repeat", "number of repetitions", |
|
|
|
{"t", "text", "pass a text", {args::param(txt, "text")}}, |
|
|
|
{args::param(r, "number")}}, |
|
|
|
{"c", "coord", "add some 2D coordinates", {args::param(x, "x"), |
|
|
|
{"f", "flag", "check a flag", {args::param(flag)}}, |
|
|
|
args::param(y, "y")}}, |
|
|
|
{"t", "text", "pass a text", {args::param(txt, "text")}}, |
|
|
|
{"", "test", "call test_func, no shortcut", {args::func(test_func)}}, |
|
|
|
{"c", "coord", "add some 2D coordinates", {args::param(x, "x"), |
|
|
|
}); |
|
|
|
args::param(y, "y")}}, |
|
|
|
|
|
|
|
{"", "test", "call test_func, no shortcut", |
|
|
|
|
|
|
|
{args::func(test_func)}}, |
|
|
|
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
|
|
// [...] now the variables are setup according to the user's choice |
|
|
|
// [...] now the variables are setup according to the user's choice |
|
|
|
|
|
|
|
|
|
|
|
return 0; |
|
|
|
return 0; |
|
|
|
} catch (const std::exception& x) { // error in commandline options |
|
|
|
} catch (const std::exception& x) { // error in commandline options |
|
|
|
std::cerr<<"**** ERROR: "<<x.what()<<std::endl; |
|
|
|
std::cerr<<"**** ERROR: "<<x.what()<<std::endl; |
|
|
|
args::show_help(); // display help |
|
|
|
args::show_help(); // display help |
|
|
|
return 1; |
|
|
|
return 1; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
@endcode |
|
|
|
|
|
|
|
@note This library requires C++11, but there is also a fallback |
|
|
|
|
|
|
|
for old compilers. But the calling syntax is much worse on |
|
|
|
|
|
|
|
non C++11 compliant compilers, so the use of a modern |
|
|
|
|
|
|
|
compiler is strongly recommended. On GNU GCC, you may have |
|
|
|
|
|
|
|
to add the commandline option @c -std=c++11 |
|
|
|
|
|
|
|
@note I suggest to take this library into the next C++ standard. */ |
|
|
|
|
|
|
|
namespace args { |
|
|
|
|
|
|
|
/// Parent for holding a reference to a parameter variable. |
|
|
|
|
|
|
|
/** This class ist used only as parent for instances of template |
|
|
|
|
|
|
|
parameters. It represents one parameter that is evaluated when |
|
|
|
|
|
|
|
a commandline option is given. The derived classes hold a |
|
|
|
|
|
|
|
reference to a variable that is set in method @ref evaluate when |
|
|
|
|
|
|
|
the specific commandline parameter is given. */ |
|
|
|
|
|
|
|
class abstract_parameter { |
|
|
|
|
|
|
|
public: |
|
|
|
|
|
|
|
/// Called when the user specifies the related commandline option. |
|
|
|
|
|
|
|
/** Method must be implemented according to the specific type in |
|
|
|
|
|
|
|
the child classes. */ |
|
|
|
|
|
|
|
virtual void evaluate(char**& a, char** max) = 0; |
|
|
|
|
|
|
|
/// Convert the actual value to a string. |
|
|
|
|
|
|
|
/** Defaults to empty string, can be overwritten in child |
|
|
|
|
|
|
|
classes. Used by the help function to show the default |
|
|
|
|
|
|
|
value. */ |
|
|
|
|
|
|
|
virtual operator std::string() { |
|
|
|
|
|
|
|
return std::string(); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
virtual std::string type() { |
|
|
|
|
|
|
|
return std::string(); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
}; |
|
|
|
|
|
|
|
// default implementation for all types |
|
|
|
|
|
|
|
template<typename T> class parameter: public abstract_parameter { |
|
|
|
|
|
|
|
public: |
|
|
|
|
|
|
|
parameter(T& ref, const std::string& s = std::string()): |
|
|
|
|
|
|
|
_ref(ref), _type(s) {} |
|
|
|
|
|
|
|
virtual void evaluate(char**& a, char** max) { |
|
|
|
|
|
|
|
if (max<a+1) |
|
|
|
|
|
|
|
throw std::runtime_error("missing parameter for: "+std::string(*a)); |
|
|
|
|
|
|
|
std::stringstream ss; |
|
|
|
|
|
|
|
ss<<*++a; |
|
|
|
|
|
|
|
ss>>_ref; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
virtual operator std::string() { |
|
|
|
|
|
|
|
std::stringstream ss; |
|
|
|
|
|
|
|
ss<<_ref; |
|
|
|
|
|
|
|
return ss.str(); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
virtual std::string type() { |
|
|
|
|
|
|
|
return _type.size()?" <"+_type+">":""; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
private: |
|
|
|
|
|
|
|
T& _ref; |
|
|
|
|
|
|
|
std::string _type; |
|
|
|
|
|
|
|
}; |
|
|
|
|
|
|
|
// special case: boolean has no parameter |
|
|
|
|
|
|
|
template<> class parameter<bool>: public abstract_parameter { |
|
|
|
|
|
|
|
public: |
|
|
|
|
|
|
|
parameter(bool& ref, const std::string& = std::string()): |
|
|
|
|
|
|
|
_ref(ref=false) { |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
virtual void evaluate(char**&, char**) { |
|
|
|
|
|
|
|
_ref = true; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
private: |
|
|
|
|
|
|
|
bool& _ref; |
|
|
|
|
|
|
|
}; |
|
|
|
|
|
|
|
// special case: string cannot easily be shifted |
|
|
|
|
|
|
|
template<typename T> class parameter<std::basic_string<T> >: |
|
|
|
|
|
|
|
public abstract_parameter { |
|
|
|
|
|
|
|
public: |
|
|
|
|
|
|
|
parameter(std::basic_string<T>& ref, |
|
|
|
|
|
|
|
const std::string& s = std::string()): |
|
|
|
|
|
|
|
_ref(ref), _type(s) {} |
|
|
|
|
|
|
|
virtual void evaluate(char**& a, char** max) { |
|
|
|
|
|
|
|
if (max<a+1) |
|
|
|
|
|
|
|
throw std::runtime_error("missing parameter for: "+std::string(*a)); |
|
|
|
|
|
|
|
std::stringstream ss; |
|
|
|
|
|
|
|
ss<<*++a; |
|
|
|
|
|
|
|
_ref = std::basic_string<T>(std::istreambuf_iterator<T>(ss), |
|
|
|
|
|
|
|
std::istreambuf_iterator<T>()); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
virtual operator std::string() { |
|
|
|
|
|
|
|
std::stringstream ss; |
|
|
|
|
|
|
|
ss<<_ref; |
|
|
|
|
|
|
|
return ss.str(); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
virtual std::string type() { |
|
|
|
|
|
|
|
return _type.size()?" <"+_type+">":""; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
private: |
|
|
|
|
|
|
|
std::basic_string<T>& _ref; |
|
|
|
|
|
|
|
std::string _type; |
|
|
|
|
|
|
|
}; |
|
|
|
|
|
|
|
// special case: if a function is called |
|
|
|
|
|
|
|
template<> class parameter<void(*)()>: public abstract_parameter { |
|
|
|
|
|
|
|
public: |
|
|
|
|
|
|
|
parameter(void(*ref)(), const std::string& = std::string()): |
|
|
|
|
|
|
|
_ref(ref) { |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
virtual void evaluate(char**&, char**) { |
|
|
|
|
|
|
|
_ref(); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
private: |
|
|
|
|
|
|
|
void(*_ref)(); |
|
|
|
|
|
|
|
}; |
|
|
|
|
|
|
|
template<typename T> |
|
|
|
|
|
|
|
std::shared_ptr<abstract_parameter> param |
|
|
|
|
|
|
|
(T& t, const std::string& s = std::string()) { |
|
|
|
|
|
|
|
return std::shared_ptr<abstract_parameter>(new args::parameter<T>(t, s)); |
|
|
|
} |
|
|
|
} |
|
|
|
@endcode |
|
|
|
struct declaration { |
|
|
|
@note This library requires C++11, but there is also a fallback |
|
|
|
#ifdef ARGS__OLD_PRE11_COMPILER |
|
|
|
for old compilers. But the calling syntax is much worse on |
|
|
|
declaration(std::string p1, std::string p2, std::string p3, |
|
|
|
non C++11 compliant compilers, so the use of a modern |
|
|
|
std::vector<std::shared_ptr<abstract_parameter> > p4): |
|
|
|
compiler is strongly recommended. On GNU GCC, you may have |
|
|
|
short_arg(p1), long_arg(p2), desc(p3), params(p4) { |
|
|
|
to add the commandline option @c -std=c++11 |
|
|
|
} |
|
|
|
@note I suggest to take this library into the next C++ standard. |
|
|
|
|
|
|
|
*/ |
|
|
|
|
|
|
|
namespace args { |
|
|
|
|
|
|
|
/// Parent for holding a reference to a parameter variable. |
|
|
|
|
|
|
|
/** This class ist used only as parent for instances of template |
|
|
|
|
|
|
|
parameters. It represents one parameter that is evaluated when |
|
|
|
|
|
|
|
a commandline option is given. The derived classes hold a |
|
|
|
|
|
|
|
reference to a variable that is set in method @ref evaluate when |
|
|
|
|
|
|
|
the specific commandline parameter is given. */ |
|
|
|
|
|
|
|
class abstract_parameter { |
|
|
|
|
|
|
|
public: |
|
|
|
|
|
|
|
/// Called when the user specifies the related commandline option. |
|
|
|
|
|
|
|
/** Method must be implemented according to the specific type in |
|
|
|
|
|
|
|
the child classes. */ |
|
|
|
|
|
|
|
virtual void evaluate(char**& a, char** max) = 0; |
|
|
|
|
|
|
|
/// Convert the actual value to a string. |
|
|
|
|
|
|
|
/** Defaults to empty string, can be overwritten in child |
|
|
|
|
|
|
|
classes. Used by the help function to show the default |
|
|
|
|
|
|
|
value. */ |
|
|
|
|
|
|
|
virtual operator std::string() { |
|
|
|
|
|
|
|
return std::string(); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
virtual std::string type() { |
|
|
|
|
|
|
|
return std::string(); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
}; |
|
|
|
|
|
|
|
// default implementation for all types |
|
|
|
|
|
|
|
template<typename T> class parameter: public abstract_parameter { |
|
|
|
|
|
|
|
public: |
|
|
|
|
|
|
|
parameter(T& ref, const std::string& s = std::string()): |
|
|
|
|
|
|
|
_ref(ref), _type(s) {} |
|
|
|
|
|
|
|
virtual void evaluate(char**& a, char** max) { |
|
|
|
|
|
|
|
if (max<a+1) |
|
|
|
|
|
|
|
throw std::runtime_error("missing parameter for: "+std::string(*a)); |
|
|
|
|
|
|
|
std::stringstream ss; |
|
|
|
|
|
|
|
ss<<*++a; |
|
|
|
|
|
|
|
ss>>_ref; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
virtual operator std::string() { |
|
|
|
|
|
|
|
std::stringstream ss; |
|
|
|
|
|
|
|
ss<<_ref; |
|
|
|
|
|
|
|
return ss.str(); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
virtual std::string type() { |
|
|
|
|
|
|
|
return _type.size()?" <"+_type+">":""; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
private: |
|
|
|
|
|
|
|
T& _ref; |
|
|
|
|
|
|
|
std::string _type; |
|
|
|
|
|
|
|
}; |
|
|
|
|
|
|
|
// special case: boolean has no parameter |
|
|
|
|
|
|
|
template<> class parameter<bool>: public abstract_parameter { |
|
|
|
|
|
|
|
public: |
|
|
|
|
|
|
|
parameter(bool& ref, const std::string& = std::string()): _ref(ref=false) {} |
|
|
|
|
|
|
|
virtual void evaluate(char**&, char**) { |
|
|
|
|
|
|
|
_ref = true; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
private: |
|
|
|
|
|
|
|
bool& _ref; |
|
|
|
|
|
|
|
}; |
|
|
|
|
|
|
|
// special case: string cannot easily be shifted |
|
|
|
|
|
|
|
template<typename T> class parameter<std::basic_string<T> >: public abstract_parameter { |
|
|
|
|
|
|
|
public: |
|
|
|
|
|
|
|
parameter(std::basic_string<T>& ref, const std::string& s = std::string()): |
|
|
|
|
|
|
|
_ref(ref), _type(s) {} |
|
|
|
|
|
|
|
virtual void evaluate(char**& a, char** max) { |
|
|
|
|
|
|
|
if (max<a+1) |
|
|
|
|
|
|
|
throw std::runtime_error("missing parameter for: "+std::string(*a)); |
|
|
|
|
|
|
|
std::stringstream ss; |
|
|
|
|
|
|
|
ss<<*++a; |
|
|
|
|
|
|
|
_ref = std::basic_string<T>(std::istreambuf_iterator<T>(ss), |
|
|
|
|
|
|
|
std::istreambuf_iterator<T>()); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
virtual operator std::string() { |
|
|
|
|
|
|
|
std::stringstream ss; |
|
|
|
|
|
|
|
ss<<_ref; |
|
|
|
|
|
|
|
return ss.str(); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
virtual std::string type() { |
|
|
|
|
|
|
|
return _type.size()?" <"+_type+">":""; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
private: |
|
|
|
|
|
|
|
std::basic_string<T>& _ref; |
|
|
|
|
|
|
|
std::string _type; |
|
|
|
|
|
|
|
}; |
|
|
|
|
|
|
|
// special case: if a function is called |
|
|
|
|
|
|
|
template<> class parameter<void(*)()>: public abstract_parameter { |
|
|
|
|
|
|
|
public: |
|
|
|
|
|
|
|
parameter(void(*ref)(), const std::string& = std::string()): _ref(ref) {} |
|
|
|
|
|
|
|
virtual void evaluate(char**&, char**) { |
|
|
|
|
|
|
|
_ref(); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
private: |
|
|
|
|
|
|
|
void(*_ref)(); |
|
|
|
|
|
|
|
}; |
|
|
|
|
|
|
|
template<typename T> |
|
|
|
|
|
|
|
std::shared_ptr<abstract_parameter> param |
|
|
|
|
|
|
|
(T& t, const std::string& s = std::string()) { |
|
|
|
|
|
|
|
return std::shared_ptr<abstract_parameter>(new args::parameter<T>(t, s)); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
struct declaration { |
|
|
|
|
|
|
|
#ifndef ARGS__CPLUSPLUS11 |
|
|
|
|
|
|
|
declaration(std::string p1, std::string p2, std::string p3, |
|
|
|
|
|
|
|
std::vector<std::shared_ptr<abstract_parameter> > p4): |
|
|
|
|
|
|
|
short_arg(p1), long_arg(p2), desc(p3), params(p4) { |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
#endif |
|
|
|
#endif |
|
|
|
std::string short_arg; |
|
|
|
std::string short_arg; |
|
|
|
std::string long_arg; |
|
|
|
std::string long_arg; |
|
|
|
std::string desc; |
|
|
|
std::string desc; |
|
|
|
std::vector<std::shared_ptr<abstract_parameter> > params; |
|
|
|
std::vector<std::shared_ptr<abstract_parameter> > params; |
|
|
|
}; |
|
|
|
}; |
|
|
|
typedef std::map<std::string, |
|
|
|
typedef std::map<std::string, |
|
|
|
std::vector<std::shared_ptr<abstract_parameter> > > |
|
|
|
std::vector<std::shared_ptr<abstract_parameter> > > |
|
|
|
arg_map; |
|
|
|
arg_map; |
|
|
|
static arg_map& args() { |
|
|
|
static arg_map& args() { |
|
|
|
static arg_map a; |
|
|
|
static arg_map a; |
|
|
|
return a; |
|
|
|
return a; |
|
|
|
} |
|
|
|
} |
|
|
|
typedef std::vector<declaration> list; |
|
|
|
typedef std::vector<declaration> list; |
|
|
|
static list& arg_list() { |
|
|
|
static list& arg_list() { |
|
|
|
static list a; |
|
|
|
static list a; |
|
|
|
return a; |
|
|
|
return a; |
|
|
|
} |
|
|
|
} |
|
|
|
void match(const std::string& arg, char**& a, char** max) { |
|
|
|
void match(const std::string& arg, char**& a, char** max) { |
|
|
|
#ifndef ARGS__CPLUSPLUS11 |
|
|
|
#ifdef ARGS__OLD_PRE11_COMPILER |
|
|
|
arg_map::iterator it(args().find(arg)); |
|
|
|
arg_map::iterator it(args().find(arg)); |
|
|
|
#else |
|
|
|
#else |
|
|
|
auto it(args().find(arg)); |
|
|
|
auto it(args().find(arg)); |
|
|
|
#endif |
|
|
|
#endif |
|
|
|
if (it==args().end()) |
|
|
|
if (it==args().end()) |
|
|
|
throw std::runtime_error("unknown argument: "+std::string(*a)); |
|
|
|
throw std::runtime_error("unknown argument: "+std::string(*a)); |
|
|
|
#ifndef ARGS__CPLUSPLUS11 |
|
|
|
#ifdef ARGS__OLD_PRE11_COMPILER |
|
|
|
for (std::vector<std::shared_ptr<abstract_parameter> >::iterator |
|
|
|
for (std::vector<std::shared_ptr<abstract_parameter> >::iterator |
|
|
|
it2(it->second.begin()); it2!=it->second.end(); ++it2) { |
|
|
|
it2(it->second.begin()); it2!=it->second.end(); ++it2) |
|
|
|
#else |
|
|
|
#else |
|
|
|
for (auto it2(it->second.begin()); it2!=it->second.end(); ++it2) { |
|
|
|
for (auto it2(it->second.begin()); it2!=it->second.end(); ++it2) |
|
|
|
#endif |
|
|
|
#endif |
|
|
|
(*it2)->evaluate(a, max); |
|
|
|
(*it2)->evaluate(a, max); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
/// Filename as passed in argv[0]. |
|
|
|
/// Filename as passed in argv[0]. |
|
|
|
/** Used in the help display. |
|
|
|
/** Used in the help display. |
|
|
|
@note This function emulates a global variable using parts of a |
|
|
|
@note This function emulates a global variable using parts of a |
|
|
|
singleton pattern. */ |
|
|
|
singleton pattern. */ |
|
|
|
static std::string filename(const std::string& arg0 = std::string()) { |
|
|
|
static std::string filename(const std::string& arg0 = std::string()) { |
|
|
|
static std::string file(arg0); |
|
|
|
static std::string file(arg0); |
|
|
|
return file; |
|
|
|
return file; |
|
|
|
} |
|
|
|
} |
|
|
|
/// Description of the program. |
|
|
|
/// Description of the program. |
|
|
|
/** Used in the help display. |
|
|
|
/** Used in the help display. |
|
|
|
@note This function emulates a global variable using parts of a |
|
|
|
@note This function emulates a global variable using parts of a |
|
|
|
singleton pattern. */ |
|
|
|
singleton pattern. */ |
|
|
|
static std::string description(const std::string& desc = std::string()) { |
|
|
|
static std::string description(const std::string& desc = std::string()) { |
|
|
|
static std::string d(desc); |
|
|
|
static std::string d(desc); |
|
|
|
return d; |
|
|
|
return d; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
/// Return values of the program. |
|
|
|
|
|
|
|
/** Used in the help display. |
|
|
|
|
|
|
|
@note This function emulates a global variable using parts of a |
|
|
|
|
|
|
|
singleton pattern. */ |
|
|
|
|
|
|
|
static std::string returns(const std::string& ret = std::string()) { |
|
|
|
|
|
|
|
static std::string r(ret); |
|
|
|
|
|
|
|
return r; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
/// Initialize all parameters according to the commandline options. |
|
|
|
|
|
|
|
/** Sets up the parser from the @ref list of parameters |
|
|
|
|
|
|
|
@param argc the argument count as given in C++ @c main function |
|
|
|
|
|
|
|
@param argv the array of arguments as given in C++ @c main function |
|
|
|
|
|
|
|
@param descr a string describing what the program does, |
|
|
|
|
|
|
|
used in @ref show_help |
|
|
|
|
|
|
|
@param l list of options and parameters to be parsed |
|
|
|
|
|
|
|
@raram ret documentation of the return values of the program */ |
|
|
|
|
|
|
|
static void init(int argc, char** argv, const std::string& desc, list l, |
|
|
|
|
|
|
|
const std::string& ret = std::string()) { |
|
|
|
|
|
|
|
filename(argv[0]); // store filename for later use in help |
|
|
|
|
|
|
|
description(desc); // store program description for later use in help |
|
|
|
|
|
|
|
returns(ret); // store return values description for later use in help |
|
|
|
|
|
|
|
arg_list() = l; // store options and parameters for later use in help |
|
|
|
|
|
|
|
// setup the argument mapping table |
|
|
|
|
|
|
|
#ifndef ARGS__CPLUSPLUS11 |
|
|
|
|
|
|
|
for (list::iterator it(l.begin()); it!=l.end(); ++it) { |
|
|
|
|
|
|
|
#else |
|
|
|
|
|
|
|
for (auto it(l.begin()); it!=l.end(); ++it) { |
|
|
|
|
|
|
|
#endif |
|
|
|
|
|
|
|
if (it->short_arg.size()==1) args()[it->short_arg] = it->params; |
|
|
|
|
|
|
|
if (it->long_arg.size()) args()["--"+it->long_arg] = it->params; |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
// parse commandline and evaluate the arguments |
|
|
|
/// Return values of the program. |
|
|
|
#ifndef ARGS__CPLUSPLUS11 |
|
|
|
/** Used in the help display. |
|
|
|
for (char** a(argv+1); a<argv+argc; ++a) { |
|
|
|
@note This function emulates a global variable using parts of a |
|
|
|
|
|
|
|
singleton pattern. */ |
|
|
|
|
|
|
|
static std::string returns(const std::string& ret = std::string()) { |
|
|
|
|
|
|
|
static std::string r(ret); |
|
|
|
|
|
|
|
return r; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
/// Initialize all parameters according to the commandline options. |
|
|
|
|
|
|
|
/** Sets up the parser from the @ref list of parameters |
|
|
|
|
|
|
|
@param argc the argument count as given in C++ @c main function |
|
|
|
|
|
|
|
@param argv the array of arguments as given in C++ @c main function |
|
|
|
|
|
|
|
@param descr a string describing what the program does, |
|
|
|
|
|
|
|
used in @ref show_help |
|
|
|
|
|
|
|
@param l list of options and parameters to be parsed |
|
|
|
|
|
|
|
@raram ret documentation of the return values of the program */ |
|
|
|
|
|
|
|
static void parse(int argc, char** argv, const std::string& desc, list l, |
|
|
|
|
|
|
|
const std::string& ret = std::string()) { |
|
|
|
|
|
|
|
filename(argv[0]); // store filename for later use in help |
|
|
|
|
|
|
|
description(desc); // store program description for later use in help |
|
|
|
|
|
|
|
returns(ret); // store return documentation for later use in help |
|
|
|
|
|
|
|
arg_list() = l; // store options and parameters for later use in help |
|
|
|
|
|
|
|
// setup the argument mapping table |
|
|
|
|
|
|
|
#ifdef ARGS__OLD_PRE11_COMPILER |
|
|
|
|
|
|
|
for (list::iterator it(l.begin()); it!=l.end(); ++it) |
|
|
|
#else |
|
|
|
#else |
|
|
|
for (auto a(argv+1); a<argv+argc; ++a) { |
|
|
|
for (auto it(l.begin()); it!=l.end(); ++it) |
|
|
|
#endif |
|
|
|
#endif |
|
|
|
std::string arg(*a); |
|
|
|
{ |
|
|
|
if (arg.size()>1&&arg[0]=='-'&&arg[1]!='-') { // short argument |
|
|
|
if (it->short_arg.size()==1) args()[it->short_arg] = it->params; |
|
|
|
#ifndef ARGS__CPLUSPLUS11 |
|
|
|
if (it->long_arg.size()) args()["--"+it->long_arg] = it->params; |
|
|
|
for (std::string::iterator it(arg.begin()+1); it!=arg.end(); ++it) |
|
|
|
} |
|
|
|
|
|
|
|
// parse commandline and evaluate the arguments |
|
|
|
|
|
|
|
#ifdef ARGS__OLD_PRE11_COMPILER |
|
|
|
|
|
|
|
for (char** a(argv+1); a<argv+argc; ++a) |
|
|
|
#else |
|
|
|
#else |
|
|
|
for (auto it(arg.begin()+1); it!=arg.end(); ++it) |
|
|
|
for (auto a(argv+1); a<argv+argc; ++a) |
|
|
|
#endif |
|
|
|
#endif |
|
|
|
match(std::string(1, *it), a, argv+argc); |
|
|
|
{ |
|
|
|
} else { // long argument or wrong argument |
|
|
|
std::string arg(*a); |
|
|
|
match(arg, a, argv+argc); |
|
|
|
if (arg.size()>1&&arg[0]=='-'&&arg[1]!='-') { // short argument |
|
|
|
} |
|
|
|
#ifdef ARGS__OLD_PRE11_COMPILER |
|
|
|
} |
|
|
|
for (std::string::iterator it(arg.begin()+1); it!=arg.end(); ++it) |
|
|
|
} |
|
|
|
|
|
|
|
//! IO-Manipulator to split long lines into several shorter lines. |
|
|
|
|
|
|
|
template <class _CharT = char, class _Traits = std::char_traits<_CharT> > |
|
|
|
|
|
|
|
class basic_split /*: public std::basic_ostream<_CharT, _Traits>*/ { |
|
|
|
|
|
|
|
public: |
|
|
|
|
|
|
|
basic_split(int width=80, int indent=0, char fill=' '): |
|
|
|
|
|
|
|
_width(width), _indent(indent), _fill(fill), _os(0) { |
|
|
|
|
|
|
|
if (_width<_indent) |
|
|
|
|
|
|
|
#ifndef ARGS__CPLUSPLUS11 |
|
|
|
|
|
|
|
throw std::runtime_error(((std::stringstream&) |
|
|
|
|
|
|
|
(std::stringstream() |
|
|
|
|
|
|
|
<<"wrong use of split: width is "<<_width |
|
|
|
|
|
|
|
<<" but should be larger than indent," |
|
|
|
|
|
|
|
<<" which is "<<_indent)).str()); |
|
|
|
|
|
|
|
#else |
|
|
|
#else |
|
|
|
throw std::runtime_error("wrong use of split: width is "+ |
|
|
|
for (auto it(arg.begin()+1); it!=arg.end(); ++it) |
|
|
|
std::to_string(_width)+ |
|
|
|
|
|
|
|
" but should be larger than indent," |
|
|
|
|
|
|
|
" which is "+std::to_string(_indent)); |
|
|
|
|
|
|
|
#endif |
|
|
|
#endif |
|
|
|
} |
|
|
|
match(std::string(1, *it), a, argv+argc); |
|
|
|
virtual ~basic_split() { |
|
|
|
} else { // long argument or wrong argument |
|
|
|
flush(); |
|
|
|
match(arg, a, argv+argc); |
|
|
|
} |
|
|
|
|
|
|
|
friend basic_split& operator<<(std::basic_ostream<_CharT, _Traits>& os, |
|
|
|
|
|
|
|
const basic_split& s) { |
|
|
|
|
|
|
|
if (s._os!=&os) { |
|
|
|
|
|
|
|
const_cast<basic_split&>(s).flush(); |
|
|
|
|
|
|
|
const_cast<basic_split&>(s)._os = &os; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
return const_cast<basic_split&>(s); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
template <typename T> basic_split& operator<<(T t) { |
|
|
|
|
|
|
|
if (!_os) |
|
|
|
|
|
|
|
throw std::runtime_error("wrong use of split, it is an io manipulator" |
|
|
|
|
|
|
|
" and must be used within a stream"); |
|
|
|
|
|
|
|
_buffer<<t; |
|
|
|
|
|
|
|
while (_buffer.str().size()>=_width-_indent) { |
|
|
|
|
|
|
|
if (_indent) *_os<<std::setw(_indent)<<std::setfill(_fill)<<_fill; |
|
|
|
|
|
|
|
std::string::size_type pos |
|
|
|
|
|
|
|
(_buffer.str().find_last_of(' ', _width-_indent)); |
|
|
|
|
|
|
|
std::string::size_type next(pos+1); |
|
|
|
|
|
|
|
if (pos==std::string::npos) { |
|
|
|
|
|
|
|
pos=_width-_indent; |
|
|
|
|
|
|
|
next=pos; |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
*_os<<_buffer.str().substr(0, pos)<<std::endl; |
|
|
|
|
|
|
|
_buffer.str(_buffer.str().substr(next)); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
return *this; |
|
|
|
} |
|
|
|
} |
|
|
|
void show_help(const std::string& synopsis_txt="SYNOPSIS", |
|
|
|
private: |
|
|
|
const std::string& description_txt="DESCRIPTION", |
|
|
|
void flush() { |
|
|
|
const std::string& options_txt="OPTIONS", |
|
|
|
if (_os) { |
|
|
|
const std::string& returns_txt="RETURNS", |
|
|
|
if (_indent) *_os<<std::setw(_indent)<<std::setfill(_fill)<<_fill; |
|
|
|
int max_line=80, int indent=2, int long_indent=4, |
|
|
|
*_os<<_buffer.str(); |
|
|
|
int option_len=16, int param_len=21) { |
|
|
|
_buffer.clear(); |
|
|
|
std::cout<<synopsis_txt<<std::endl<<std::endl |
|
|
|
} |
|
|
|
<<" "<<filename()<<" ["<<options_txt<<"]"<<std::endl<<std::endl |
|
|
|
} |
|
|
|
<<description_txt<<std::endl<<std::endl |
|
|
|
private: |
|
|
|
<<split(max_line, indent)<<description(); //! @bug |
|
|
|
int _width; |
|
|
|
std::cout<<std::endl<<std::endl |
|
|
|
int _indent; |
|
|
|
<<options_txt<<std::endl; |
|
|
|
char _fill; |
|
|
|
#ifdef ARGS__OLD_PRE11_COMPILER |
|
|
|
std::stringstream _buffer; |
|
|
|
for (list::iterator arg(arg_list().begin()); arg!=arg_list().end(); ++arg) |
|
|
|
std::basic_ostream<_CharT, _Traits>* _os; |
|
|
|
|
|
|
|
}; |
|
|
|
|
|
|
|
typedef basic_split<char> split; |
|
|
|
|
|
|
|
void show_help(const std::string& synopsis_txt="SYNOPSIS", |
|
|
|
|
|
|
|
const std::string& description_txt="DESCRIPTION", |
|
|
|
|
|
|
|
const std::string& options_txt="OPTIONS", |
|
|
|
|
|
|
|
const std::string& returns_txt="RETURNS", |
|
|
|
|
|
|
|
int max_line=80, int indent=2, int long_indent=4, |
|
|
|
|
|
|
|
int option_len=16, int param_len=21) { |
|
|
|
|
|
|
|
std::cout<<synopsis_txt<<std::endl<<std::endl |
|
|
|
|
|
|
|
<<" "<<filename()<<" ["<<options_txt<<"]"<<std::endl<<std::endl |
|
|
|
|
|
|
|
<<description_txt<<std::endl<<std::endl |
|
|
|
|
|
|
|
<<split(max_line, indent)<<description(); //! @bug |
|
|
|
|
|
|
|
std::cout<<std::endl<<std::endl |
|
|
|
|
|
|
|
<<options_txt<<std::endl; |
|
|
|
|
|
|
|
#ifndef ARGS__CPLUSPLUS11 |
|
|
|
|
|
|
|
for (list::iterator arg(arg_list().begin()); arg!=arg_list().end(); ++arg) { |
|
|
|
|
|
|
|
#else |
|
|
|
#else |
|
|
|
for (auto arg(arg_list().begin()); arg!=arg_list().end(); ++arg) { |
|
|
|
for (auto arg(arg_list().begin()); arg!=arg_list().end(); ++arg) |
|
|
|
#endif |
|
|
|
#endif |
|
|
|
std::string o((arg->short_arg.size()?"-"+arg->short_arg:"")+ |
|
|
|
{ |
|
|
|
(arg->short_arg.size()&&arg->long_arg.size()?", ":"")+ |
|
|
|
std::string o((arg->short_arg.size()?"-"+arg->short_arg:"")+ |
|
|
|
(arg->long_arg.size()?"--"+arg->long_arg:"")); |
|
|
|
(arg->short_arg.size()&&arg->long_arg.size()?", ":"")+ |
|
|
|
#ifndef ARGS__CPLUSPLUS11 |
|
|
|
(arg->long_arg.size()?"--"+arg->long_arg:"")); |
|
|
|
std::string a; |
|
|
|
#ifdef ARGS__OLD_PRE11_COMPILER |
|
|
|
for (std::vector<std::shared_ptr<abstract_parameter> >::iterator |
|
|
|
std::string a; |
|
|
|
p(arg->params.begin()); |
|
|
|
for (std::vector<std::shared_ptr<abstract_parameter> >::iterator |
|
|
|
p!=arg->params.end(); ++p) { |
|
|
|
p(arg->params.begin()); |
|
|
|
std::string def(**p); |
|
|
|
p!=arg->params.end(); ++p) { |
|
|
|
a+=(*p)->type()+(def.size()?"="+def:""); |
|
|
|
std::string def(**p); |
|
|
|
} |
|
|
|
a+=(*p)->type()+(def.size()?"="+def:""); |
|
|
|
|
|
|
|
} |
|
|
|
#else |
|
|
|
#else |
|
|
|
std::string a(std::accumulate(arg->params.begin(), arg->params.end(), |
|
|
|
std::string a(std::accumulate(arg->params.begin(), arg->params.end(), |
|
|
|
std::string(), |
|
|
|
std::string(), |
|
|
|
[](const std::string& s, |
|
|
|
[](const std::string& s, |
|
|
|
std::shared_ptr<abstract_parameter> p) |
|
|
|
std::shared_ptr<abstract_parameter> p) |
|
|
|
-> std::string { |
|
|
|
-> std::string { |
|
|
|
std::string def(**p); |
|
|
|
std::string def(**p); |
|
|
|
return s+p->type()+(def.size()?"="+def:""); |
|
|
|
return s+p->type()+(def.size()?"="+def:""); |
|
|
|
})); |
|
|
|
})); |
|
|
|
#endif |
|
|
|
#endif |
|
|
|
std::cout<<std::endl<<" " |
|
|
|
std::cout<<std::endl<<" " |
|
|
|
<<o<<std::setw(option_len-o.size())<<' ' |
|
|
|
<<o<<std::setw(option_len-o.size())<<' ' |
|
|
|
<<a; |
|
|
|
<<a; |
|
|
|
if (arg->desc.size()>max_line-indent-option_len-param_len) |
|
|
|
if (arg->desc.size()>max_line-indent-option_len-param_len) |
|
|
|
std::cout<<std::endl<<std::endl<<split(max_line, long_indent) |
|
|
|
std::cout<<std::endl<<std::endl<<split(max_line, long_indent) |
|
|
|
<<arg->desc; |
|
|
|
<<arg->desc; |
|
|
|
else |
|
|
|
else |
|
|
|
std::cout<<std::setw(param_len-a.size())<<' '<<arg->desc; |
|
|
|
std::cout<<std::setw(param_len-a.size())<<' '<<arg->desc; |
|
|
|
std::cout<<std::endl; |
|
|
|
std::cout<<std::endl; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
if (returns().size()) { |
|
|
|
|
|
|
|
std::cout<<std::endl<<returns_txt<<std::endl<<std::endl |
|
|
|
|
|
|
|
<<split(max_line, indent)<<returns(); |
|
|
|
|
|
|
|
std::cout<<std::endl; |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
if (returns().size()) { |
|
|
|
void help_no_arg() { |
|
|
|
std::cout<<std::endl<<returns_txt<<std::endl<<std::endl |
|
|
|
show_help(); |
|
|
|
<<split(max_line, indent)<<returns(); |
|
|
|
} |
|
|
|
std::cout<<std::endl; |
|
|
|
void do_exit() { |
|
|
|
|
|
|
|
exit(1); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
std::shared_ptr<args::abstract_parameter> func(void(*fn)()) { |
|
|
|
|
|
|
|
return |
|
|
|
|
|
|
|
std::shared_ptr<args::abstract_parameter> |
|
|
|
|
|
|
|
(new args::parameter<void(*)()>(fn)); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
std::shared_ptr<args::abstract_parameter> help() { |
|
|
|
|
|
|
|
return func(args::help_no_arg); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
std::shared_ptr<args::abstract_parameter> exit() { |
|
|
|
|
|
|
|
return func(args::do_exit); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
void help_no_arg() { |
|
|
|
|
|
|
|
show_help(); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
void do_exit() { |
|
|
|
|
|
|
|
exit(1); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
std::shared_ptr<args::abstract_parameter> func(void(*fn)()) { |
|
|
|
|
|
|
|
return |
|
|
|
|
|
|
|
std::shared_ptr<args::abstract_parameter> |
|
|
|
|
|
|
|
(new args::parameter<void(*)()>(fn)); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
std::shared_ptr<args::abstract_parameter> help() { |
|
|
|
|
|
|
|
return func(args::help_no_arg); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
std::shared_ptr<args::abstract_parameter> exit() { |
|
|
|
|
|
|
|
return func(args::do_exit); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
#endif |
|
|
|
#endif |
|
|
|