41 std::istream&
operator>>(std::istream& is, std::vector<int>& v);
42 std::ostream& operator<<(std::ostream& os, const std::vector<int>& v);
43 std::istream&
operator>>(std::istream& is, std::vector<double>& v);
44 std::ostream& operator<<(std::ostream& os, const std::vector<double>& v);
46 std::istream&
operator>>(std::istream& is, std::vector<int>& v){
51 const char* c = s.c_str();
52 char* caux =
const_cast<char*
>(c);
55 bool hasNextValue=
true;
57 int i =
static_cast<int>(strtol(c,&caux,10));
68 std::ostream& operator<<(std::ostream& os, const std::vector<int>& v){
72 for (
size_t i=1; i<v.size(); i++){
84 std::istringstream i(s);
91 template<
class T1,
class T2,
class Pred = std::less<T1> >
93 bool operator()(
const std::pair<T1,T2>& left,
const std::pair<T1,T2>& right) {
94 return Pred()(left.first, right.first);
103 CommandArgs::CommandArgs()
107 CommandArgs::~CommandArgs()
111 bool CommandArgs::parseArgs(
int argc,
char** argv,
bool exitOnError)
115 for (i = 1; i < argc; i++) {
116 string name = argv[i];
118 if (name[0] !=
'-') {
132 string::size_type dashPos = name.find_first_not_of(
'-');
133 if (dashPos != string::npos)
134 name = name.substr(dashPos);
136 if (name ==
"help" || name ==
"h") {
142 std::vector<CommandArgument>::iterator it = _args.begin();
143 for ( ; it != _args.end(); ++it) {
144 if (it->name == name) {
147 bool* data =
static_cast<bool*
>(it->data);
153 cerr <<
"Argument " << name <<
"needs value.\n";
160 str2arg(argv[i], *it);
166 if (it == _args.end()) {
167 cerr <<
"Error: Unknown Option '" << name <<
"' (use -help to get list of options).\n";
177 if ((
int)_leftOvers.size() > argc - i) {
178 cerr <<
"Error: program requires parameters" << endl;
184 for (
size_t j = 0; (i < argc && j < _leftOvers.size()); i++, j++) {
185 string* s =
static_cast<string*
>(_leftOvers[j].data);
190 for (
size_t j = 0; (i < argc && j < _leftOversOptional.size()); i++, j++) {
191 string* s =
static_cast<string*
>(_leftOversOptional[j].data);
198 void CommandArgs::param(
const std::string& name,
bool& p,
bool defValue,
const std::string& desc)
204 ca.
data =
static_cast<void*
>(&p);
210 void CommandArgs::param(
const std::string& name,
int& p,
int defValue,
const std::string& desc)
216 ca.
data =
static_cast<void*
>(&p);
222 void CommandArgs::param(
const std::string& name,
float& p,
float defValue,
const std::string& desc)
228 ca.
data =
static_cast<void*
>(&p);
234 void CommandArgs::param(
const std::string& name,
double& p,
double defValue,
const std::string& desc)
240 ca.
data =
static_cast<void*
>(&p);
246 void CommandArgs::param(
const std::string& name, std::string& p,
const std::string& defValue,
const std::string& desc)
252 ca.
data =
static_cast<void*
>(&p);
258 void CommandArgs::param(
const std::string& name, std::vector<int>& p,
const std::vector<int>& defValue,
const std::string& desc){
263 ca.
data =
static_cast<void*
>(&p);
269 void CommandArgs::param(
const std::string& name, std::vector<double>& p,
const std::vector<double>& defValue,
const std::string& desc)
275 ca.
data =
static_cast<void*
>(&p);
281 void CommandArgs::printHelp(std::ostream& os)
284 os << _banner << endl;
285 os <<
"Usage: " << _progName << (_args.size()>0?
" [options] ":
" ");
286 if (_leftOvers.size() > 0) {
287 for (
size_t i = 0; i < _leftOvers.size(); ++i) {
290 os << _leftOvers[i].name;
293 if (_leftOversOptional.size() > 0) {
294 if (_leftOvers.size() > 0)
296 for (
size_t i = 0; i < _leftOversOptional.size(); ++i) {
299 os <<
"[" << _leftOversOptional[i].name <<
"]";
303 os <<
"General options:" << endl;
304 os <<
"-------------------------------------------" << endl;
305 os <<
"-help / -h Displays this help." << endl << endl;
306 if (_args.size() > 0) {
307 os <<
"Program Options:" << endl;
308 os <<
"-------------------------------------------" << endl;
310 vector<pair<string, string> > tableStrings;
311 tableStrings.reserve(_args.size());
312 size_t maxArgLen = 0;
313 for (
size_t i = 0; i < _args.size(); ++i) {
315 string defaultValueStr = arg2str(_args[i]);
316 if (! defaultValueStr.empty())
317 tableStrings.push_back(make_pair(_args[i].name +
" " + type2str(_args[i].type), _args[i].description +
" (default: " + defaultValueStr +
")"));
319 tableStrings.push_back(make_pair(_args[i].name +
" " + type2str(_args[i].type), _args[i].description));
321 tableStrings.push_back(make_pair(_args[i].name, _args[i].description));
322 maxArgLen = (std::max)(maxArgLen, tableStrings.back().first.size());
324 sort(tableStrings.begin(), tableStrings.end(), CmpPairFirst<string,string>());
326 for (
size_t i = 0; i < tableStrings.size(); ++i) {
327 os <<
"-" << tableStrings[i].first;
328 for (
size_t l = tableStrings[i].first.size(); l < maxArgLen; ++l)
330 os << tableStrings[i].second << endl;
337 void CommandArgs::setBanner(
const std::string& banner)
342 void CommandArgs::paramLeftOver(
const std::string& name, std::string& p,
const std::string& defValue,
const std::string& desc,
bool optional)
348 ca.
data =
static_cast<void*
>(&p);
353 _leftOversOptional.push_back(ca);
355 _leftOvers.push_back(ca);
358 const char* CommandArgs::type2str(
int t)
const 372 return "<vector_int>";
373 case CAT_VECTOR_DOUBLE:
374 return "<vector_double>";
387 float* data =
static_cast<float*
>(ca.
data);
397 double* data =
static_cast<double*
>(ca.
data);
407 int* data =
static_cast<int*
>(ca.
data);
417 bool* data =
static_cast<bool*
>(ca.
data);
424 string* data =
static_cast<string*
>(ca.
data);
430 std::vector<int> aux;
433 std::vector<int>* data =
static_cast< std::vector<int>*
>(ca.
data);
438 case CAT_VECTOR_DOUBLE:
440 std::vector<double> aux;
443 std::vector<double>* data =
static_cast< std::vector<double>*
>(ca.
data);
456 float* data =
static_cast<float*
>(ca.
data);
457 stringstream auxStream;
459 return auxStream.str();
463 double* data =
static_cast<double*
>(ca.
data);
464 stringstream auxStream;
466 return auxStream.str();
470 int* data =
static_cast<int*
>(ca.
data);
471 stringstream auxStream;
473 return auxStream.str();
477 bool* data =
static_cast<bool*
>(ca.
data);
478 stringstream auxStream;
480 return auxStream.str();
484 string* data =
static_cast<string*
>(ca.
data);
489 std::vector<int> * data =
static_cast< std::vector<int> *
>(ca.
data);
490 stringstream auxStream;
491 auxStream << (*data);
492 return auxStream.str();
494 case CAT_VECTOR_DOUBLE:
496 std::vector<double> * data =
static_cast< std::vector<double> *
>(ca.
data);
497 stringstream auxStream;
498 auxStream << (*data);
499 return auxStream.str();
509 string::size_type b = s.find_first_not_of(
" \t\n");
510 string::size_type e = s.find_last_not_of(
" \t\n");
511 if(b == string::npos)
513 return std::string(s, b, e - b + 1);
517 std::istream&
operator>>(std::istream& is, std::vector<double>& v){
522 const char* c = s.c_str();
523 char* caux =
const_cast<char*
>(c);
526 bool hasNextValue=
true;
528 double i=strtod(c,&caux);
534 hasNextValue =
false;
539 std::ostream& operator<<(std::ostream& os, const std::vector<double>& v)
543 for (
size_t i=1; i<v.size(); i++)
548 bool CommandArgs::parsedParam(
const std::string& param)
const 550 std::vector<CommandArgument>::const_iterator it = _args.begin();
551 for ( ; it != _args.end(); ++it) {
552 if (it->name == param) {
std::istream & operator>>(std::istream &is, std::vector< double > &v)
std::string trim(const std::string &s)
bool convertString(const std::string &s, T &x)
bool operator()(const std::pair< T1, T2 > &left, const std::pair< T1, T2 > &right)