g2o
Classes | Public Member Functions | Protected Member Functions | Protected Attributes | List of all members
g2o::CommandArgs Class Reference

Command line parsing of argc and argv. More...

#include <command_args.h>

Classes

struct  CommandArgument
 

Public Member Functions

 CommandArgs ()
 
virtual ~CommandArgs ()
 
bool parseArgs (int argc, char **argv, bool exitOnError=true)
 
void param (const std::string &name, bool &p, bool defValue, const std::string &desc)
 
void param (const std::string &name, int &p, int defValue, const std::string &desc)
 
void param (const std::string &name, float &p, float defValue, const std::string &desc)
 
void param (const std::string &name, double &p, double defValue, const std::string &desc)
 
void param (const std::string &name, std::string &p, const std::string &defValue, const std::string &desc)
 
void param (const std::string &name, std::vector< int > &p, const std::vector< int > &defValue, const std::string &desc)
 
void param (const std::string &name, std::vector< double > &p, const std::vector< double > &defValue, const std::string &desc)
 
void paramLeftOver (const std::string &name, std::string &p, const std::string &defValue, const std::string &desc, bool optional=false)
 
void printParams (std::ostream &os)
 
const std::string & getBanner () const
 return the banner string More...
 
void setBanner (const std::string &banner)
 
void printHelp (std::ostream &os)
 
bool parsedParam (const std::string &paramFlag) const
 

Protected Member Functions

const char * type2str (int t) const
 
void str2arg (const std::string &input, CommandArgument &ca) const
 
std::string arg2str (const CommandArgument &ca) const
 
std::string trim (const std::string &s) const
 

Protected Attributes

std::vector< CommandArgument_args
 
std::vector< CommandArgument_leftOvers
 
std::vector< CommandArgument_leftOversOptional
 
std::string _banner
 
std::string _progName
 

Detailed Description

Command line parsing of argc and argv.

Parse the command line to get the program options. Additionally, we can store the config in a file and reload a parameter set from this file.

Definition at line 46 of file command_args.h.

Constructor & Destructor Documentation

g2o::CommandArgs::CommandArgs ( )

Definition at line 103 of file command_args.cpp.

104 {
105 }
g2o::CommandArgs::~CommandArgs ( )
virtual

Definition at line 107 of file command_args.cpp.

108 {
109 }

Member Function Documentation

std::string g2o::CommandArgs::arg2str ( const CommandArgument ca) const
protected

Definition at line 451 of file command_args.cpp.

References g2o::CAT_BOOL, g2o::CAT_DOUBLE, g2o::CAT_FLOAT, g2o::CAT_INT, g2o::CAT_STRING, g2o::CAT_VECTOR_INT, g2o::CommandArgs::CommandArgument::data, and g2o::CommandArgs::CommandArgument::type.

452 {
453  switch (ca.type) {
454  case CAT_FLOAT:
455  {
456  float* data = static_cast<float*>(ca.data);
457  stringstream auxStream;
458  auxStream << *data;
459  return auxStream.str();
460  }
461  case CAT_DOUBLE:
462  {
463  double* data = static_cast<double*>(ca.data);
464  stringstream auxStream;
465  auxStream << *data;
466  return auxStream.str();
467  }
468  case CAT_INT:
469  {
470  int* data = static_cast<int*>(ca.data);
471  stringstream auxStream;
472  auxStream << *data;
473  return auxStream.str();
474  }
475  case CAT_BOOL:
476  {
477  bool* data = static_cast<bool*>(ca.data);
478  stringstream auxStream;
479  auxStream << *data;
480  return auxStream.str();
481  }
482  case CAT_STRING:
483  {
484  string* data = static_cast<string*>(ca.data);
485  return *data;
486  }
487  case CAT_VECTOR_INT:
488  {
489  std::vector<int> * data = static_cast< std::vector<int> * >(ca.data);
490  stringstream auxStream;
491  auxStream << (*data);
492  return auxStream.str();
493  }
494  case CAT_VECTOR_DOUBLE:
495  {
496  std::vector<double> * data = static_cast< std::vector<double> * >(ca.data);
497  stringstream auxStream;
498  auxStream << (*data);
499  return auxStream.str();
500  }
501  }
502  return "";
503 }
const std::string& g2o::CommandArgs::getBanner ( ) const
inline

return the banner string

Definition at line 96 of file command_args.h.

96 { return _banner; }
std::string _banner
Definition: command_args.h:113
void g2o::CommandArgs::param ( const std::string &  name,
bool &  p,
bool  defValue,
const std::string &  desc 
)

add a bool parameter, if found on the command line, will toggle defValue

Definition at line 198 of file command_args.cpp.

References g2o::CAT_BOOL, g2o::CommandArgs::CommandArgument::data, g2o::CommandArgs::CommandArgument::description, g2o::CommandArgs::CommandArgument::name, g2o::CommandArgs::CommandArgument::parsed, and g2o::CommandArgs::CommandArgument::type.

Referenced by main().

199 {
200  CommandArgument ca;
201  ca.name = name;
202  ca.description = desc;
203  ca.type = CAT_BOOL;
204  ca.data = static_cast<void*>(&p);
205  ca.parsed = false;
206  p = defValue;
207  _args.push_back(ca);
208 }
std::vector< CommandArgument > _args
Definition: command_args.h:110
void g2o::CommandArgs::param ( const std::string &  name,
int &  p,
int  defValue,
const std::string &  desc 
)

add a int parameter

Definition at line 210 of file command_args.cpp.

References g2o::CAT_INT, g2o::CommandArgs::CommandArgument::data, g2o::CommandArgs::CommandArgument::description, g2o::CommandArgs::CommandArgument::name, g2o::CommandArgs::CommandArgument::parsed, and g2o::CommandArgs::CommandArgument::type.

211 {
212  CommandArgument ca;
213  ca.name = name;
214  ca.description = desc;
215  ca.type = CAT_INT;
216  ca.data = static_cast<void*>(&p);
217  ca.parsed = false;
218  p = defValue;
219  _args.push_back(ca);
220 }
std::vector< CommandArgument > _args
Definition: command_args.h:110
void g2o::CommandArgs::param ( const std::string &  name,
float &  p,
float  defValue,
const std::string &  desc 
)

add a float parameter

Definition at line 222 of file command_args.cpp.

References g2o::CAT_FLOAT, g2o::CommandArgs::CommandArgument::data, g2o::CommandArgs::CommandArgument::description, g2o::CommandArgs::CommandArgument::name, g2o::CommandArgs::CommandArgument::parsed, and g2o::CommandArgs::CommandArgument::type.

223 {
224  CommandArgument ca;
225  ca.name = name;
226  ca.description = desc;
227  ca.type = CAT_FLOAT;
228  ca.data = static_cast<void*>(&p);
229  ca.parsed = false;
230  p = defValue;
231  _args.push_back(ca);
232 }
std::vector< CommandArgument > _args
Definition: command_args.h:110
void g2o::CommandArgs::param ( const std::string &  name,
double &  p,
double  defValue,
const std::string &  desc 
)

add a float parameter

Definition at line 234 of file command_args.cpp.

References g2o::CAT_DOUBLE, g2o::CommandArgs::CommandArgument::data, g2o::CommandArgs::CommandArgument::description, g2o::CommandArgs::CommandArgument::name, g2o::CommandArgs::CommandArgument::parsed, and g2o::CommandArgs::CommandArgument::type.

235 {
236  CommandArgument ca;
237  ca.name = name;
238  ca.description = desc;
239  ca.type = CAT_DOUBLE;
240  ca.data = static_cast<void*>(&p);
241  ca.parsed = false;
242  p = defValue;
243  _args.push_back(ca);
244 }
std::vector< CommandArgument > _args
Definition: command_args.h:110
void g2o::CommandArgs::param ( const std::string &  name,
std::string &  p,
const std::string &  defValue,
const std::string &  desc 
)

add a string parameter

Definition at line 246 of file command_args.cpp.

References g2o::CAT_STRING, g2o::CommandArgs::CommandArgument::data, g2o::CommandArgs::CommandArgument::description, g2o::CommandArgs::CommandArgument::name, g2o::CommandArgs::CommandArgument::parsed, and g2o::CommandArgs::CommandArgument::type.

247 {
248  CommandArgument ca;
249  ca.name = name;
250  ca.description = desc;
251  ca.type = CAT_STRING;
252  ca.data = static_cast<void*>(&p);
253  ca.parsed = false;
254  p = defValue;
255  _args.push_back(ca);
256 }
std::vector< CommandArgument > _args
Definition: command_args.h:110
void g2o::CommandArgs::param ( const std::string &  name,
std::vector< int > &  p,
const std::vector< int > &  defValue,
const std::string &  desc 
)

add an int vector parameter

Definition at line 258 of file command_args.cpp.

References g2o::CAT_VECTOR_INT, g2o::CommandArgs::CommandArgument::data, g2o::CommandArgs::CommandArgument::description, g2o::CommandArgs::CommandArgument::name, g2o::CommandArgs::CommandArgument::parsed, and g2o::CommandArgs::CommandArgument::type.

258  {
259  CommandArgument ca;
260  ca.name = name;
261  ca.description = desc;
262  ca.type = CAT_VECTOR_INT;
263  ca.data = static_cast<void*>(&p);
264  ca.parsed = false;
265  p = defValue;
266  _args.push_back(ca);
267 }
std::vector< CommandArgument > _args
Definition: command_args.h:110
void g2o::CommandArgs::param ( const std::string &  name,
std::vector< double > &  p,
const std::vector< double > &  defValue,
const std::string &  desc 
)

add an vector of doubles as a parameter

Definition at line 269 of file command_args.cpp.

References g2o::CAT_VECTOR_DOUBLE, g2o::CommandArgs::CommandArgument::data, g2o::CommandArgs::CommandArgument::description, g2o::CommandArgs::CommandArgument::name, g2o::CommandArgs::CommandArgument::parsed, and g2o::CommandArgs::CommandArgument::type.

270 {
271  CommandArgument ca;
272  ca.name = name;
273  ca.description = desc;
274  ca.type = CAT_VECTOR_DOUBLE;
275  ca.data = static_cast<void*>(&p);
276  ca.parsed = false;
277  p = defValue;
278  _args.push_back(ca);
279 }
std::vector< CommandArgument > _args
Definition: command_args.h:110
void g2o::CommandArgs::paramLeftOver ( const std::string &  name,
std::string &  p,
const std::string &  defValue,
const std::string &  desc,
bool  optional = false 
)

add a param wich is specified as a plain argument

Definition at line 342 of file command_args.cpp.

References g2o::CAT_STRING, g2o::CommandArgs::CommandArgument::data, g2o::CommandArgs::CommandArgument::description, g2o::CommandArgs::CommandArgument::name, g2o::CommandArgs::CommandArgument::optional, g2o::CommandArgs::CommandArgument::parsed, and g2o::CommandArgs::CommandArgument::type.

Referenced by main(), and g2o::RunG2OViewer::run().

343 {
344  CommandArgument ca;
345  ca.name = name;
346  ca.description = desc;
347  ca.type = CAT_STRING;
348  ca.data = static_cast<void*>(&p);
349  ca.parsed = false;
350  ca.optional = optional;
351  p = defValue;
352  if (optional)
353  _leftOversOptional.push_back(ca);
354  else
355  _leftOvers.push_back(ca);
356 }
std::vector< CommandArgument > _leftOvers
Definition: command_args.h:111
std::vector< CommandArgument > _leftOversOptional
Definition: command_args.h:112
bool g2o::CommandArgs::parseArgs ( int  argc,
char **  argv,
bool  exitOnError = true 
)

parse the command line for the requested parameters.

Parameters
argcthe number of params
argvthe value array
exitOnErrorcall exit() if the parsing fails
Returns
true, if parsing was correct

Definition at line 111 of file command_args.cpp.

References g2o::CAT_BOOL.

Referenced by main(), and g2o::RunG2OViewer::run().

112 {
113  _progName = argv[0];
114  int i;
115  for (i = 1; i < argc; i++) {
116  string name = argv[i];
117 
118  if (name[0] != '-') { // each param has to start with at least one dash
119  //cerr << "Error: expecting parameter, got " << name << endl;
120  //printHelp(cerr);
121  //if (exitOnError)
122  //exit(1);
123  //return false;
124  break;
125  }
126  /* first check whether it's -- and we should not continue parsing */
127  if (name == "--") {
128  ++i;
129  break;
130  }
131 
132  string::size_type dashPos = name.find_first_not_of('-');
133  if (dashPos != string::npos)
134  name = name.substr(dashPos);
135 
136  if (name == "help" || name == "h") {
137  printHelp(cout);
138  exit(0);
139  }
140  else {
141  // command line argument parsing
142  std::vector<CommandArgument>::iterator it = _args.begin();
143  for ( ; it != _args.end(); ++it) {
144  if (it->name == name) {
145  if (it->type == CAT_BOOL) {
146  if (!it->parsed) {
147  bool* data = static_cast<bool*>(it->data);
148  *data = !(*data);
149  }
150  it->parsed = true;
151  } else {
152  if(i >= argc-1) {
153  cerr << "Argument " << name << "needs value.\n";
154  printHelp(cerr);
155  if (exitOnError)
156  exit(1);
157  return false;
158  }
159  i++;
160  str2arg(argv[i], *it);
161  it->parsed = true;
162  }
163  break;
164  }
165  }
166  if (it == _args.end()) {
167  cerr << "Error: Unknown Option '" << name << "' (use -help to get list of options).\n";
168  if (exitOnError)
169  exit(1);
170  return false;
171  }
172 
173  }
174 
175  } // for argv[i]
176 
177  if ((int)_leftOvers.size() > argc - i) {
178  cerr << "Error: program requires parameters" << endl;
179  printHelp(cerr);
180  if (exitOnError)
181  exit(1);
182  return false;
183  }
184  for (size_t j = 0; (i < argc && j < _leftOvers.size()); i++, j++) {
185  string* s = static_cast<string*>(_leftOvers[j].data);
186  *s = argv[i];
187  }
188 
189  // the optional leftOvers
190  for (size_t j = 0; (i < argc && j < _leftOversOptional.size()); i++, j++) {
191  string* s = static_cast<string*>(_leftOversOptional[j].data);
192  *s = argv[i];
193  }
194 
195  return true;
196 }
std::vector< CommandArgument > _args
Definition: command_args.h:110
std::vector< CommandArgument > _leftOvers
Definition: command_args.h:111
std::string _progName
Definition: command_args.h:114
void str2arg(const std::string &input, CommandArgument &ca) const
void printHelp(std::ostream &os)
std::vector< CommandArgument > _leftOversOptional
Definition: command_args.h:112
bool g2o::CommandArgs::parsedParam ( const std::string &  paramFlag) const

returns true, if the param was parsed via the command line

Definition at line 548 of file command_args.cpp.

Referenced by main().

549 {
550  std::vector<CommandArgument>::const_iterator it = _args.begin();
551  for ( ; it != _args.end(); ++it) {
552  if (it->name == param) {
553  return it->parsed;
554  }
555  }
556  return false;
557 }
std::vector< CommandArgument > _args
Definition: command_args.h:110
void param(const std::string &name, bool &p, bool defValue, const std::string &desc)
void g2o::CommandArgs::printHelp ( std::ostream &  os)

print the help

Definition at line 281 of file command_args.cpp.

References g2o::CAT_BOOL.

282 {
283  if (_banner.size())
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) {
288  if (i > 0)
289  os << " ";
290  os << _leftOvers[i].name;
291  }
292  }
293  if (_leftOversOptional.size() > 0) {
294  if (_leftOvers.size() > 0)
295  os << " ";
296  for (size_t i = 0; i < _leftOversOptional.size(); ++i) {
297  if (i > 0)
298  os << " ";
299  os << "[" << _leftOversOptional[i].name << "]";
300  }
301  }
302  os << endl << endl;
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;
309  // build up option string to print as table
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) {
314  if (_args[i].type != CAT_BOOL) {
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 + ")"));
318  else
319  tableStrings.push_back(make_pair(_args[i].name + " " + type2str(_args[i].type), _args[i].description));
320  } else
321  tableStrings.push_back(make_pair(_args[i].name, _args[i].description));
322  maxArgLen = (std::max)(maxArgLen, tableStrings.back().first.size());
323  }
324  sort(tableStrings.begin(), tableStrings.end(), CmpPairFirst<string,string>());
325  maxArgLen += 3;
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)
329  os << " ";
330  os << tableStrings[i].second << endl;
331  }
332  // TODO should output description for leftOver params?
333  }
334 }
std::string _banner
Definition: command_args.h:113
std::string arg2str(const CommandArgument &ca) const
std::vector< CommandArgument > _args
Definition: command_args.h:110
std::vector< CommandArgument > _leftOvers
Definition: command_args.h:111
std::string _progName
Definition: command_args.h:114
const char * type2str(int t) const
std::vector< CommandArgument > _leftOversOptional
Definition: command_args.h:112
void g2o::CommandArgs::printParams ( std::ostream &  os)

print the value of all params to an ostream

void g2o::CommandArgs::setBanner ( const std::string &  banner)

Definition at line 337 of file command_args.cpp.

338 {
339  _banner = banner;
340 }
std::string _banner
Definition: command_args.h:113
void g2o::CommandArgs::str2arg ( const std::string &  input,
CommandArgument ca 
) const
protected

Definition at line 379 of file command_args.cpp.

References g2o::CAT_BOOL, g2o::CAT_DOUBLE, g2o::CAT_FLOAT, g2o::CAT_INT, g2o::CAT_STRING, g2o::CAT_VECTOR_INT, g2o::convertString(), g2o::CommandArgs::CommandArgument::data, and g2o::CommandArgs::CommandArgument::type.

380 {
381  switch (ca.type) {
382  case CAT_FLOAT:
383  {
384  float aux;
385  bool convertStatus = convertString(input, aux);
386  if (convertStatus) {
387  float* data = static_cast<float*>(ca.data);
388  *data = aux;
389  }
390  }
391  break;
392  case CAT_DOUBLE:
393  {
394  double aux;
395  bool convertStatus = convertString(input, aux);
396  if (convertStatus) {
397  double* data = static_cast<double*>(ca.data);
398  *data = aux;
399  }
400  }
401  break;
402  case CAT_INT:
403  {
404  int aux;
405  bool convertStatus = convertString(input, aux);
406  if (convertStatus) {
407  int* data = static_cast<int*>(ca.data);
408  *data = aux;
409  }
410  }
411  break;
412  case CAT_BOOL:
413  {
414  bool aux;
415  bool convertStatus = convertString(input, aux);
416  if (convertStatus) {
417  bool* data = static_cast<bool*>(ca.data);
418  *data = aux;
419  }
420  }
421  break;
422  case CAT_STRING:
423  {
424  string* data = static_cast<string*>(ca.data);
425  *data = input;
426  }
427  break;
428  case CAT_VECTOR_INT:
429  {
430  std::vector<int> aux;
431  bool convertStatus = convertString(input, aux);
432  if (convertStatus) {
433  std::vector<int>* data = static_cast< std::vector<int>* >(ca.data);
434  *data = aux;
435  }
436  }
437  break;
438  case CAT_VECTOR_DOUBLE:
439  {
440  std::vector<double> aux;
441  bool convertStatus = convertString(input, aux);
442  if (convertStatus) {
443  std::vector<double>* data = static_cast< std::vector<double>* >(ca.data);
444  *data = aux;
445  }
446  }
447  break;
448  }
449 }
bool convertString(const std::string &s, T &x)
std::string g2o::CommandArgs::trim ( const std::string &  s) const
protected

Definition at line 505 of file command_args.cpp.

506 {
507  if(s.length() == 0)
508  return s;
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)
512  return "";
513  return std::string(s, b, e - b + 1);
514 }
const char * g2o::CommandArgs::type2str ( int  t) const
protected

Definition at line 358 of file command_args.cpp.

References g2o::CAT_BOOL, g2o::CAT_DOUBLE, g2o::CAT_FLOAT, g2o::CAT_INT, g2o::CAT_STRING, and g2o::CAT_VECTOR_INT.

359 {
360  switch (t) {
361  case CAT_DOUBLE:
362  return "<double>";
363  case CAT_FLOAT:
364  return "<float>";
365  case CAT_INT:
366  return "<int>";
367  case CAT_STRING:
368  return "<string>";
369  case CAT_BOOL:
370  return "<bool>";
371  case CAT_VECTOR_INT:
372  return "<vector_int>";
373  case CAT_VECTOR_DOUBLE:
374  return "<vector_double>";
375  }
376  return "";
377 }

Member Data Documentation

std::vector<CommandArgument> g2o::CommandArgs::_args
protected

Definition at line 110 of file command_args.h.

std::string g2o::CommandArgs::_banner
protected

Definition at line 113 of file command_args.h.

std::vector<CommandArgument> g2o::CommandArgs::_leftOvers
protected

Definition at line 111 of file command_args.h.

std::vector<CommandArgument> g2o::CommandArgs::_leftOversOptional
protected

Definition at line 112 of file command_args.h.

std::string g2o::CommandArgs::_progName
protected

Definition at line 114 of file command_args.h.


The documentation for this class was generated from the following files: