lemon/arg_parser.cc
changeset 842 c2ff0a365245
parent 440 88ed40ad0d4f
child 877 141f9c0db4a3
     1.1 --- a/lemon/arg_parser.cc	Fri Feb 12 22:24:26 2010 +0100
     1.2 +++ b/lemon/arg_parser.cc	Sun Feb 14 19:23:55 2010 +0100
     1.3 @@ -20,14 +20,23 @@
     1.4  
     1.5  namespace lemon {
     1.6  
     1.7 +  void ArgParser::_terminate(ArgParserException::Reason reason) const
     1.8 +  {
     1.9 +    if(_exit_on_problems)
    1.10 +      exit(1);
    1.11 +    else throw(ArgParserException(reason));
    1.12 +  }
    1.13 +  
    1.14 +  
    1.15    void ArgParser::_showHelp(void *p)
    1.16    {
    1.17      (static_cast<ArgParser*>(p))->showHelp();
    1.18 -    exit(1);
    1.19 +    (static_cast<ArgParser*>(p))->_terminate(ArgParserException::HELP);
    1.20    }
    1.21  
    1.22    ArgParser::ArgParser(int argc, const char * const *argv)
    1.23 -    :_argc(argc), _argv(argv), _command_name(argv[0]) {
    1.24 +    :_argc(argc), _argv(argv), _command_name(argv[0]),
    1.25 +    _exit_on_problems(true) {
    1.26      funcOption("-help","Print a short help message",_showHelp,this);
    1.27      synonym("help","-help");
    1.28      synonym("h","-help");
    1.29 @@ -342,7 +351,7 @@
    1.30      for(std::vector<OtherArg>::const_iterator i=_others_help.begin();
    1.31          i!=_others_help.end();++i) showHelp(i);
    1.32      for(Opts::const_iterator i=_opts.begin();i!=_opts.end();++i) showHelp(i);
    1.33 -    exit(1);
    1.34 +    _terminate(ArgParserException::HELP);
    1.35    }
    1.36  
    1.37  
    1.38 @@ -351,7 +360,7 @@
    1.39      std::cerr << "\nUnknown option: " << arg << "\n";
    1.40      std::cerr << "\nType '" << _command_name <<
    1.41        " --help' to obtain a short summary on the usage.\n\n";
    1.42 -    exit(1);
    1.43 +    _terminate(ArgParserException::UNKNOWN_OPT);
    1.44    }
    1.45  
    1.46    void ArgParser::requiresValue(std::string arg, OptType t) const
    1.47 @@ -414,7 +423,7 @@
    1.48      if(!ok) {
    1.49        std::cerr << "\nType '" << _command_name <<
    1.50          " --help' to obtain a short summary on the usage.\n\n";
    1.51 -      exit(1);
    1.52 +      _terminate(ArgParserException::INVALID_OPT);
    1.53      }
    1.54    }
    1.55