VR-Forces Developer's Guide
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
DtVrvCommandLineParser.h
Go to the documentation of this file.
1 /******************************************************************************
2  *
3  * file: DtVrvCommandLineParser.h
4  *
5  * Copyright (c) 2003, Michael E. Smoot .
6  * Copyright (c) 2004, Michael E. Smoot, Daniel Aarno.
7  * All rights reverved.
8  *
9  * See the file COPYING in the top directory of this distribution for
10  * more information.
11  *
12  * THE SOFTWARE IS PROVIDED _AS IS_, WITHOUT WARRANTY OF ANY KIND, EXPRESS
13  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
14  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
15  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
16  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
17  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
18  * DEALINGS IN THE SOFTWARE.
19  *
20  *****************************************************************************/
21 
22 #pragma once
23 
27 
28 #include <cmdLine/cmdLineInterface.h>
29 
31 #include <cmdLine/cmdUnlabeledValueArg.h>
32 #include <cmdLine/cmdUnlabeledMultiArg.h>
33 
34 #include <cmdLine/cmdXorHandler.h>
35 #include <cmdLine/cmdHelpVisitor.h>
36 #include <cmdLine/cmdVersionVisitor.h>
37 #include <cmdLine/cmdIgnoreRestVisitor.h>
38 
39 #include <cmdLine/cmdLineOutput.h>
40 #include <cmdLine/cmdStdOutput.h>
41 
42 #include <string>
43 #include <vector>
44 #include <list>
45 #include <iostream>
46 #include <iomanip>
47 #include <algorithm>
48 
49 #include <vlutil/vlProcessControl.h>
50 #include <vlutil/vlKeyboard.h>
51 #include <vlutil/vlPrint.h>
52 
53 namespace makVrv {
59  class CmdLine : public DtCmdLine::CmdLineInterface
63  {
64  protected:
65 
70  std::list<DtCmdLine::Arg*> _argList;
71 
75  std::string _progName;
76 
80  std::string _message;
81 
85  std::string _version;
86 
93 
98  char _delimiter;
99 
103  DtCmdLine::XorHandler _xorHandler;
104 
110  std::list<DtCmdLine::Arg*> _argDeleteOnExitList;
111 
117  std::list<DtCmdLine::Visitor*> _visitorDeleteOnExitList;
118 
122  DtCmdLine::CmdLineOutput* _output;
123 
130  bool _emptyCombined(const std::string& s);
131 
133 
134  private:
135 
140  void _constructor();
141 
145  void deleteOnExit(DtCmdLine::Arg* ptr);
146 
150  void deleteOnExit(DtCmdLine::Visitor* ptr);
151 
157 
158  public:
159 
171  CmdLine(const std::string& name,
172  const std::string& message,
173  const std::string& version = "none",
174 #ifdef _WIN32
175  bool pressAnyKey = true);
176 #else
177  bool pressAnyKey = false);
178 #endif
179 
191  CmdLine(const std::string& message,
192  const char delimiter = ' ',
193  const std::string& version = "none",
194 #ifdef _WIN32
195  bool pressAnyKey = true);
196 #else
197  bool pressAnyKey = false);
198 #endif
199 
203  virtual ~CmdLine();
204 
209  void add( DtCmdLine::Arg& a );
210 
215  void add( DtCmdLine::Arg* a );
216 
223  void xorAdd( DtCmdLine::Arg& a, DtCmdLine::Arg& b );
224 
230  void xorAdd( std::vector<DtCmdLine::Arg*>& xors );
231 
238  void parse(int argc, char** argv, bool printOnError = true);
239 
243  DtCmdLine::CmdLineOutput* getOutput();
244 
248  void setOutput(DtCmdLine::CmdLineOutput* co);
249 
253  std::string& getVersion();
254 
258  std::string& getProgramName();
259 
263  std::list<DtCmdLine::Arg*>& getArgList();
264 
268  DtCmdLine::XorHandler& getXorHandler();
269 
273  char getDelimiter();
274 
278  std::string& getMessage();
279  };
280 
281 
283  //Begin CmdLine.cpp
285 
286  inline CmdLine::CmdLine(const std::string& n,
287  const std::string& m,
288  const std::string& v,
289  bool pressAnyKey)
290  : _progName(n),
291  _message(m),
292  _version(v),
293  _numRequired(0),
294  _delimiter(' '),
295  myPressAnyKey(pressAnyKey),
296  _userSetOutput(false)
297  {
298  _constructor();
299  }
300 
301  inline CmdLine::CmdLine(const std::string& m,
302  char delim,
303  const std::string& v,
304  bool pressAnyKey)
305  : _progName("not_set_yet"),
306  _message(m),
307  _version(v),
308  _numRequired(0),
309  _delimiter(delim),
310  myPressAnyKey(pressAnyKey),
311  _userSetOutput(false)
312  {
313  _constructor();
314  }
315 
317  {
318  DtCmdLine::ArgListIterator argIter;
319  DtCmdLine::VisitorListIterator visIter;
320 
321  for( argIter = _argDeleteOnExitList.begin();
322  argIter != _argDeleteOnExitList.end();
323  ++argIter)
324  delete *argIter;
325 
326  for( visIter = _visitorDeleteOnExitList.begin();
327  visIter != _visitorDeleteOnExitList.end();
328  ++visIter)
329  delete *visIter;
330 
331  if ( !_userSetOutput )
332  delete _output;
333  }
334 
335  inline void CmdLine::_constructor()
336  {
337  _output = new DtCmdLine::StdOutput;
338 
339  DtCmdLine::Visitor *v;
340 
341  DtCmdLine::Arg::setDelimiter( _delimiter );
342 
343  v = new DtCmdLine::HelpVisitor( this, &_output, myPressAnyKey );
344  makVrv::SwitchArg* help = new makVrv::SwitchArg("h","help",
345  "Displays usage information and exits.",
346  false, v);
347  add( help );
348  deleteOnExit(help);
349  deleteOnExit(v);
350 
351  v = new DtCmdLine::VersionVisitor( this, &_output, myPressAnyKey );
352  makVrv::SwitchArg* vers = new makVrv::SwitchArg("v","version",
353  "Displays version information and exits.",
354  false, v);
355  add( vers );
356  deleteOnExit(vers);
357  deleteOnExit(v);
358 
359  v = new DtCmdLine::IgnoreRestVisitor();
360  makVrv::SwitchArg* ignore = new makVrv::SwitchArg(DtCmdLine::Arg::flagStartString(),
361  DtCmdLine::Arg::ignoreNameString(),
362  "Ignores the rest of the labeled arguments following this flag.",
363  false, v);
364  add( ignore );
365  deleteOnExit(ignore);
366  deleteOnExit(v);
367  }
368 
369  inline void CmdLine::xorAdd( std::vector<DtCmdLine::Arg*>& ors )
370  {
371  _xorHandler.add( ors );
372 
373  for (DtCmdLine::ArgVectorIterator it = ors.begin(); it != ors.end(); ++it)
374  {
375  (*it)->forceRequired();
376  (*it)->setRequireLabel( "OR required" );
377 
378  add( *it );
379  }
380  }
381 
382  inline void CmdLine::xorAdd( DtCmdLine::Arg& a, DtCmdLine::Arg& b )
383  {
384  std::vector<DtCmdLine::Arg*> ors;
385  ors.push_back( &a );
386  ors.push_back( &b );
387  xorAdd( ors );
388  }
389 
390  inline void CmdLine::add( DtCmdLine::Arg& a )
391  {
392  add( &a );
393  }
394 
395  inline void CmdLine::add( DtCmdLine::Arg* a )
396  {
397  for( DtCmdLine::ArgListIterator it = _argList.begin(); it != _argList.end(); ++it )
398  if ( *a == *(*it) )
399  throw( DtCmdLine::SpecificationException( "Argument with same flag/name already exists!",
400  a->longID() ) );
401 
402  a->addToList( _argList );
403 
404  if ( a->isRequired() )
405  _numRequired++;
406  }
407 
408  inline void CmdLine::parse(int argc, char** argv, bool /*printOnError*/)
409  {
410  try {
411 
412  _progName = argv[0];
413 
415  std::vector<std::string> args;
416  int i;
417  for (i = 1; i < argc; i++)
418  args.push_back(argv[i]);
419 
420  int requiredCount = 0;
421 
422  for (i = 0; (unsigned int)i < args.size(); i++)
423  {
424  bool matched = false;
425  for (DtCmdLine::ArgListIterator it = _argList.begin(); it != _argList.end(); ++it)
426  {
427  //This line changes the behavior of unlabeled multi args so that
428  // they are only included if they are before the ignore rest parameter
429  if(!(*it)->isIgnoreable() || !DtCmdLine::Arg::ignoreRest())
430  {
431  if ( (*it)->processArg( &i, args ) )
432  {
433  requiredCount += _xorHandler.check( *it );
434  matched = true;
435  break;
436  }
437  }
438 
439  }
440 
443  if ( !matched && _emptyCombined( args[i] ) )
444  matched = true;
445 
446  if ( !matched && !DtCmdLine::Arg::ignoreRest() )
447  throw(DtCmdLine::CmdLineParseException("Couldn't find match for argument",
448  args[i]));
449  }
450 
451  if ( requiredCount < _numRequired )
452  throw(DtCmdLine::CmdLineParseException("One or more required arguments missing!"));
453 
454  if ( requiredCount > _numRequired )
455  throw(DtCmdLine::CmdLineParseException("Too many arguments!"));
456 
457  } catch ( DtCmdLine::ArgException& e ) {
458  _output->failure(*this,e);
459 
460  int oldNotifyLevel = DtNotifyLevel;
461  DtNotifyLevel = 3;
462 
463  if (myPressAnyKey)
464  {
465  DtInfo << "Press any key to continue" << std::endl;
466 
467  while (DtPollBlockingInputChar() == '\0')
468  {
469  DtSleep(0.1);
470  }
471  }
472 
473  DtNotifyLevel = oldNotifyLevel;
474 
475  exit(1);
476  }
477  }
478 
479  inline bool CmdLine::_emptyCombined(const std::string& s)
480  {
481  if ( s[0] != DtCmdLine::Arg::flagStartChar() )
482  return false;
483 
484  for ( int i = 1; (unsigned int)i < s.length(); i++ )
485  if ( s[i] != DtCmdLine::Arg::blankChar() )
486  return false;
487 
488  return true;
489  }
490 
491  inline void CmdLine::deleteOnExit(DtCmdLine::Arg* ptr)
492  {
493  _argDeleteOnExitList.push_back(ptr);
494  }
495 
496  inline void CmdLine::deleteOnExit(DtCmdLine::Visitor* ptr)
497  {
498  _visitorDeleteOnExitList.push_back(ptr);
499  }
500 
501  inline DtCmdLine::CmdLineOutput* CmdLine::getOutput()
502  {
503  return _output;
504  }
505 
506  inline void CmdLine::setOutput(DtCmdLine::CmdLineOutput* co)
507  {
508  _userSetOutput = true;
509  _output = co;
510  }
511 
512  inline std::string& CmdLine::getVersion()
513  {
514  return _version;
515  }
516 
517  inline std::string& CmdLine::getProgramName()
518  {
519  return _progName;
520  }
521 
522  inline std::list<DtCmdLine::Arg*>& CmdLine::getArgList()
523  {
524  return _argList;
525  }
526 
527  inline DtCmdLine::XorHandler& CmdLine::getXorHandler()
528  {
529  return _xorHandler;
530  }
531 
532  inline char CmdLine::getDelimiter()
533  {
534  return _delimiter;
535  }
536 
537  inline std::string& CmdLine::getMessage()
538  {
539  return _message;
540  }
541 
543  //End CmdLine.cpp
545 }
std::string & getMessage()
Definition: DtVrvCommandLineParser.h:537
std::string & getVersion()
Definition: DtVrvCommandLineParser.h:512
std::string & getProgramName()
Definition: DtVrvCommandLineParser.h:517
std::list< DtCmdLine::Arg * > & getArgList()
Definition: DtVrvCommandLineParser.h:522
void _constructor()
Encapsulates the code common to the constructors (which is all of it).
Definition: DtVrvCommandLineParser.h:335
CmdLine(const std::string &name, const std::string &message, const std::string &version="none", bool pressAnyKey=false)
Command line constructor.
Definition: DtVrvCommandLineParser.h:286
DtCmdLine::XorHandler & getXorHandler()
Definition: DtVrvCommandLineParser.h:527
std::string _version
The version to be displayed with the –version switch.
Definition: DtVrvCommandLineParser.h:85
typedef int(ZCALLBACK *close_file_func) OF((voidpf opaque
DtCmdLine::XorHandler _xorHandler
The handler that manages xoring lists of args.
Definition: DtVrvCommandLineParser.h:103
DtCmdLine::CmdLineOutput * _output
Object that handles all output for the CmdLine.
Definition: DtVrvCommandLineParser.h:122
virtual ~CmdLine()
Deletes any resources allocated by a CmdLine object.
Definition: DtVrvCommandLineParser.h:316
std::string _progName
The name of the program.
Definition: DtVrvCommandLineParser.h:75
bool _userSetOutput
Is set to true when a user sets the output object.
Definition: DtVrvCommandLineParser.h:156
void deleteOnExit(DtCmdLine::Arg *ptr)
Perform a delete ptr; operation on ptr when this object is deleted.
Definition: DtVrvCommandLineParser.h:491
char _delimiter
The character that is used to separate the argument flag/name from the value.
Definition: DtVrvCommandLineParser.h:98
void setOutput(DtCmdLine::CmdLineOutput *co)
Definition: DtVrvCommandLineParser.h:506
This file declares the class SwitchArg.
void parse(int argc, char **argv, bool printOnError=true)
Parses the command line.
Definition: DtVrvCommandLineParser.h:408
bool myPressAnyKey
Definition: DtVrvCommandLineParser.h:132
std::list< DtCmdLine::Arg * > _argList
The list of arguments that will be tested against the command line.
Definition: DtVrvCommandLineParser.h:70
A simple switch argument.
Definition: DtVrvCmdSwitchArg.h:42
std::string _message
A message used to describe the program.
Definition: DtVrvCommandLineParser.h:80
char getDelimiter()
Definition: DtVrvCommandLineParser.h:532
int _numRequired
The number of arguments that are required to be present on the command line.
Definition: DtVrvCommandLineParser.h:92
std::list< DtCmdLine::Arg * > _argDeleteOnExitList
A list of Args to be explicitly deleted when the destructor is called.
Definition: DtVrvCommandLineParser.h:110
void add(DtCmdLine::Arg &a)
Adds an argument to the list of arguments to be parsed.
Definition: DtVrvCommandLineParser.h:390
std::list< DtCmdLine::Visitor * > _visitorDeleteOnExitList
A list of Visitors to be explicitly deleted when the destructor is called.
Definition: DtVrvCommandLineParser.h:117
bool _emptyCombined(const std::string &s)
Checks whether a name/flag string matches entirely matches the Arg::blankChar.
Definition: DtVrvCommandLineParser.h:479
DtCmdLine::CmdLineOutput * getOutput()
Definition: DtVrvCommandLineParser.h:501
void xorAdd(DtCmdLine::Arg &a, DtCmdLine::Arg &b)
Add two Args that will be xor&#39;d.
Definition: DtVrvCommandLineParser.h:382

Document ID: Generated on Thu Oct 15 21:27:02 EDT 2026 from SVN revision 296421
Copyright © 2005-2026 MAK Technologies. All Rights Reserved (www.mak.com)