diff -r 70b199792735 -r ad40f7d32846 lemon/arg_parser.h --- a/lemon/arg_parser.h Fri Aug 09 11:07:27 2013 +0200 +++ b/lemon/arg_parser.h Sun Aug 11 15:28:12 2013 +0200 @@ -2,7 +2,7 @@ * * This file is a part of LEMON, a generic C++ optimization library. * - * Copyright (C) 2003-2009 + * Copyright (C) 2003-2010 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport * (Egervary Research Group on Combinatorial Optimization, EGRES). * @@ -34,6 +34,51 @@ namespace lemon { + ///Exception used by ArgParser + + ///Exception used by ArgParser. + /// + class ArgParserException : public Exception { + public: + /// Reasons for failure + + /// Reasons for failure. + /// + enum Reason { + HELP, ///< --help option was given. + UNKNOWN_OPT, ///< Unknown option was given. + INVALID_OPT ///< Invalid combination of options. + }; + + private: + Reason _reason; + + public: + ///Constructor + ArgParserException(Reason r) throw() : _reason(r) {} + ///Virtual destructor + virtual ~ArgParserException() throw() {} + ///A short description of the exception + virtual const char* what() const throw() { + switch(_reason) + { + case HELP: + return "lemon::ArgParseException: ask for help"; + break; + case UNKNOWN_OPT: + return "lemon::ArgParseException: unknown option"; + break; + case INVALID_OPT: + return "lemon::ArgParseException: invalid combination of options"; + break; + } + return ""; + } + ///Return the reason for the failure + Reason reason() const {return _reason; } + }; + + ///Command line arguments parser ///\ingroup misc @@ -116,6 +161,10 @@ const std::string &help, void (*func)(void *),void *data); + bool _exit_on_problems; + + void _terminate(ArgParserException::Reason reason) const; + public: ///Constructor @@ -380,6 +429,11 @@ ///not starting with a '-' character. const std::vector &files() const { return _file_args; } + ///Throw instead of exit in case of problems + void throwOnProblems() + { + _exit_on_problems=false; + } }; }