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 
91 
97  std::list<Arg*> _argDeleteOnExitList;
98 
104  std::list<Visitor*> _visitorDeleteOnExitList;
105 
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 
210 
214  void setOutput(CmdLineOutput* co);
215 
219  std::string& getVersion();
220 
224  std::string& getProgramName();
225 
229  std::list<Arg*>& getArgList();
230 
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 {
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 {
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 
436 {
437  return _output;
438 }
439 
440 inline void CmdLine::setOutput(CmdLineOutput* co)
441 {
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 
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 {
479 }
480 
482 //End CmdLine.cpp
484 
485 }; // namespace ecl
486 
487 #endif
cmd_line_output.hpp
TCLAP command line argument parser classes.
ecl::Arg::setDelimiter
static void setDelimiter(char c)
Definition: arg.hpp:214
std_output.hpp
TCLAP command line argument parser classes.
ecl::CmdLine::getVersion
std::string & getVersion()
Definition: cmd_line.hpp:448
ecl::CmdLine::deleteOnExit
void deleteOnExit(Arg *ptr)
Definition: cmd_line.hpp:427
ecl::XorHandler::check
int check(const Arg *a)
Definition: xor_handler.hpp:96
ecl::CmdLine::_output
CmdLineOutput * _output
Definition: cmd_line.hpp:113
ecl::HelpVisitor
TClap class indirectly used to define the interface for visitors.
Definition: help_visitor.hpp:32
ecl::ArgListIterator
std::list< Arg * >::iterator ArgListIterator
Definition: arg.hpp:338
ecl::CmdLine::hasHelpAndVersion
bool hasHelpAndVersion()
Definition: cmd_line.hpp:478
ecl::VisitorListIterator
std::list< Visitor * >::iterator VisitorListIterator
Definition: arg.hpp:348
ecl::CmdLine::_version
std::string _version
Definition: cmd_line.hpp:76
unlabeled_multi_arg.hpp
TCLAP command line argument parser classes.
ecl::XorHandler
TClap class indirectly used by CmdLine for handling xor'd arguments.
Definition: xor_handler.hpp:34
ecl::CmdLine::getOutput
CmdLineOutput * getOutput()
Definition: cmd_line.hpp:437
multi_switch_arg.hpp
TCLAP command line argument parser classes.
xor_handler.hpp
TCLAP command line argument parser classes.
help_visitor.hpp
TCLAP command line argument parser classes.
ecl::ArgVectorIterator
std::vector< Arg * >::iterator ArgVectorIterator
Definition: arg.hpp:343
values_constraint.hpp
TCLAP command line argument parser classes.
ecl::CmdLine::_constructor
void _constructor()
Definition: cmd_line.hpp:293
ecl::CmdLineOutput::failure
virtual void failure(CmdLineInterface &c, ArgException &e)=0
ecl::CmdLine::_xorHandler
XorHandler _xorHandler
Definition: cmd_line.hpp:94
ecl::CmdLine::_argList
std::list< Arg * > _argList
Definition: cmd_line.hpp:61
ecl::CmdLine::_progName
std::string _progName
Definition: cmd_line.hpp:66
ecl::Arg
Virtual parent for all the different argument classes.
Definition: arg.hpp:39
ignore_rest_visitor.hpp
TCLAP command line argument parser classes.
ecl::ArgException
Defines the exception that is thrown whenever a command line is created and parsed.
Definition: arg_exception.hpp:32
ecl::SpecificationException
Defines the exception that is thrown whenever a conflict in arguments occurs.
Definition: arg_exception.hpp:169
ecl::StdOutput
TClap class indirectly used to handle CmdLine arguments.
Definition: std_output.hpp:40
ecl::IgnoreRestVisitor
TClap class indirectly used to define the interface for visitors.
Definition: ignore_rest_visitor.hpp:32
ecl::CmdLine::add
void add(Arg &a)
Definition: cmd_line.hpp:351
ecl::CmdLine::_emptyCombined
bool _emptyCombined(const std::string &s)
Definition: cmd_line.hpp:415
ecl::CmdLine::_helpAndVersion
bool _helpAndVersion
Definition: cmd_line.hpp:150
ecl::CmdLine::_visitorDeleteOnExitList
std::list< Visitor * > _visitorDeleteOnExitList
Definition: cmd_line.hpp:108
ecl::CmdLine::getXorHandler
XorHandler & getXorHandler()
Definition: cmd_line.hpp:463
ecl::SwitchArg
Definition: switch_arg.hpp:33
ecl::CmdLine::_delimiter
char _delimiter
Definition: cmd_line.hpp:89
ecl::CmdLine::_argDeleteOnExitList
std::list< Arg * > _argDeleteOnExitList
Definition: cmd_line.hpp:101
unlabeled_value_arg.hpp
TCLAP command line argument parser classes.
ecl::CmdLine::setOutput
void setOutput(CmdLineOutput *co)
Definition: cmd_line.hpp:442
ecl::CmdLine::getProgramName
std::string & getProgramName()
Definition: cmd_line.hpp:453
ecl::CmdLine::_message
std::string _message
Definition: cmd_line.hpp:71
ecl::Arg::addToList
virtual void addToList(std::list< Arg * > &argList) const
Definition: arg.hpp:560
ecl::Arg::flagStartChar
static char flagStartChar()
Definition: arg.hpp:191
ecl::Arg::blankChar
static char blankChar()
Definition: arg.hpp:186
ecl::VersionVisitor
TClap class indirectly used to define the interface for visitors.
Definition: version_visitor.hpp:32
ecl::CmdLine::getMessage
std::string & getMessage()
Definition: cmd_line.hpp:473
ecl::Visitor
TClap class indirectly used to define the interface for visitors.
Definition: visitor.hpp:27
ecl::CmdLine::~CmdLine
virtual ~CmdLine()
Definition: cmd_line.hpp:274
ecl::Arg::ignoreRest
static bool ignoreRest()
Definition: arg.hpp:173
version_visitor.hpp
TCLAP command line argument parser classes.
constraint.hpp
TCLAP command line argument parser classes.
ecl::CmdLine::CmdLine
CmdLine(const std::string &message, const char delimiter=' ', const std::string &version="none", bool helpAndVersion=true)
Definition: cmd_line.hpp:259
ecl::CmdLineOutput
Tclap class indirectly used by children for standardising outputs.
Definition: cmd_line_output.hpp:37
ecl::CmdLine::xorAdd
void xorAdd(Arg &a, Arg &b)
Definition: cmd_line.hpp:343
ecl::CmdLine::_userSetOutput
bool _userSetOutput
Definition: cmd_line.hpp:145
ecl::CmdLine::_numRequired
int _numRequired
Definition: cmd_line.hpp:83
ecl::Arg::flagStartString
static std::string flagStartString()
Definition: arg.hpp:197
ecl::Arg::longID
virtual std::string longID(const std::string &valueId="val") const
Definition: arg.hpp:419
ecl::Arg::ignoreNameString
static std::string ignoreNameString()
Definition: arg.hpp:208
ecl::CmdLineParseException
Defines the exception when an argument is improperly specified.
Definition: arg_exception.hpp:143
ecl::CmdLine::parse
void parse(int argc, char **argv)
Definition: cmd_line.hpp:370
ecl::CmdLine::getDelimiter
char getDelimiter()
Definition: cmd_line.hpp:468
ecl::CmdLine::getArgList
std::list< Arg * > & getArgList()
Definition: cmd_line.hpp:458
ecl::Arg::isRequired
virtual bool isRequired() const
Definition: arg.hpp:467
switch_arg.hpp
TCLAP command line argument parser classes.
ecl::XorHandler::add
void add(std::vector< Arg * > &ors)
Definition: xor_handler.hpp:91
ecl


ecl_command_line
Author(s): Daniel Stonier
autogenerated on Wed Mar 2 2022 00:16:13