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 
170  CmdLine(const std::string& name,
171  const std::string& message,
172  const std::string& version = "none",
173 #ifdef _WIN32
174  bool pressAnyKey = true);
175 #else
176  bool pressAnyKey = false);
177 #endif
178 
189  CmdLine(const std::string& message,
190  const char delimiter = ' ',
191  const std::string& version = "none",
192 #ifdef _WIN32
193  bool pressAnyKey = true);
194 #else
195  bool pressAnyKey = false);
196 #endif
197 
201  virtual ~CmdLine();
202 
207  void add( DtCmdLine::Arg& a );
208 
213  void add( DtCmdLine::Arg* a );
214 
221  void xorAdd( DtCmdLine::Arg& a, DtCmdLine::Arg& b );
222 
228  void xorAdd( std::vector<DtCmdLine::Arg*>& xors );
229 
235  void parse(int argc, char** argv, bool printOnError = true);
236 
240  DtCmdLine::CmdLineOutput* getOutput();
241 
245  void setOutput(DtCmdLine::CmdLineOutput* co);
246 
250  std::string& getVersion();
251 
255  std::string& getProgramName();
256 
260  std::list<DtCmdLine::Arg*>& getArgList();
261 
265  DtCmdLine::XorHandler& getXorHandler();
266 
270  char getDelimiter();
271 
275  std::string& getMessage();
276  };
277 
278 
280  //Begin CmdLine.cpp
282 
283  inline CmdLine::CmdLine(const std::string& n,
284  const std::string& m,
285  const std::string& v,
286  bool pressAnyKey)
287  : _progName(n),
288  _message(m),
289  _version(v),
290  _numRequired(0),
291  _delimiter(' '),
292  myPressAnyKey(pressAnyKey),
293  _userSetOutput(false)
294  {
295  _constructor();
296  }
297 
298  inline CmdLine::CmdLine(const std::string& m,
299  char delim,
300  const std::string& v,
301  bool pressAnyKey)
302  : _progName("not_set_yet"),
303  _message(m),
304  _version(v),
305  _numRequired(0),
306  _delimiter(delim),
307  myPressAnyKey(pressAnyKey),
308  _userSetOutput(false)
309  {
310  _constructor();
311  }
312 
314  {
315  DtCmdLine::ArgListIterator argIter;
316  DtCmdLine::VisitorListIterator visIter;
317 
318  for( argIter = _argDeleteOnExitList.begin();
319  argIter != _argDeleteOnExitList.end();
320  ++argIter)
321  delete *argIter;
322 
323  for( visIter = _visitorDeleteOnExitList.begin();
324  visIter != _visitorDeleteOnExitList.end();
325  ++visIter)
326  delete *visIter;
327 
328  if ( !_userSetOutput )
329  delete _output;
330  }
331 
332  inline void CmdLine::_constructor()
333  {
334  _output = new DtCmdLine::StdOutput;
335 
336  DtCmdLine::Visitor *v;
337 
338  DtCmdLine::Arg::setDelimiter( _delimiter );
339 
340  v = new DtCmdLine::HelpVisitor( this, &_output, myPressAnyKey );
341  makVrv::SwitchArg* help = new makVrv::SwitchArg("h","help",
342  "Displays usage information and exits.",
343  false, v);
344  add( help );
345  deleteOnExit(help);
346  deleteOnExit(v);
347 
348  v = new DtCmdLine::VersionVisitor( this, &_output, myPressAnyKey );
349  makVrv::SwitchArg* vers = new makVrv::SwitchArg("v","version",
350  "Displays version information and exits.",
351  false, v);
352  add( vers );
353  deleteOnExit(vers);
354  deleteOnExit(v);
355 
356  v = new DtCmdLine::IgnoreRestVisitor();
357  makVrv::SwitchArg* ignore = new makVrv::SwitchArg(DtCmdLine::Arg::flagStartString(),
358  DtCmdLine::Arg::ignoreNameString(),
359  "Ignores the rest of the labeled arguments following this flag.",
360  false, v);
361  add( ignore );
362  deleteOnExit(ignore);
363  deleteOnExit(v);
364  }
365 
366  inline void CmdLine::xorAdd( std::vector<DtCmdLine::Arg*>& ors )
367  {
368  _xorHandler.add( ors );
369 
370  for (DtCmdLine::ArgVectorIterator it = ors.begin(); it != ors.end(); ++it)
371  {
372  (*it)->forceRequired();
373  (*it)->setRequireLabel( "OR required" );
374 
375  add( *it );
376  }
377  }
378 
379  inline void CmdLine::xorAdd( DtCmdLine::Arg& a, DtCmdLine::Arg& b )
380  {
381  std::vector<DtCmdLine::Arg*> ors;
382  ors.push_back( &a );
383  ors.push_back( &b );
384  xorAdd( ors );
385  }
386 
387  inline void CmdLine::add( DtCmdLine::Arg& a )
388  {
389  add( &a );
390  }
391 
392  inline void CmdLine::add( DtCmdLine::Arg* a )
393  {
394  for( DtCmdLine::ArgListIterator it = _argList.begin(); it != _argList.end(); ++it )
395  if ( *a == *(*it) )
396  throw( DtCmdLine::SpecificationException( "Argument with same flag/name already exists!",
397  a->longID() ) );
398 
399  a->addToList( _argList );
400 
401  if ( a->isRequired() )
402  _numRequired++;
403  }
404 
405  inline void CmdLine::parse(int argc, char** argv, bool /*printOnError*/)
406  {
407  try {
408 
409  _progName = argv[0];
410 
412  std::vector<std::string> args;
413  int i;
414  for (i = 1; i < argc; i++)
415  args.push_back(argv[i]);
416 
417  int requiredCount = 0;
418 
419  for (i = 0; (unsigned int)i < args.size(); i++)
420  {
421  bool matched = false;
422  for (DtCmdLine::ArgListIterator it = _argList.begin(); it != _argList.end(); ++it)
423  {
424  //This line changes the behavior of unlabeled multi args so that
425  // they are only included if they are before the ignore rest parameter
426  if(!(*it)->isIgnoreable() || !DtCmdLine::Arg::ignoreRest())
427  {
428  if ( (*it)->processArg( &i, args ) )
429  {
430  requiredCount += _xorHandler.check( *it );
431  matched = true;
432  break;
433  }
434  }
435 
436  }
437 
440  if ( !matched && _emptyCombined( args[i] ) )
441  matched = true;
442 
443  if ( !matched && !DtCmdLine::Arg::ignoreRest() )
444  throw(DtCmdLine::CmdLineParseException("Couldn't find match for argument",
445  args[i]));
446  }
447 
448  if ( requiredCount < _numRequired )
449  throw(DtCmdLine::CmdLineParseException("One or more required arguments missing!"));
450 
451  if ( requiredCount > _numRequired )
452  throw(DtCmdLine::CmdLineParseException("Too many arguments!"));
453 
454  } catch ( DtCmdLine::ArgException& e ) {
455  _output->failure(*this,e);
456 
457  int oldNotifyLevel = DtNotifyLevel;
458  DtNotifyLevel = 3;
459 
460  if (myPressAnyKey)
461  {
462  DtInfo << "Press any key to continue" << std::endl;
463 
464  while (DtPollBlockingInputChar() == '\0')
465  {
466  DtSleep(0.1);
467  }
468  }
469 
470  DtNotifyLevel = oldNotifyLevel;
471 
472  exit(1);
473  }
474  }
475 
476  inline bool CmdLine::_emptyCombined(const std::string& s)
477  {
478  if ( s[0] != DtCmdLine::Arg::flagStartChar() )
479  return false;
480 
481  for ( int i = 1; (unsigned int)i < s.length(); i++ )
482  if ( s[i] != DtCmdLine::Arg::blankChar() )
483  return false;
484 
485  return true;
486  }
487 
488  inline void CmdLine::deleteOnExit(DtCmdLine::Arg* ptr)
489  {
490  _argDeleteOnExitList.push_back(ptr);
491  }
492 
493  inline void CmdLine::deleteOnExit(DtCmdLine::Visitor* ptr)
494  {
495  _visitorDeleteOnExitList.push_back(ptr);
496  }
497 
498  inline DtCmdLine::CmdLineOutput* CmdLine::getOutput()
499  {
500  return _output;
501  }
502 
503  inline void CmdLine::setOutput(DtCmdLine::CmdLineOutput* co)
504  {
505  _userSetOutput = true;
506  _output = co;
507  }
508 
509  inline std::string& CmdLine::getVersion()
510  {
511  return _version;
512  }
513 
514  inline std::string& CmdLine::getProgramName()
515  {
516  return _progName;
517  }
518 
519  inline std::list<DtCmdLine::Arg*>& CmdLine::getArgList()
520  {
521  return _argList;
522  }
523 
524  inline DtCmdLine::XorHandler& CmdLine::getXorHandler()
525  {
526  return _xorHandler;
527  }
528 
529  inline char CmdLine::getDelimiter()
530  {
531  return _delimiter;
532  }
533 
534  inline std::string& CmdLine::getMessage()
535  {
536  return _message;
537  }
538 
540  //End CmdLine.cpp
542 }
std::string & getMessage()
Definition: DtVrvCommandLineParser.h:534
std::string & getVersion()
Definition: DtVrvCommandLineParser.h:509
std::string & getProgramName()
Definition: DtVrvCommandLineParser.h:514
std::list< DtCmdLine::Arg * > & getArgList()
Definition: DtVrvCommandLineParser.h:519
void _constructor()
Encapsulates the code common to the constructors (which is all of it).
Definition: DtVrvCommandLineParser.h:332
CmdLine(const std::string &name, const std::string &message, const std::string &version="none", bool pressAnyKey=false)
Command line constructor.
Definition: DtVrvCommandLineParser.h:283
DtCmdLine::XorHandler & getXorHandler()
Definition: DtVrvCommandLineParser.h:524
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:313
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:488
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:503
This file declares the class SwitchArg.
void parse(int argc, char **argv, bool printOnError=true)
Parses the command line.
Definition: DtVrvCommandLineParser.h:405
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:529
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:387
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:476
DtCmdLine::CmdLineOutput * getOutput()
Definition: DtVrvCommandLineParser.h:498
void xorAdd(DtCmdLine::Arg &a, DtCmdLine::Arg &b)
Add two Args that will be xor&#39;d.
Definition: DtVrvCommandLineParser.h:379

Document ID: Generated on Tue Sep 24 19:28:17 EDT 2024 from SVN revision 269799
Copyright © 2005-2024 MAK Technologies. All Rights Reserved (www.mak.com)