lemon/arg_parser.cc
changeset 2504 46a82ce84cc6
parent 2406 0ffc78641b34
child 2553 bfced05fa852
equal deleted inserted replaced
4:e73e8d8d2127 5:cfdcf1011897
    24   {
    24   {
    25     (static_cast<ArgParser*>(p))->showHelp();
    25     (static_cast<ArgParser*>(p))->showHelp();
    26     exit(1);
    26     exit(1);
    27   }
    27   }
    28 
    28 
    29   ArgParser::ArgParser(int argc, char **argv) :_argc(argc), _argv(argv),
    29   ArgParser::ArgParser(int argc, const char **argv) :_argc(argc), _argv(argv),
    30 					       _command_name(argv[0]) {
    30                                                      _command_name(argv[0]) {
    31     funcOption("-help","Print a short help message",_showHelp,this);
    31     funcOption("-help","Print a short help message",_showHelp,this);
    32     synonym("help","-help");
    32     synonym("help","-help");
    33     synonym("h","-help");
    33     synonym("h","-help");
    34 
    34 
    35   }
    35   }
   132     _opts[name]=p;
   132     _opts[name]=p;
   133     return *this;
   133     return *this;
   134   }
   134   }
   135 
   135 
   136   ArgParser &ArgParser::refOption(const std::string &name,
   136   ArgParser &ArgParser::refOption(const std::string &name,
   137 			       const std::string &help,
   137                                   const std::string &help,
   138 			       double &ref, bool obl)
   138                                   double &ref, bool obl)
   139   {
   139   {
   140     ParData p;
   140     ParData p;
   141     p.double_p=&ref;
   141     p.double_p=&ref;
   142     p.self_delete=false;
   142     p.self_delete=false;
   143     p.help=help;
   143     p.help=help;
   146     _opts[name]=p;
   146     _opts[name]=p;
   147     return *this;
   147     return *this;
   148   }
   148   }
   149 
   149 
   150   ArgParser &ArgParser::refOption(const std::string &name,
   150   ArgParser &ArgParser::refOption(const std::string &name,
   151 			       const std::string &help,
   151                                   const std::string &help,
   152 			       bool &ref, bool obl)
   152                                   bool &ref, bool obl)
   153   {
   153   {
   154     ParData p;
   154     ParData p;
   155     p.bool_p=&ref;
   155     p.bool_p=&ref;
   156     p.self_delete=false;
   156     p.self_delete=false;
   157     p.help=help;
   157     p.help=help;
   195 
   195 
   196   ArgParser &ArgParser::optionGroup(const std::string &group,
   196   ArgParser &ArgParser::optionGroup(const std::string &group,
   197 				    const std::string &opt)
   197 				    const std::string &opt)
   198   {
   198   {
   199     Opts::iterator i = _opts.find(opt);
   199     Opts::iterator i = _opts.find(opt);
   200     if(i==_opts.end()) throw LogicError();
   200     LEMON_ASSERT(i!=_opts.end(), "Unknown option: '"+opt+"'");
   201     else if(i->second.ingroup) throw LogicError();
   201     LEMON_ASSERT(!(i->second.ingroup), 
   202     else {
   202                  "Option already in option group: '"+opt+"'");
   203       GroupData &g=_groups[group];
   203     GroupData &g=_groups[group];
   204       g.opts.push_back(opt);
   204     g.opts.push_back(opt);
   205       i->second.ingroup=true;
   205     i->second.ingroup=true;
   206     }
       
   207     return *this;
   206     return *this;
   208   }
   207   }
   209 
   208 
   210   ArgParser &ArgParser::onlyOneGroup(const std::string &group)
   209   ArgParser &ArgParser::onlyOneGroup(const std::string &group)
   211   {
   210   {
   217   ArgParser &ArgParser::synonym(const std::string &syn,
   216   ArgParser &ArgParser::synonym(const std::string &syn,
   218 				const std::string &opt)
   217 				const std::string &opt)
   219   {
   218   {
   220     Opts::iterator o = _opts.find(opt);
   219     Opts::iterator o = _opts.find(opt);
   221     Opts::iterator s = _opts.find(syn);
   220     Opts::iterator s = _opts.find(syn);
   222     if(o==_opts.end()||s!=_opts.end())
   221     LEMON_ASSERT(o!=_opts.end(), "Unknown option: '"+opt+"'");
   223       throw LogicError();
   222     LEMON_ASSERT(s==_opts.end(), "Option already used: '"+syn+"'");
   224     else {
   223     ParData p;
   225       ParData p;
   224     p.help=opt;
   226       p.help=opt;
   225     p.mandatory=false;
   227       p.mandatory=false;
   226     p.syn=true;
   228       p.syn=true;
   227     _opts[syn]=p;
   229       _opts[syn]=p;
   228     o->second.has_syn=true;
   230       o->second.has_syn=true;
       
   231     }
       
   232     return *this;
   229     return *this;
   233   }
   230   }
   234 
   231 
   235   ArgParser &ArgParser::mandatoryGroup(const std::string &group)
   232   ArgParser &ArgParser::mandatoryGroup(const std::string &group)
   236   {
   233   {