lemon/arg_parser.h
changeset 964 2b6bffe0e7e8
parent 842 c2ff0a365245
child 879 38213abd2911
     1.1 --- a/lemon/arg_parser.h	Tue Dec 20 17:44:38 2011 +0100
     1.2 +++ b/lemon/arg_parser.h	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 @@ -34,6 +34,44 @@
    1.13  
    1.14  namespace lemon {
    1.15  
    1.16 +  ///Exception used by ArgParser
    1.17 +  class ArgParserException : public Exception {
    1.18 +  public:
    1.19 +    enum Reason {
    1.20 +      HELP,         /// <tt>--help</tt> option was given
    1.21 +      UNKNOWN_OPT,  /// Unknown option was given
    1.22 +      INVALID_OPT   /// Invalid combination of options
    1.23 +    };
    1.24 +
    1.25 +  private:
    1.26 +    Reason _reason;
    1.27 +
    1.28 +  public:
    1.29 +    ///Constructor
    1.30 +    ArgParserException(Reason r) throw() : _reason(r) {}
    1.31 +    ///Virtual destructor
    1.32 +    virtual ~ArgParserException() throw() {}
    1.33 +    ///A short description of the exception
    1.34 +    virtual const char* what() const throw() {
    1.35 +      switch(_reason)
    1.36 +        {
    1.37 +        case HELP:
    1.38 +          return "lemon::ArgParseException: ask for help";
    1.39 +          break;
    1.40 +        case UNKNOWN_OPT:
    1.41 +          return "lemon::ArgParseException: unknown option";
    1.42 +          break;
    1.43 +        case INVALID_OPT:
    1.44 +          return "lemon::ArgParseException: invalid combination of options";
    1.45 +          break;
    1.46 +        }
    1.47 +      return "";
    1.48 +    }
    1.49 +    ///Return the reason for the failure
    1.50 +    Reason reason() const {return _reason; }
    1.51 +  };
    1.52 +
    1.53 +
    1.54    ///Command line arguments parser
    1.55  
    1.56    ///\ingroup misc
    1.57 @@ -116,6 +154,10 @@
    1.58                      const std::string &help,
    1.59                      void (*func)(void *),void *data);
    1.60  
    1.61 +    bool _exit_on_problems;
    1.62 +
    1.63 +    void _terminate(ArgParserException::Reason reason) const;
    1.64 +
    1.65    public:
    1.66  
    1.67      ///Constructor
    1.68 @@ -380,6 +422,11 @@
    1.69      ///not starting with a '-' character.
    1.70      const std::vector<std::string> &files() const { return _file_args; }
    1.71  
    1.72 +    ///Throw instead of exit in case of problems
    1.73 +    void throwOnProblems()
    1.74 +    {
    1.75 +      _exit_on_problems=false;
    1.76 +    }
    1.77    };
    1.78  }
    1.79