Changes in / [843:81f7e910060b:841:aa8c9008b3de] in lemon-1.2
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
demo/arg_parser_demo.cc
r842 r440 66 66 .other("..."); 67 67 68 // Throw an exception when problems occurs. The default behavior is to69 // exit(1) on these cases, but this makes Valgrind falsely warn70 // about memory leaks.71 ap.throwOnProblems();72 73 68 // Perform the parsing process 74 69 // (in case of any error it terminates the program) 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; } 70 ap.parse(); 80 71 81 72 // Check each option if it has been given and print its value -
lemon/arg_parser.cc
r842 r440 21 21 namespace lemon { 22 22 23 void ArgParser::_terminate(ArgParserException::Reason reason) const24 {25 if(_exit_on_problems)26 exit(1);27 else throw(ArgParserException(reason));28 }29 30 31 23 void ArgParser::_showHelp(void *p) 32 24 { 33 25 (static_cast<ArgParser*>(p))->showHelp(); 34 (static_cast<ArgParser*>(p))->_terminate(ArgParserException::HELP);26 exit(1); 35 27 } 36 28 37 29 ArgParser::ArgParser(int argc, const char * const *argv) 38 :_argc(argc), _argv(argv), _command_name(argv[0]), 39 _exit_on_problems(true) { 30 :_argc(argc), _argv(argv), _command_name(argv[0]) { 40 31 funcOption("-help","Print a short help message",_showHelp,this); 41 32 synonym("help","-help"); … … 352 343 i!=_others_help.end();++i) showHelp(i); 353 344 for(Opts::const_iterator i=_opts.begin();i!=_opts.end();++i) showHelp(i); 354 _terminate(ArgParserException::HELP);345 exit(1); 355 346 } 356 347 … … 361 352 std::cerr << "\nType '" << _command_name << 362 353 " --help' to obtain a short summary on the usage.\n\n"; 363 _terminate(ArgParserException::UNKNOWN_OPT);354 exit(1); 364 355 } 365 356 … … 424 415 std::cerr << "\nType '" << _command_name << 425 416 " --help' to obtain a short summary on the usage.\n\n"; 426 _terminate(ArgParserException::INVALID_OPT);417 exit(1); 427 418 } 428 419 } -
lemon/arg_parser.h
r842 r440 34 34 35 35 namespace lemon { 36 37 ///Exception used by ArgParser38 class ArgParserException : public Exception {39 public:40 enum Reason {41 HELP, /// <tt>--help</tt> option was given42 UNKNOWN_OPT, /// Unknown option was given43 INVALID_OPT /// Invalid combination of options44 };45 46 private:47 Reason _reason;48 49 public:50 ///Constructor51 ArgParserException(Reason r) throw() : _reason(r) {}52 ///Virtual destructor53 virtual ~ArgParserException() throw() {}54 ///A short description of the exception55 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 failure71 Reason reason() const {return _reason; }72 };73 74 36 75 37 ///Command line arguments parser … … 142 104 std::string _command_name; 143 105 144 106 145 107 private: 146 108 //Bind a function to an option. … … 154 116 const std::string &help, 155 117 void (*func)(void *),void *data); 156 157 bool _exit_on_problems;158 159 void _terminate(ArgParserException::Reason reason) const;160 118 161 119 public: … … 423 381 const std::vector<std::string> &files() const { return _file_args; } 424 382 425 ///Throw instead of exit in case of problems426 void throwOnProblems()427 {428 _exit_on_problems=false;429 }430 383 }; 431 384 }
Note: See TracChangeset
for help on using the changeset viewer.