COIN-OR::LEMON - Graph Library

Changeset 209:765619b7cbb2 in lemon-main for lemon/arg_parser.cc


Ignore:
Timestamp:
07/13/08 20:51:02 (16 years ago)
Author:
Alpar Juttner <alpar@…>
Branch:
default
Phase:
public
Message:

Apply unify-sources.sh to the source tree

File:
1 edited

Legend:

Unmodified
Added
Removed
  • lemon/arg_parser.cc

    r137 r209  
    1 /* -*- C++ -*-
     1/* -*- mode: C++; indent-tabs-mode: nil; -*-
    22 *
    3  * This file is a part of LEMON, a generic C++ optimization library
     3 * This file is a part of LEMON, a generic C++ optimization library.
    44 *
    55 * Copyright (C) 2003-2008
     
    3939    for(Opts::iterator i=_opts.begin();i!=_opts.end();++i)
    4040      if(i->second.self_delete)
    41         switch(i->second.type) {
    42         case BOOL:
    43           delete i->second.bool_p;
    44           break;
    45         case STRING:
    46           delete i->second.string_p;
    47           break;
    48         case DOUBLE:
    49           delete i->second.double_p;
    50           break;
    51         case INTEGER:
    52           delete i->second.int_p;
    53           break;
    54         case UNKNOWN:
    55           break;
    56         case FUNC:
    57           break;
    58         }
    59   }
    60  
     41        switch(i->second.type) {
     42        case BOOL:
     43          delete i->second.bool_p;
     44          break;
     45        case STRING:
     46          delete i->second.string_p;
     47          break;
     48        case DOUBLE:
     49          delete i->second.double_p;
     50          break;
     51        case INTEGER:
     52          delete i->second.int_p;
     53          break;
     54        case UNKNOWN:
     55          break;
     56        case FUNC:
     57          break;
     58        }
     59  }
     60
    6161
    6262  ArgParser &ArgParser::intOption(const std::string &name,
    63                                const std::string &help,
    64                                int value, bool obl)
     63                               const std::string &help,
     64                               int value, bool obl)
    6565  {
    6666    ParData p;
     
    7575
    7676  ArgParser &ArgParser::doubleOption(const std::string &name,
    77                                const std::string &help,
    78                                double value, bool obl)
     77                               const std::string &help,
     78                               double value, bool obl)
    7979  {
    8080    ParData p;
     
    8989
    9090  ArgParser &ArgParser::boolOption(const std::string &name,
    91                                const std::string &help,
    92                                bool value, bool obl)
     91                               const std::string &help,
     92                               bool value, bool obl)
    9393  {
    9494    ParData p;
     
    103103
    104104  ArgParser &ArgParser::stringOption(const std::string &name,
    105                                const std::string &help,
    106                                std::string value, bool obl)
     105                               const std::string &help,
     106                               std::string value, bool obl)
    107107  {
    108108    ParData p;
     
    117117
    118118  ArgParser &ArgParser::refOption(const std::string &name,
    119                                const std::string &help,
    120                                int &ref, bool obl)
     119                               const std::string &help,
     120                               int &ref, bool obl)
    121121  {
    122122    ParData p;
     
    162162
    163163  ArgParser &ArgParser::refOption(const std::string &name,
    164                                const std::string &help,
    165                                std::string &ref, bool obl)
     164                               const std::string &help,
     165                               std::string &ref, bool obl)
    166166  {
    167167    ParData p;
     
    176176
    177177  ArgParser &ArgParser::funcOption(const std::string &name,
    178                                const std::string &help,
    179                                void (*func)(void *),void *data)
     178                               const std::string &help,
     179                               void (*func)(void *),void *data)
    180180  {
    181181    ParData p;
     
    191191
    192192  ArgParser &ArgParser::optionGroup(const std::string &group,
    193                                     const std::string &opt)
     193                                    const std::string &opt)
    194194  {
    195195    Opts::iterator i = _opts.find(opt);
    196196    LEMON_ASSERT(i!=_opts.end(), "Unknown option: '"+opt+"'");
    197     LEMON_ASSERT(!(i->second.ingroup), 
     197    LEMON_ASSERT(!(i->second.ingroup),
    198198                 "Option already in option group: '"+opt+"'");
    199199    GroupData &g=_groups[group];
     
    211211
    212212  ArgParser &ArgParser::synonym(const std::string &syn,
    213                                 const std::string &opt)
     213                                const std::string &opt)
    214214  {
    215215    Opts::iterator o = _opts.find(opt);
     
    234234
    235235  ArgParser &ArgParser::other(const std::string &name,
    236                               const std::string &help)
     236                              const std::string &help)
    237237  {
    238238    _others_help.push_back(OtherArg(name,help));
     
    245245    if(i->second.has_syn)
    246246      for(Opts::iterator j=_opts.begin();j!=_opts.end();++j)
    247         if(j->second.syn&&j->second.help==i->first)
    248           os << "|-" << j->first;
     247        if(j->second.syn&&j->second.help==i->first)
     248          os << "|-" << j->first;
    249249    switch(i->second.type) {
    250250    case STRING:
     
    271271    }
    272272  }
    273    
     273
    274274  void ArgParser::showHelp(Opts::iterator i)
    275275  {
     
    284284    if(i->help.size()==0) return;
    285285    std::cerr << "  " << i->name << std::endl
    286               << "     " << i->help << std::endl;
    287   }
    288    
     286              << "     " << i->help << std::endl;
     287  }
     288
    289289  void ArgParser::shortHelp()
    290290  {
     
    300300      if(!g->second.mandatory) cstr << ']';
    301301      if(pos+cstr.str().size()>LINE_LEN) {
    302         std::cerr << std::endl << indent;
    303         pos=indent.size();
     302        std::cerr << std::endl << indent;
     303        pos=indent.size();
    304304      }
    305305      std::cerr << cstr.str();
     
    308308    for(Opts::iterator i=_opts.begin();i!=_opts.end();++i)
    309309      if(!i->second.ingroup&&!i->second.syn) {
    310         std::ostringstream cstr;
    311         cstr << ' ';
    312         if(!i->second.mandatory) cstr << '[';
    313         show(cstr,i);
    314         if(!i->second.mandatory) cstr << ']';
    315         if(pos+cstr.str().size()>LINE_LEN) {
    316           std::cerr << std::endl << indent;
    317           pos=indent.size();
    318         }
    319         std::cerr << cstr.str();
    320         pos+=cstr.str().size();
     310        std::ostringstream cstr;
     311        cstr << ' ';
     312        if(!i->second.mandatory) cstr << '[';
     313        show(cstr,i);
     314        if(!i->second.mandatory) cstr << ']';
     315        if(pos+cstr.str().size()>LINE_LEN) {
     316          std::cerr << std::endl << indent;
     317          pos=indent.size();
     318        }
     319        std::cerr << cstr.str();
     320        pos+=cstr.str().size();
    321321      }
    322322    for(std::vector<OtherArg>::iterator i=_others_help.begin();
    323         i!=_others_help.end();++i)
     323        i!=_others_help.end();++i)
    324324      {
    325         std::ostringstream cstr;
    326         cstr << ' ' << i->name;
    327      
    328         if(pos+cstr.str().size()>LINE_LEN) {
    329           std::cerr << std::endl << indent;
    330           pos=indent.size();
    331         }
    332         std::cerr << cstr.str();
    333         pos+=cstr.str().size();
     325        std::ostringstream cstr;
     326        cstr << ' ' << i->name;
     327
     328        if(pos+cstr.str().size()>LINE_LEN) {
     329          std::cerr << std::endl << indent;
     330          pos=indent.size();
     331        }
     332        std::cerr << cstr.str();
     333        pos+=cstr.str().size();
    334334      }
    335335    std::cerr << std::endl;
    336336  }
    337    
     337
    338338  void ArgParser::showHelp()
    339339  {
     
    341341    std::cerr << "Where:\n";
    342342    for(std::vector<OtherArg>::iterator i=_others_help.begin();
    343         i!=_others_help.end();++i) showHelp(i);
     343        i!=_others_help.end();++i) showHelp(i);
    344344    for(Opts::iterator i=_opts.begin();i!=_opts.end();++i) showHelp(i);
    345345    exit(1);
    346346  }
    347    
    348      
    349   void ArgParser::unknownOpt(std::string arg) 
     347
     348
     349  void ArgParser::unknownOpt(std::string arg)
    350350  {
    351351    std::cerr << "\nUnknown option: " << arg << "\n";
     
    354354    exit(1);
    355355  }
    356    
    357   void ArgParser::requiresValue(std::string arg, OptType t) 
     356
     357  void ArgParser::requiresValue(std::string arg, OptType t)
    358358  {
    359359    std::cerr << "Argument '" << arg << "' requires a";
     
    374374    showHelp();
    375375  }
    376    
     376
    377377
    378378  void ArgParser::checkMandatories()
     
    380380    bool ok=true;
    381381    for(Opts::iterator i=_opts.begin();i!=_opts.end();++i)
    382       if(i->second.mandatory&&!i->second.set) 
    383         {
    384           if(ok)
    385             std::cerr << _command_name
    386                       << ": The following mandatory arguments are missing.\n";
    387           ok=false;
    388           showHelp(i);
    389         }
     382      if(i->second.mandatory&&!i->second.set)
     383        {
     384          if(ok)
     385            std::cerr << _command_name
     386                      << ": The following mandatory arguments are missing.\n";
     387          ok=false;
     388          showHelp(i);
     389        }
    390390    for(Groups::iterator i=_groups.begin();i!=_groups.end();++i)
    391391      if(i->second.mandatory||i->second.only_one)
    392         {
    393           int set=0;
    394           for(GroupData::Opts::iterator o=i->second.opts.begin();
    395               o!=i->second.opts.end();++o)
    396             if(_opts.find(*o)->second.set) ++set;
    397           if(i->second.mandatory&&!set) {
    398             std::cerr << _command_name
    399                       << ": At least one of the following arguments is mandatory.\n";
    400             ok=false;
    401             for(GroupData::Opts::iterator o=i->second.opts.begin();
    402                 o!=i->second.opts.end();++o)
    403               showHelp(_opts.find(*o));
    404           }
    405           if(i->second.only_one&&set>1) {
    406             std::cerr << _command_name
    407                       << ": At most one of the following arguments can be given.\n";
    408             ok=false;
    409             for(GroupData::Opts::iterator o=i->second.opts.begin();
    410                 o!=i->second.opts.end();++o)
    411               showHelp(_opts.find(*o));
    412           }
    413         }
     392        {
     393          int set=0;
     394          for(GroupData::Opts::iterator o=i->second.opts.begin();
     395              o!=i->second.opts.end();++o)
     396            if(_opts.find(*o)->second.set) ++set;
     397          if(i->second.mandatory&&!set) {
     398            std::cerr << _command_name
     399                      << ": At least one of the following arguments is mandatory.\n";
     400            ok=false;
     401            for(GroupData::Opts::iterator o=i->second.opts.begin();
     402                o!=i->second.opts.end();++o)
     403              showHelp(_opts.find(*o));
     404          }
     405          if(i->second.only_one&&set>1) {
     406            std::cerr << _command_name
     407                      << ": At most one of the following arguments can be given.\n";
     408            ok=false;
     409            for(GroupData::Opts::iterator o=i->second.opts.begin();
     410                o!=i->second.opts.end();++o)
     411              showHelp(_opts.find(*o));
     412          }
     413        }
    414414    if(!ok) {
    415415      std::cerr << "\nType '" << _command_name <<
    416         " --help' to obtain a short summary on the usage.\n\n";
     416        " --help' to obtain a short summary on the usage.\n\n";
    417417      exit(1);
    418418    }
     
    424424      std::string arg(_argv[ar]);
    425425      if (arg[0] != '-' || arg.size() == 1) {
    426         _file_args.push_back(arg);
     426        _file_args.push_back(arg);
    427427      }
    428428      else {
    429         Opts::iterator i = _opts.find(arg.substr(1));
    430         if(i==_opts.end()) unknownOpt(arg);
    431         else {
    432           if(i->second.syn) i=_opts.find(i->second.help);
    433           ParData &p(i->second);
    434           if (p.type==BOOL) *p.bool_p=true;
    435           else if (p.type==FUNC) p.func_p.p(p.func_p.data);
    436           else if(++ar==_argc) requiresValue(arg, p.type);
    437           else {
    438             std::string val(_argv[ar]);
    439             std::istringstream vals(val);
    440             switch(p.type) {
    441             case STRING:
    442               *p.string_p=val;
    443               break;
    444             case INTEGER:
    445               vals >> *p.int_p;
    446               break;
    447             case DOUBLE:
    448               vals >> *p.double_p;
    449               break;
    450             default:
    451               break;
    452             }
    453             if(p.type!=STRING&&(!vals||!vals.eof()))
    454               requiresValue(arg, p.type);
    455           }
    456           p.set = true;
    457         }
     429        Opts::iterator i = _opts.find(arg.substr(1));
     430        if(i==_opts.end()) unknownOpt(arg);
     431        else {
     432          if(i->second.syn) i=_opts.find(i->second.help);
     433          ParData &p(i->second);
     434          if (p.type==BOOL) *p.bool_p=true;
     435          else if (p.type==FUNC) p.func_p.p(p.func_p.data);
     436          else if(++ar==_argc) requiresValue(arg, p.type);
     437          else {
     438            std::string val(_argv[ar]);
     439            std::istringstream vals(val);
     440            switch(p.type) {
     441            case STRING:
     442              *p.string_p=val;
     443              break;
     444            case INTEGER:
     445              vals >> *p.int_p;
     446              break;
     447            case DOUBLE:
     448              vals >> *p.double_p;
     449              break;
     450            default:
     451              break;
     452            }
     453            if(p.type!=STRING&&(!vals||!vals.eof()))
     454              requiresValue(arg, p.type);
     455          }
     456          p.set = true;
     457        }
    458458      }
    459459    }
     
    461461
    462462    return *this;
    463   } 
     463  }
    464464
    465465}
Note: See TracChangeset for help on using the changeset viewer.