lemon/arg_parser.cc
changeset 850 e77b621e6e7e
parent 440 88ed40ad0d4f
child 877 141f9c0db4a3
equal deleted inserted replaced
8:9d416f1a519c 9:5f0b3d7c53ba
    18 
    18 
    19 #include <lemon/arg_parser.h>
    19 #include <lemon/arg_parser.h>
    20 
    20 
    21 namespace lemon {
    21 namespace lemon {
    22 
    22 
       
    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   
    23   void ArgParser::_showHelp(void *p)
    31   void ArgParser::_showHelp(void *p)
    24   {
    32   {
    25     (static_cast<ArgParser*>(p))->showHelp();
    33     (static_cast<ArgParser*>(p))->showHelp();
    26     exit(1);
    34     (static_cast<ArgParser*>(p))->_terminate(ArgParserException::HELP);
    27   }
    35   }
    28 
    36 
    29   ArgParser::ArgParser(int argc, const char * const *argv)
    37   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) {
    31     funcOption("-help","Print a short help message",_showHelp,this);
    40     funcOption("-help","Print a short help message",_showHelp,this);
    32     synonym("help","-help");
    41     synonym("help","-help");
    33     synonym("h","-help");
    42     synonym("h","-help");
    34   }
    43   }
    35 
    44 
   340     shortHelp();
   349     shortHelp();
   341     std::cerr << "Where:\n";
   350     std::cerr << "Where:\n";
   342     for(std::vector<OtherArg>::const_iterator i=_others_help.begin();
   351     for(std::vector<OtherArg>::const_iterator i=_others_help.begin();
   343         i!=_others_help.end();++i) showHelp(i);
   352         i!=_others_help.end();++i) showHelp(i);
   344     for(Opts::const_iterator i=_opts.begin();i!=_opts.end();++i) showHelp(i);
   353     for(Opts::const_iterator i=_opts.begin();i!=_opts.end();++i) showHelp(i);
   345     exit(1);
   354     _terminate(ArgParserException::HELP);
   346   }
   355   }
   347 
   356 
   348 
   357 
   349   void ArgParser::unknownOpt(std::string arg) const
   358   void ArgParser::unknownOpt(std::string arg) const
   350   {
   359   {
   351     std::cerr << "\nUnknown option: " << arg << "\n";
   360     std::cerr << "\nUnknown option: " << arg << "\n";
   352     std::cerr << "\nType '" << _command_name <<
   361     std::cerr << "\nType '" << _command_name <<
   353       " --help' to obtain a short summary on the usage.\n\n";
   362       " --help' to obtain a short summary on the usage.\n\n";
   354     exit(1);
   363     _terminate(ArgParserException::UNKNOWN_OPT);
   355   }
   364   }
   356 
   365 
   357   void ArgParser::requiresValue(std::string arg, OptType t) const
   366   void ArgParser::requiresValue(std::string arg, OptType t) const
   358   {
   367   {
   359     std::cerr << "Argument '" << arg << "' requires a";
   368     std::cerr << "Argument '" << arg << "' requires a";
   412           }
   421           }
   413         }
   422         }
   414     if(!ok) {
   423     if(!ok) {
   415       std::cerr << "\nType '" << _command_name <<
   424       std::cerr << "\nType '" << _command_name <<
   416         " --help' to obtain a short summary on the usage.\n\n";
   425         " --help' to obtain a short summary on the usage.\n\n";
   417       exit(1);
   426       _terminate(ArgParserException::INVALID_OPT);
   418     }
   427     }
   419   }
   428   }
   420 
   429 
   421   ArgParser &ArgParser::parse()
   430   ArgParser &ArgParser::parse()
   422   {
   431   {