lemon/arg_parser.cc
changeset 964 2b6bffe0e7e8
parent 842 c2ff0a365245
child 1179 d7e25df22e88
     1.1 --- a/lemon/arg_parser.cc	Tue Dec 20 17:44:38 2011 +0100
     1.2 +++ b/lemon/arg_parser.cc	Tue Dec 20 18:15:14 2011 +0100
     1.3 @@ -2,7 +2,7 @@
     1.4   *
     1.5   * This file is a part of LEMON, a generic C++ optimization library.
     1.6   *
     1.7 - * Copyright (C) 2003-2009
     1.8 + * Copyright (C) 2003-2010
     1.9   * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
    1.10   * (Egervary Research Group on Combinatorial Optimization, EGRES).
    1.11   *
    1.12 @@ -20,14 +20,23 @@
    1.13  
    1.14  namespace lemon {
    1.15  
    1.16 +  void ArgParser::_terminate(ArgParserException::Reason reason) const
    1.17 +  {
    1.18 +    if(_exit_on_problems)
    1.19 +      exit(1);
    1.20 +    else throw(ArgParserException(reason));
    1.21 +  }
    1.22 +
    1.23 +
    1.24    void ArgParser::_showHelp(void *p)
    1.25    {
    1.26      (static_cast<ArgParser*>(p))->showHelp();
    1.27 -    exit(1);
    1.28 +    (static_cast<ArgParser*>(p))->_terminate(ArgParserException::HELP);
    1.29    }
    1.30  
    1.31    ArgParser::ArgParser(int argc, const char * const *argv)
    1.32 -    :_argc(argc), _argv(argv), _command_name(argv[0]) {
    1.33 +    :_argc(argc), _argv(argv), _command_name(argv[0]),
    1.34 +    _exit_on_problems(true) {
    1.35      funcOption("-help","Print a short help message",_showHelp,this);
    1.36      synonym("help","-help");
    1.37      synonym("h","-help");
    1.38 @@ -342,7 +351,7 @@
    1.39      for(std::vector<OtherArg>::const_iterator i=_others_help.begin();
    1.40          i!=_others_help.end();++i) showHelp(i);
    1.41      for(Opts::const_iterator i=_opts.begin();i!=_opts.end();++i) showHelp(i);
    1.42 -    exit(1);
    1.43 +    _terminate(ArgParserException::HELP);
    1.44    }
    1.45  
    1.46  
    1.47 @@ -351,7 +360,7 @@
    1.48      std::cerr << "\nUnknown option: " << arg << "\n";
    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::UNKNOWN_OPT);
    1.53    }
    1.54  
    1.55    void ArgParser::requiresValue(std::string arg, OptType t) const
    1.56 @@ -414,7 +423,7 @@
    1.57      if(!ok) {
    1.58        std::cerr << "\nType '" << _command_name <<
    1.59          " --help' to obtain a short summary on the usage.\n\n";
    1.60 -      exit(1);
    1.61 +      _terminate(ArgParserException::INVALID_OPT);
    1.62      }
    1.63    }
    1.64