COIN-OR::LEMON - Graph Library

Changeset 915:c2ff0a365245 in lemon for lemon/arg_parser.h


Ignore:
Timestamp:
02/14/10 19:23:55 (10 years ago)
Author:
Alpar Juttner <alpar@…>
Branch:
default
Phase:
public
Message:

ArgParser? can throw exception instead of exit(1) (#332)

File:
1 edited

Legend:

Unmodified
Added
Removed
  • lemon/arg_parser.h

    r463 r915  
    3535namespace lemon {
    3636
     37  ///Exception used by ArgParser
     38  class ArgParserException : public Exception {
     39  public:
     40    enum Reason {
     41      HELP,         /// <tt>--help</tt> option was given
     42      UNKNOWN_OPT,  /// Unknown option was given
     43      INVALID_OPT   /// Invalid combination of options
     44    };
     45   
     46  private:
     47    Reason _reason;
     48   
     49  public:
     50    ///Constructor
     51    ArgParserException(Reason r) throw() : _reason(r) {}
     52    ///Virtual destructor
     53    virtual ~ArgParserException() throw() {}
     54    ///A short description of the exception
     55    virtual const char* what() const throw() {
     56      switch(_reason)
     57        {
     58        case HELP:
     59          return "lemon::ArgParseException: ask for help";
     60          break;
     61        case UNKNOWN_OPT:
     62          return "lemon::ArgParseException: unknown option";
     63          break;
     64        case INVALID_OPT:
     65          return "lemon::ArgParseException: invalid combination of options";
     66          break;
     67        }
     68      return "";
     69    }
     70    ///Return the reason for the failure
     71    Reason reason() const {return _reason; }
     72  };
     73
     74
    3775  ///Command line arguments parser
    3876
     
    104142    std::string _command_name;
    105143
    106 
     144   
    107145  private:
    108146    //Bind a function to an option.
     
    116154                    const std::string &help,
    117155                    void (*func)(void *),void *data);
     156
     157    bool _exit_on_problems;
     158   
     159    void _terminate(ArgParserException::Reason reason) const;
    118160
    119161  public:
     
    381423    const std::vector<std::string> &files() const { return _file_args; }
    382424
     425    ///Throw instead of exit in case of problems
     426    void throwOnProblems()
     427    {
     428      _exit_on_problems=false;
     429    }
    383430  };
    384431}
Note: See TracChangeset for help on using the changeset viewer.