VR-Forces 4.3.1 Class Documentation
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator 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 
60 
61 
62  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  if ( (*it)->processArg( &i, args ) )
425  {
426  requiredCount += _xorHandler.check( *it );
427  matched = true;
428  break;
429  }
430  }
431 
434  if ( !matched && _emptyCombined( args[i] ) )
435  matched = true;
436 
437  if ( !matched && !DtCmdLine::Arg::ignoreRest() )
438  throw(DtCmdLine::CmdLineParseException("Couldn't find match for argument",
439  args[i]));
440  }
441 
442  if ( requiredCount < _numRequired )
443  throw(DtCmdLine::CmdLineParseException("One or more required arguments missing!"));
444 
445  if ( requiredCount > _numRequired )
446  throw(DtCmdLine::CmdLineParseException("Too many arguments!"));
447 
448  } catch ( DtCmdLine::ArgException e ) {
449  _output->failure(*this,e);
450 
451  int oldNotifyLevel = DtNotifyLevel;
452  DtNotifyLevel = 3;
453 
454  if (myPressAnyKey)
455  {
456  DtInfo << "Press any key to continue" << std::endl;
457 
458  while (DtPollBlockingInputChar() == '\0')
459  {
460  DtSleep(0.1);
461  }
462  }
463 
464  DtNotifyLevel = oldNotifyLevel;
465 
466  exit(1);
467  }
468  }
469 
470  inline bool CmdLine::_emptyCombined(const std::string& s)
471  {
472  if ( s[0] != DtCmdLine::Arg::flagStartChar() )
473  return false;
474 
475  for ( int i = 1; (unsigned int)i < s.length(); i++ )
476  if ( s[i] != DtCmdLine::Arg::blankChar() )
477  return false;
478 
479  return true;
480  }
481 
482  inline void CmdLine::deleteOnExit(DtCmdLine::Arg* ptr)
483  {
484  _argDeleteOnExitList.push_back(ptr);
485  }
486 
487  inline void CmdLine::deleteOnExit(DtCmdLine::Visitor* ptr)
488  {
489  _visitorDeleteOnExitList.push_back(ptr);
490  }
491 
492  inline DtCmdLine::CmdLineOutput* CmdLine::getOutput()
493  {
494  return _output;
495  }
496 
497  inline void CmdLine::setOutput(DtCmdLine::CmdLineOutput* co)
498  {
499  _userSetOutput = true;
500  _output = co;
501  }
502 
503  inline std::string& CmdLine::getVersion()
504  {
505  return _version;
506  }
507 
508  inline std::string& CmdLine::getProgramName()
509  {
510  return _progName;
511  }
512 
513  inline std::list<DtCmdLine::Arg*>& CmdLine::getArgList()
514  {
515  return _argList;
516  }
517 
518  inline DtCmdLine::XorHandler& CmdLine::getXorHandler()
519  {
520  return _xorHandler;
521  }
522 
523  inline char CmdLine::getDelimiter()
524  {
525  return _delimiter;
526  }
527 
528  inline std::string& CmdLine::getMessage()
529  {
530  return _message;
531  }
532 
534  //End CmdLine.cpp
536 }

Document ID: Generated on Wed Jul 29 00:55:00 EDT 2015 from SVN revision 155257
Copyright © 2005-2015 VT MÄK. All Rights Reserved (www.mak.com)