lemon/arg_parser.h
changeset 1095 ad40f7d32846
parent 877 141f9c0db4a3
child 1123 18c89646185e
     1.1 --- a/lemon/arg_parser.h	Fri Aug 09 11:07:27 2013 +0200
     1.2 +++ b/lemon/arg_parser.h	Sun Aug 11 15:28:12 2013 +0200
     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 @@ -34,6 +34,51 @@
    1.13  
    1.14  namespace lemon {
    1.15  
    1.16 +  ///Exception used by ArgParser
    1.17 +
    1.18 +  ///Exception used by ArgParser.
    1.19 +  ///
    1.20 +  class ArgParserException : public Exception {
    1.21 +  public:
    1.22 +    /// Reasons for failure
    1.23 +
    1.24 +    /// Reasons for failure.
    1.25 +    ///
    1.26 +    enum Reason {
    1.27 +      HELP,         ///< <tt>--help</tt> option was given.
    1.28 +      UNKNOWN_OPT,  ///< Unknown option was given.
    1.29 +      INVALID_OPT   ///< Invalid combination of options.
    1.30 +    };
    1.31 +
    1.32 +  private:
    1.33 +    Reason _reason;
    1.34 +
    1.35 +  public:
    1.36 +    ///Constructor
    1.37 +    ArgParserException(Reason r) throw() : _reason(r) {}
    1.38 +    ///Virtual destructor
    1.39 +    virtual ~ArgParserException() throw() {}
    1.40 +    ///A short description of the exception
    1.41 +    virtual const char* what() const throw() {
    1.42 +      switch(_reason)
    1.43 +        {
    1.44 +        case HELP:
    1.45 +          return "lemon::ArgParseException: ask for help";
    1.46 +          break;
    1.47 +        case UNKNOWN_OPT:
    1.48 +          return "lemon::ArgParseException: unknown option";
    1.49 +          break;
    1.50 +        case INVALID_OPT:
    1.51 +          return "lemon::ArgParseException: invalid combination of options";
    1.52 +          break;
    1.53 +        }
    1.54 +      return "";
    1.55 +    }
    1.56 +    ///Return the reason for the failure
    1.57 +    Reason reason() const {return _reason; }
    1.58 +  };
    1.59 +
    1.60 +
    1.61    ///Command line arguments parser
    1.62  
    1.63    ///\ingroup misc
    1.64 @@ -116,6 +161,10 @@
    1.65                      const std::string &help,
    1.66                      void (*func)(void *),void *data);
    1.67  
    1.68 +    bool _exit_on_problems;
    1.69 +
    1.70 +    void _terminate(ArgParserException::Reason reason) const;
    1.71 +
    1.72    public:
    1.73  
    1.74      ///Constructor
    1.75 @@ -380,6 +429,11 @@
    1.76      ///not starting with a '-' character.
    1.77      const std::vector<std::string> &files() const { return _file_args; }
    1.78  
    1.79 +    ///Throw instead of exit in case of problems
    1.80 +    void throwOnProblems()
    1.81 +    {
    1.82 +      _exit_on_problems=false;
    1.83 +    }
    1.84    };
    1.85  }
    1.86