cmd_line.hpp
Go to the documentation of this file.
1 
11 /*****************************************************************************
12 ** Ifdefs
13 *****************************************************************************/
14 
15 #ifndef TCLAP_CMDLINE_H
16 #define TCLAP_CMDLINE_H
17 
18 #include "switch_arg.hpp"
19 #include "multi_switch_arg.hpp"
20 #include "unlabeled_value_arg.hpp"
21 #include "unlabeled_multi_arg.hpp"
22 
23 #include "xor_handler.hpp"
24 #include "help_visitor.hpp"
25 #include "version_visitor.hpp"
26 #include "ignore_rest_visitor.hpp"
27 
28 #include "cmd_line_output.hpp"
29 #include "std_output.hpp"
30 
31 #include "constraint.hpp"
32 #include "values_constraint.hpp"
33 
34 #include <string>
35 #include <vector>
36 #include <list>
37 #include <iostream>
38 #include <iomanip>
39 #include <algorithm>
40 
41 namespace ecl {
42 
49 class CmdLine : public CmdLineInterface
50 {
51  protected:
52 
57  std::list<Arg*> _argList;
58 
62  std::string _progName;
63 
67  std::string _message;
68 
72  std::string _version;
73 
79  int _numRequired;
80 
85  char _delimiter;
86 
90  XorHandler _xorHandler;
91 
97  std::list<Arg*> _argDeleteOnExitList;
98 
104  std::list<Visitor*> _visitorDeleteOnExitList;
105 
109  CmdLineOutput* _output;
110 
117  bool _emptyCombined(const std::string& s);
118 
122  void deleteOnExit(Arg* ptr);
123 
127  void deleteOnExit(Visitor* ptr);
128 
129  private:
130 
135  void _constructor();
136 
141  bool _userSetOutput;
142 
146  bool _helpAndVersion;
147 
148  public:
149 
162  CmdLine(const std::string& message,
163  const char delimiter = ' ',
164  const std::string& version = "none",
165  bool helpAndVersion = true);
166 
170  virtual ~CmdLine();
171 
176  void add( Arg& a );
177 
182  void add( Arg* a );
183 
190  void xorAdd( Arg& a, Arg& b );
191 
197  void xorAdd( std::vector<Arg*>& xors );
198 
204  void parse(int argc, char** argv);
205 
209  CmdLineOutput* getOutput();
210 
214  void setOutput(CmdLineOutput* co);
215 
219  std::string& getVersion();
220 
224  std::string& getProgramName();
225 
229  std::list<Arg*>& getArgList();
230 
234  XorHandler& getXorHandler();
235 
239  char getDelimiter();
240 
244  std::string& getMessage();
245 
249  bool hasHelpAndVersion();
250 };
251 
252 
254 //Begin CmdLine.cpp
256 
257 inline CmdLine::CmdLine(const std::string& m,
258  char delim,
259  const std::string& v,
260  bool help )
261 : _progName("not_set_yet"),
262  _message(m),
263  _version(v),
264  _numRequired(0),
265  _delimiter(delim),
266  _userSetOutput(false),
267  _helpAndVersion(help)
268 {
269  _constructor();
270 }
271 
272 inline CmdLine::~CmdLine()
273 {
274  ArgListIterator argIter;
275  VisitorListIterator visIter;
276 
277  for( argIter = _argDeleteOnExitList.begin();
278  argIter != _argDeleteOnExitList.end();
279  ++argIter)
280  delete *argIter;
281 
282  for( visIter = _visitorDeleteOnExitList.begin();
283  visIter != _visitorDeleteOnExitList.end();
284  ++visIter)
285  delete *visIter;
286 
287  if ( !_userSetOutput )
288  delete _output;
289 }
290 
291 inline void CmdLine::_constructor()
292 {
293  _output = new StdOutput;
294 
296 
297  Visitor* v;
298 
299  if ( _helpAndVersion )
300  {
301  v = new HelpVisitor( this, &_output );
302  SwitchArg* help = new SwitchArg("h","help",
303  "Displays usage information and exits.",
304  false, v);
305  add( help );
306  deleteOnExit(help);
307  deleteOnExit(v);
308 
309  v = new VersionVisitor( this, &_output );
310  SwitchArg* vers = new SwitchArg("","version",
311  "Displays version information and exits.",
312  false, v);
313  add( vers );
314  deleteOnExit(vers);
315  deleteOnExit(v);
316  }
317 
318  v = new IgnoreRestVisitor();
319  SwitchArg* ignore = new SwitchArg(Arg::flagStartString(),
321  "Ignores the rest of the labeled arguments following this flag.",
322  false, v);
323  add( ignore );
324  deleteOnExit(ignore);
325  deleteOnExit(v);
326 }
327 
328 inline void CmdLine::xorAdd( std::vector<Arg*>& ors )
329 {
330  _xorHandler.add( ors );
331 
332  for (ArgVectorIterator it = ors.begin(); it != ors.end(); it++)
333  {
334  (*it)->forceRequired();
335  (*it)->setRequireLabel( "OR required" );
336 
337  add( *it );
338  }
339 }
340 
341 inline void CmdLine::xorAdd( Arg& a, Arg& b )
342 {
343  std::vector<Arg*> ors;
344  ors.push_back( &a );
345  ors.push_back( &b );
346  xorAdd( ors );
347 }
348 
349 inline void CmdLine::add( Arg& a )
350 {
351  add( &a );
352 }
353 
354 inline void CmdLine::add( Arg* a )
355 {
356  for( ArgListIterator it = _argList.begin(); it != _argList.end(); it++ )
357  if ( *a == *(*it) )
358  throw( SpecificationException(
359  "Argument with same flag/name already exists!",
360  a->longID() ) );
361 
362  a->addToList( _argList );
363 
364  if ( a->isRequired() )
365  _numRequired++;
366 }
367 
368 inline void CmdLine::parse(int argc, char** argv)
369 {
370  try {
371 
372  _progName = argv[0];
373 
374  // this step is necessary so that we have easy access to mutable strings.
375  std::vector<std::string> args;
376  for (int i = 1; i < argc; i++)
377  args.push_back(argv[i]);
378 
379  int requiredCount = 0;
380 
381  for (int i = 0; static_cast<unsigned int>(i) < args.size(); i++)
382  {
383  bool matched = false;
384  for (ArgListIterator it = _argList.begin(); it != _argList.end(); it++)
385  {
386  if ( (*it)->processArg( &i, args ) )
387  {
388  requiredCount += _xorHandler.check( *it );
389  matched = true;
390  break;
391  }
392  }
393 
394  // checks to see if the argument is an empty combined switch ...
395  // and if so, then we've actually matched it
396  if ( !matched && _emptyCombined( args[i] ) )
397  matched = true;
398 
399  if ( !matched && !Arg::ignoreRest() )
400  throw(CmdLineParseException("Couldn't find match for argument",
401  args[i]));
402  }
403 
404  if ( requiredCount < _numRequired )
405  throw(CmdLineParseException("One or more required arguments missing!"));
406 
407  if ( requiredCount > _numRequired )
408  throw(CmdLineParseException("Too many arguments!"));
409 
410  } catch ( ArgException e ) { _output->failure(*this,e); exit(1); }
411 }
412 
413 inline bool CmdLine::_emptyCombined(const std::string& s)
414 {
415  if ( s[0] != Arg::flagStartChar() )
416  return false;
417 
418  for ( int i = 1; static_cast<unsigned int>(i) < s.length(); i++ )
419  if ( s[i] != Arg::blankChar() )
420  return false;
421 
422  return true;
423 }
424 
425 inline void CmdLine::deleteOnExit(Arg* ptr)
426 {
427  _argDeleteOnExitList.push_back(ptr);
428 }
429 
430 inline void CmdLine::deleteOnExit(Visitor* ptr)
431 {
432  _visitorDeleteOnExitList.push_back(ptr);
433 }
434 
435 inline CmdLineOutput* CmdLine::getOutput()
436 {
437  return _output;
438 }
439 
440 inline void CmdLine::setOutput(CmdLineOutput* co)
441 {
442  _userSetOutput = true;
443  _output = co;
444 }
445 
446 inline std::string& CmdLine::getVersion()
447 {
448  return _version;
449 }
450 
451 inline std::string& CmdLine::getProgramName()
452 {
453  return _progName;
454 }
455 
456 inline std::list<Arg*>& CmdLine::getArgList()
457 {
458  return _argList;
459 }
460 
461 inline XorHandler& CmdLine::getXorHandler()
462 {
463  return _xorHandler;
464 }
465 
466 inline char CmdLine::getDelimiter()
467 {
468  return _delimiter;
469 }
470 
471 inline std::string& CmdLine::getMessage()
472 {
473  return _message;
474 }
475 
476 inline bool CmdLine::hasHelpAndVersion()
477 {
478  return _helpAndVersion;
479 }
480 
482 //End CmdLine.cpp
484 
485 }; // namespace ecl
486 
487 #endif
int _numRequired
Definition: cmd_line.hpp:79
static char flagStartChar()
static char blankChar()
int check(const Arg *a)
Definition: xor_handler.hpp:94
std::list< Visitor * >::iterator VisitorListIterator
bool _userSetOutput
Definition: cmd_line.hpp:141
static bool ignoreRest()
std::list< Arg * > _argList
Definition: cmd_line.hpp:57
std::string _progName
Definition: cmd_line.hpp:62
CmdLineOutput * _output
Definition: cmd_line.hpp:109
std::list< Arg * >::iterator ArgListIterator
static std::string ignoreNameString()
CmdLineOutput * getOutput()
Definition: cmd_line.hpp:435
std::string & getVersion()
Definition: cmd_line.hpp:446
std::string _version
Definition: cmd_line.hpp:72
static std::string flagStartString()
void add(Arg &a)
Definition: cmd_line.hpp:349
XorHandler _xorHandler
Definition: cmd_line.hpp:90
bool _helpAndVersion
Definition: cmd_line.hpp:146
std::vector< Arg * >::iterator ArgVectorIterator
char _delimiter
Definition: cmd_line.hpp:85
void _constructor()
Definition: cmd_line.hpp:291
CmdLine(const std::string &message, const char delimiter= ' ', const std::string &version="none", bool helpAndVersion=true)
Definition: cmd_line.hpp:257
virtual void failure(CmdLineInterface &c, ArgException &e)=0
std::string _message
Definition: cmd_line.hpp:67
static void setDelimiter(char c)
std::string & getProgramName()
Definition: cmd_line.hpp:451
std::list< Visitor * > _visitorDeleteOnExitList
Definition: cmd_line.hpp:104
XorHandler & getXorHandler()
Definition: cmd_line.hpp:461
std::list< Arg * > _argDeleteOnExitList
Definition: cmd_line.hpp:97
bool _emptyCombined(const std::string &s)
Definition: cmd_line.hpp:413
void deleteOnExit(Arg *ptr)
Definition: cmd_line.hpp:425
std::list< Arg * > & getArgList()
Definition: cmd_line.hpp:456
bool hasHelpAndVersion()
Definition: cmd_line.hpp:476
void parse(int argc, char **argv)
Definition: cmd_line.hpp:368
std::string & getMessage()
Definition: cmd_line.hpp:471
void xorAdd(Arg &a, Arg &b)
Definition: cmd_line.hpp:341
void setOutput(CmdLineOutput *co)
Definition: cmd_line.hpp:440
virtual ~CmdLine()
Definition: cmd_line.hpp:272
char getDelimiter()
Definition: cmd_line.hpp:466
void add(std::vector< Arg * > &ors)
Definition: xor_handler.hpp:89
std::string help()


xbot_node
Author(s): Roc, wangpeng@droid.ac.cn
autogenerated on Sat Oct 10 2020 03:28:13