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 
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 
142 
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 
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 
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 
431 {
432  _visitorDeleteOnExitList.push_back(ptr);
433 }
434 
436 {
437  return _output;
438 }
439 
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 
462 {
463  return _xorHandler;
464 }
465 
467 {
468  return _delimiter;
469 }
470 
471 inline std::string& CmdLine::getMessage()
472 {
473  return _message;
474 }
475 
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()
Definition: arg.hpp:187
static char blankChar()
Definition: arg.hpp:182
int check(const Arg *a)
Definition: xor_handler.hpp:94
std::list< Visitor * >::iterator VisitorListIterator
Definition: arg.hpp:346
TCLAP command line argument parser classes.
bool _userSetOutput
Definition: cmd_line.hpp:141
static bool ignoreRest()
Definition: arg.hpp:169
virtual bool isRequired() const
Definition: arg.hpp:465
TCLAP command line argument parser classes.
std::list< Arg * > _argList
Definition: cmd_line.hpp:57
std::string _progName
Definition: cmd_line.hpp:62
TClap class indirectly used to define the interface for visitors.
Definition: visitor.hpp:25
Tclap class indirectly used by children for standardising outputs.
Managing interface for The base class that manages the command line definition and passes along the p...
CmdLineOutput * _output
Definition: cmd_line.hpp:109
Virtual parent for all the different argument classes.
Definition: arg.hpp:37
std::list< Arg * >::iterator ArgListIterator
Definition: arg.hpp:336
static std::string ignoreNameString()
Definition: arg.hpp:204
CmdLineOutput * getOutput()
Definition: cmd_line.hpp:435
TCLAP command line argument parser classes.
std::string & getVersion()
Definition: cmd_line.hpp:446
std::string _version
Definition: cmd_line.hpp:72
static std::string flagStartString()
Definition: arg.hpp:193
void add(Arg &a)
Definition: cmd_line.hpp:349
XorHandler _xorHandler
Definition: cmd_line.hpp:90
Defines the exception that is thrown whenever a command line is created and parsed.
bool _helpAndVersion
Definition: cmd_line.hpp:146
TCLAP command line argument parser classes.
std::vector< Arg * >::iterator ArgVectorIterator
Definition: arg.hpp:341
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
TCLAP command line argument parser classes.
TCLAP command line argument parser classes.
std::string _message
Definition: cmd_line.hpp:67
TClap class indirectly used by CmdLine for handling xor&#39;d arguments.
Definition: xor_handler.hpp:32
TClap class indirectly used to define the interface for visitors.
virtual std::string longID(const std::string &valueId="val") const
Definition: arg.hpp:417
static void setDelimiter(char c)
Definition: arg.hpp:210
std::string & getProgramName()
Definition: cmd_line.hpp:451
TClap class indirectly used to define the interface for visitors.
std::list< Visitor * > _visitorDeleteOnExitList
Definition: cmd_line.hpp:104
TCLAP command line argument parser classes.
XorHandler & getXorHandler()
Definition: cmd_line.hpp:461
Defines the exception when an argument is improperly specified.
TCLAP command line argument parser classes.
Manages the command line parsing object.
Definition: cmd_line.hpp:49
std::list< Arg * > _argDeleteOnExitList
Definition: cmd_line.hpp:97
TCLAP command line argument parser classes.
bool _emptyCombined(const std::string &s)
Definition: cmd_line.hpp:413
void deleteOnExit(Arg *ptr)
Definition: cmd_line.hpp:425
virtual void addToList(std::list< Arg * > &argList) const
Definition: arg.hpp:558
TCLAP command line argument parser classes.
std::list< Arg * > & getArgList()
Definition: cmd_line.hpp:456
TCLAP command line argument parser classes.
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
TClap class indirectly used to define the interface for visitors.
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
TCLAP command line argument parser classes.
Defines the exception that is thrown whenever a conflict in arguments occurs.
void add(std::vector< Arg * > &ors)
Definition: xor_handler.hpp:89
TClap class indirectly used to handle CmdLine arguments.
Definition: std_output.hpp:38


ecl_command_line
Author(s): Daniel Stonier
autogenerated on Mon Jun 10 2019 13:08:08