COIN-OR::LEMON - Graph Library

Ticket #332: c2ff0a365245.patch

File c2ff0a365245.patch, 4.6 KB (added by Alpar Juttner, 15 years ago)
  • demo/arg_parser_demo.cc

    # HG changeset patch
    # User Alpar Juttner <alpar@cs.elte.hu>
    # Date 1266171835 -3600
    # Node ID c2ff0a365245e31db53ee51bce5280d6d3061688
    # Parent  cc9e0c15d7477a877efb6ee9f299cff19b755314
    ArgParser can throw exception instead of exit(1) (#332)
    
    diff --git a/demo/arg_parser_demo.cc b/demo/arg_parser_demo.cc
    a b  
    6565  ap.other("infile", "The input file.")
    6666    .other("...");
    6767
     68  // Throw an exception when problems occurs. The default behavior is to
     69  // exit(1) on these cases, but this makes Valgrind falsely warn
     70  // about memory leaks.
     71  ap.throwOnProblems();
     72 
    6873  // Perform the parsing process
    6974  // (in case of any error it terminates the program)
    70   ap.parse();
     75  // The try {} construct is necessary only if the ap.trowOnProblems()
     76  // setting is in use.
     77  try {
     78    ap.parse();
     79  } catch (ArgParserException &) { return 1; }
    7180
    7281  // Check each option if it has been given and print its value
    7382  std::cout << "Parameters of '" << ap.commandName() << "':\n";
  • lemon/arg_parser.cc

    diff --git a/lemon/arg_parser.cc b/lemon/arg_parser.cc
    a b  
    2020
    2121namespace lemon {
    2222
     23  void ArgParser::_terminate(ArgParserException::Reason reason) const
     24  {
     25    if(_exit_on_problems)
     26      exit(1);
     27    else throw(ArgParserException(reason));
     28  }
     29 
     30 
    2331  void ArgParser::_showHelp(void *p)
    2432  {
    2533    (static_cast<ArgParser*>(p))->showHelp();
    26     exit(1);
     34    (static_cast<ArgParser*>(p))->_terminate(ArgParserException::HELP);
    2735  }
    2836
    2937  ArgParser::ArgParser(int argc, const char * const *argv)
    30     :_argc(argc), _argv(argv), _command_name(argv[0]) {
     38    :_argc(argc), _argv(argv), _command_name(argv[0]),
     39    _exit_on_problems(true) {
    3140    funcOption("-help","Print a short help message",_showHelp,this);
    3241    synonym("help","-help");
    3342    synonym("h","-help");
     
    342351    for(std::vector<OtherArg>::const_iterator i=_others_help.begin();
    343352        i!=_others_help.end();++i) showHelp(i);
    344353    for(Opts::const_iterator i=_opts.begin();i!=_opts.end();++i) showHelp(i);
    345     exit(1);
     354    _terminate(ArgParserException::HELP);
    346355  }
    347356
    348357
     
    351360    std::cerr << "\nUnknown option: " << arg << "\n";
    352361    std::cerr << "\nType '" << _command_name <<
    353362      " --help' to obtain a short summary on the usage.\n\n";
    354     exit(1);
     363    _terminate(ArgParserException::UNKNOWN_OPT);
    355364  }
    356365
    357366  void ArgParser::requiresValue(std::string arg, OptType t) const
     
    414423    if(!ok) {
    415424      std::cerr << "\nType '" << _command_name <<
    416425        " --help' to obtain a short summary on the usage.\n\n";
    417       exit(1);
     426      _terminate(ArgParserException::INVALID_OPT);
    418427    }
    419428  }
    420429
  • lemon/arg_parser.h

    diff --git a/lemon/arg_parser.h b/lemon/arg_parser.h
    a b  
    3434
    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
    3977  ///\ingroup misc
     
    103141    std::vector<std::string> _file_args;
    104142    std::string _command_name;
    105143
    106 
     144   
    107145  private:
    108146    //Bind a function to an option.
    109147
     
    116154                    const std::string &help,
    117155                    void (*func)(void *),void *data);
    118156
     157    bool _exit_on_problems;
     158   
     159    void _terminate(ArgParserException::Reason reason) const;
     160
    119161  public:
    120162
    121163    ///Constructor
     
    380422    ///not starting with a '-' character.
    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}
    385432