lemon/arg_parser.cc
changeset 260 c691064dfd4f
parent 210 81cfc04531e8
child 315 c887e703b566
equal deleted inserted replaced
5:8da87a349b6a 6:0d5244449fb5
   237   {
   237   {
   238     _others_help.push_back(OtherArg(name,help));
   238     _others_help.push_back(OtherArg(name,help));
   239     return *this;
   239     return *this;
   240   }
   240   }
   241 
   241 
   242   void ArgParser::show(std::ostream &os,Opts::iterator i)
   242   void ArgParser::show(std::ostream &os,Opts::const_iterator i) const
   243   {
   243   {
   244     os << "-" << i->first;
   244     os << "-" << i->first;
   245     if(i->second.has_syn)
   245     if(i->second.has_syn)
   246       for(Opts::iterator j=_opts.begin();j!=_opts.end();++j)
   246       for(Opts::const_iterator j=_opts.begin();j!=_opts.end();++j)
   247         if(j->second.syn&&j->second.help==i->first)
   247         if(j->second.syn&&j->second.help==i->first)
   248           os << "|-" << j->first;
   248           os << "|-" << j->first;
   249     switch(i->second.type) {
   249     switch(i->second.type) {
   250     case STRING:
   250     case STRING:
   251       os << " str";
   251       os << " str";
   259     default:
   259     default:
   260       break;
   260       break;
   261     }
   261     }
   262   }
   262   }
   263 
   263 
   264   void ArgParser::show(std::ostream &os,Groups::iterator i)
   264   void ArgParser::show(std::ostream &os,Groups::const_iterator i) const
   265   {
   265   {
   266     GroupData::Opts::iterator o=i->second.opts.begin();
   266     GroupData::Opts::const_iterator o=i->second.opts.begin();
   267     while(o!=i->second.opts.end()) {
   267     while(o!=i->second.opts.end()) {
   268       show(os,_opts.find(*o));
   268       show(os,_opts.find(*o));
   269       ++o;
   269       ++o;
   270       if(o!=i->second.opts.end()) os<<'|';
   270       if(o!=i->second.opts.end()) os<<'|';
   271     }
   271     }
   272   }
   272   }
   273 
   273 
   274   void ArgParser::showHelp(Opts::iterator i)
   274   void ArgParser::showHelp(Opts::const_iterator i) const
   275   {
   275   {
   276     if(i->second.help.size()==0||i->second.syn) return;
   276     if(i->second.help.size()==0||i->second.syn) return;
   277     std::cerr << "  ";
   277     std::cerr << "  ";
   278     show(std::cerr,i);
   278     show(std::cerr,i);
   279     std::cerr << std::endl;
   279     std::cerr << std::endl;
   280     std::cerr << "     " << i->second.help << std::endl;
   280     std::cerr << "     " << i->second.help << std::endl;
   281   }
   281   }
   282   void ArgParser::showHelp(std::vector<ArgParser::OtherArg>::iterator i)
   282   void ArgParser::showHelp(std::vector<ArgParser::OtherArg>::const_iterator i)
       
   283     const
   283   {
   284   {
   284     if(i->help.size()==0) return;
   285     if(i->help.size()==0) return;
   285     std::cerr << "  " << i->name << std::endl
   286     std::cerr << "  " << i->name << std::endl
   286               << "     " << i->help << std::endl;
   287               << "     " << i->help << std::endl;
   287   }
   288   }
   288 
   289 
   289   void ArgParser::shortHelp()
   290   void ArgParser::shortHelp() const
   290   {
   291   {
   291     const unsigned int LINE_LEN=77;
   292     const unsigned int LINE_LEN=77;
   292     const std::string indent("    ");
   293     const std::string indent("    ");
   293     std::cerr << "Usage:\n  " << _command_name;
   294     std::cerr << "Usage:\n  " << _command_name;
   294     int pos=_command_name.size()+2;
   295     int pos=_command_name.size()+2;
   295     for(Groups::iterator g=_groups.begin();g!=_groups.end();++g) {
   296     for(Groups::const_iterator g=_groups.begin();g!=_groups.end();++g) {
   296       std::ostringstream cstr;
   297       std::ostringstream cstr;
   297       cstr << ' ';
   298       cstr << ' ';
   298       if(!g->second.mandatory) cstr << '[';
   299       if(!g->second.mandatory) cstr << '[';
   299       show(cstr,g);
   300       show(cstr,g);
   300       if(!g->second.mandatory) cstr << ']';
   301       if(!g->second.mandatory) cstr << ']';
   303         pos=indent.size();
   304         pos=indent.size();
   304       }
   305       }
   305       std::cerr << cstr.str();
   306       std::cerr << cstr.str();
   306       pos+=cstr.str().size();
   307       pos+=cstr.str().size();
   307     }
   308     }
   308     for(Opts::iterator i=_opts.begin();i!=_opts.end();++i)
   309     for(Opts::const_iterator i=_opts.begin();i!=_opts.end();++i)
   309       if(!i->second.ingroup&&!i->second.syn) {
   310       if(!i->second.ingroup&&!i->second.syn) {
   310         std::ostringstream cstr;
   311         std::ostringstream cstr;
   311         cstr << ' ';
   312         cstr << ' ';
   312         if(!i->second.mandatory) cstr << '[';
   313         if(!i->second.mandatory) cstr << '[';
   313         show(cstr,i);
   314         show(cstr,i);
   317           pos=indent.size();
   318           pos=indent.size();
   318         }
   319         }
   319         std::cerr << cstr.str();
   320         std::cerr << cstr.str();
   320         pos+=cstr.str().size();
   321         pos+=cstr.str().size();
   321       }
   322       }
   322     for(std::vector<OtherArg>::iterator i=_others_help.begin();
   323     for(std::vector<OtherArg>::const_iterator i=_others_help.begin();
   323         i!=_others_help.end();++i)
   324         i!=_others_help.end();++i)
   324       {
   325       {
   325         std::ostringstream cstr;
   326         std::ostringstream cstr;
   326         cstr << ' ' << i->name;
   327         cstr << ' ' << i->name;
   327 
   328 
   333         pos+=cstr.str().size();
   334         pos+=cstr.str().size();
   334       }
   335       }
   335     std::cerr << std::endl;
   336     std::cerr << std::endl;
   336   }
   337   }
   337 
   338 
   338   void ArgParser::showHelp()
   339   void ArgParser::showHelp() const
   339   {
   340   {
   340     shortHelp();
   341     shortHelp();
   341     std::cerr << "Where:\n";
   342     std::cerr << "Where:\n";
   342     for(std::vector<OtherArg>::iterator i=_others_help.begin();
   343     for(std::vector<OtherArg>::const_iterator i=_others_help.begin();
   343         i!=_others_help.end();++i) showHelp(i);
   344         i!=_others_help.end();++i) showHelp(i);
   344     for(Opts::iterator i=_opts.begin();i!=_opts.end();++i) showHelp(i);
   345     for(Opts::const_iterator i=_opts.begin();i!=_opts.end();++i) showHelp(i);
   345     exit(1);
   346     exit(1);
   346   }
   347   }
   347 
   348 
   348 
   349 
   349   void ArgParser::unknownOpt(std::string arg)
   350   void ArgParser::unknownOpt(std::string arg) const
   350   {
   351   {
   351     std::cerr << "\nUnknown option: " << arg << "\n";
   352     std::cerr << "\nUnknown option: " << arg << "\n";
   352     std::cerr << "\nType '" << _command_name <<
   353     std::cerr << "\nType '" << _command_name <<
   353       " --help' to obtain a short summary on the usage.\n\n";
   354       " --help' to obtain a short summary on the usage.\n\n";
   354     exit(1);
   355     exit(1);
   355   }
   356   }
   356 
   357 
   357   void ArgParser::requiresValue(std::string arg, OptType t)
   358   void ArgParser::requiresValue(std::string arg, OptType t) const
   358   {
   359   {
   359     std::cerr << "Argument '" << arg << "' requires a";
   360     std::cerr << "Argument '" << arg << "' requires a";
   360     switch(t) {
   361     switch(t) {
   361     case STRING:
   362     case STRING:
   362       std::cerr << " string";
   363       std::cerr << " string";
   373     std::cerr << " value\n\n";
   374     std::cerr << " value\n\n";
   374     showHelp();
   375     showHelp();
   375   }
   376   }
   376 
   377 
   377 
   378 
   378   void ArgParser::checkMandatories()
   379   void ArgParser::checkMandatories() const
   379   {
   380   {
   380     bool ok=true;
   381     bool ok=true;
   381     for(Opts::iterator i=_opts.begin();i!=_opts.end();++i)
   382     for(Opts::const_iterator i=_opts.begin();i!=_opts.end();++i)
   382       if(i->second.mandatory&&!i->second.set)
   383       if(i->second.mandatory&&!i->second.set)
   383         {
   384         {
   384           if(ok)
   385           if(ok)
   385             std::cerr << _command_name
   386             std::cerr << _command_name
   386                       << ": The following mandatory arguments are missing.\n";
   387                       << ": The following mandatory arguments are missing.\n";
   387           ok=false;
   388           ok=false;
   388           showHelp(i);
   389           showHelp(i);
   389         }
   390         }
   390     for(Groups::iterator i=_groups.begin();i!=_groups.end();++i)
   391     for(Groups::const_iterator i=_groups.begin();i!=_groups.end();++i)
   391       if(i->second.mandatory||i->second.only_one)
   392       if(i->second.mandatory||i->second.only_one)
   392         {
   393         {
   393           int set=0;
   394           int set=0;
   394           for(GroupData::Opts::iterator o=i->second.opts.begin();
   395           for(GroupData::Opts::const_iterator o=i->second.opts.begin();
   395               o!=i->second.opts.end();++o)
   396               o!=i->second.opts.end();++o)
   396             if(_opts.find(*o)->second.set) ++set;
   397             if(_opts.find(*o)->second.set) ++set;
   397           if(i->second.mandatory&&!set) {
   398           if(i->second.mandatory&&!set) {
   398             std::cerr << _command_name <<
   399             std::cerr << _command_name <<
   399               ": At least one of the following arguments is mandatory.\n";
   400               ": At least one of the following arguments is mandatory.\n";
   400             ok=false;
   401             ok=false;
   401             for(GroupData::Opts::iterator o=i->second.opts.begin();
   402             for(GroupData::Opts::const_iterator o=i->second.opts.begin();
   402                 o!=i->second.opts.end();++o)
   403                 o!=i->second.opts.end();++o)
   403               showHelp(_opts.find(*o));
   404               showHelp(_opts.find(*o));
   404           }
   405           }
   405           if(i->second.only_one&&set>1) {
   406           if(i->second.only_one&&set>1) {
   406             std::cerr << _command_name <<
   407             std::cerr << _command_name <<
   407               ": At most one of the following arguments can be given.\n";
   408               ": At most one of the following arguments can be given.\n";
   408             ok=false;
   409             ok=false;
   409             for(GroupData::Opts::iterator o=i->second.opts.begin();
   410             for(GroupData::Opts::const_iterator o=i->second.opts.begin();
   410                 o!=i->second.opts.end();++o)
   411                 o!=i->second.opts.end();++o)
   411               showHelp(_opts.find(*o));
   412               showHelp(_opts.find(*o));
   412           }
   413           }
   413         }
   414         }
   414     if(!ok) {
   415     if(!ok) {