lemon/arg_parser.cc
author Balazs Dezso <deba@inf.elte.hu>
Tue, 29 Jul 2008 10:27:36 +0200
changeset 237 9c8efad5f587
parent 210 81cfc04531e8
child 311 c887e703b566
permissions -rw-r--r--
Fixing constness of Graph in GraphWriter
     1 /* -*- mode: C++; indent-tabs-mode: nil; -*-
     2  *
     3  * This file is a part of LEMON, a generic C++ optimization library.
     4  *
     5  * Copyright (C) 2003-2008
     6  * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
     7  * (Egervary Research Group on Combinatorial Optimization, EGRES).
     8  *
     9  * Permission to use, modify and distribute this software is granted
    10  * provided that this copyright notice appears in all copies. For
    11  * precise terms see the accompanying LICENSE file.
    12  *
    13  * This software is provided "AS IS" with no warranty of any kind,
    14  * express or implied, and with no claim as to its suitability for any
    15  * purpose.
    16  *
    17  */
    18 
    19 #include <lemon/arg_parser.h>
    20 
    21 namespace lemon {
    22 
    23   void ArgParser::_showHelp(void *p)
    24   {
    25     (static_cast<ArgParser*>(p))->showHelp();
    26     exit(1);
    27   }
    28 
    29   ArgParser::ArgParser(int argc, const char **argv) :_argc(argc), _argv(argv),
    30                                                      _command_name(argv[0]) {
    31     funcOption("-help","Print a short help message",_showHelp,this);
    32     synonym("help","-help");
    33     synonym("h","-help");
    34 
    35   }
    36 
    37   ArgParser::~ArgParser()
    38   {
    39     for(Opts::iterator i=_opts.begin();i!=_opts.end();++i)
    40       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 
    61 
    62   ArgParser &ArgParser::intOption(const std::string &name,
    63                                const std::string &help,
    64                                int value, bool obl)
    65   {
    66     ParData p;
    67     p.int_p=new int(value);
    68     p.self_delete=true;
    69     p.help=help;
    70     p.type=INTEGER;
    71     p.mandatory=obl;
    72     _opts[name]=p;
    73     return *this;
    74   }
    75 
    76   ArgParser &ArgParser::doubleOption(const std::string &name,
    77                                const std::string &help,
    78                                double value, bool obl)
    79   {
    80     ParData p;
    81     p.double_p=new double(value);
    82     p.self_delete=true;
    83     p.help=help;
    84     p.type=DOUBLE;
    85     p.mandatory=obl;
    86     _opts[name]=p;
    87     return *this;
    88   }
    89 
    90   ArgParser &ArgParser::boolOption(const std::string &name,
    91                                const std::string &help,
    92                                bool value, bool obl)
    93   {
    94     ParData p;
    95     p.bool_p=new bool(value);
    96     p.self_delete=true;
    97     p.help=help;
    98     p.type=BOOL;
    99     p.mandatory=obl;
   100     _opts[name]=p;
   101     return *this;
   102   }
   103 
   104   ArgParser &ArgParser::stringOption(const std::string &name,
   105                                const std::string &help,
   106                                std::string value, bool obl)
   107   {
   108     ParData p;
   109     p.string_p=new std::string(value);
   110     p.self_delete=true;
   111     p.help=help;
   112     p.type=STRING;
   113     p.mandatory=obl;
   114     _opts[name]=p;
   115     return *this;
   116   }
   117 
   118   ArgParser &ArgParser::refOption(const std::string &name,
   119                                const std::string &help,
   120                                int &ref, bool obl)
   121   {
   122     ParData p;
   123     p.int_p=&ref;
   124     p.self_delete=false;
   125     p.help=help;
   126     p.type=INTEGER;
   127     p.mandatory=obl;
   128     _opts[name]=p;
   129     return *this;
   130   }
   131 
   132   ArgParser &ArgParser::refOption(const std::string &name,
   133                                   const std::string &help,
   134                                   double &ref, bool obl)
   135   {
   136     ParData p;
   137     p.double_p=&ref;
   138     p.self_delete=false;
   139     p.help=help;
   140     p.type=DOUBLE;
   141     p.mandatory=obl;
   142     _opts[name]=p;
   143     return *this;
   144   }
   145 
   146   ArgParser &ArgParser::refOption(const std::string &name,
   147                                   const std::string &help,
   148                                   bool &ref, bool obl)
   149   {
   150     ParData p;
   151     p.bool_p=&ref;
   152     p.self_delete=false;
   153     p.help=help;
   154     p.type=BOOL;
   155     p.mandatory=obl;
   156     _opts[name]=p;
   157 
   158     ref = false;
   159 
   160     return *this;
   161   }
   162 
   163   ArgParser &ArgParser::refOption(const std::string &name,
   164                                const std::string &help,
   165                                std::string &ref, bool obl)
   166   {
   167     ParData p;
   168     p.string_p=&ref;
   169     p.self_delete=false;
   170     p.help=help;
   171     p.type=STRING;
   172     p.mandatory=obl;
   173     _opts[name]=p;
   174     return *this;
   175   }
   176 
   177   ArgParser &ArgParser::funcOption(const std::string &name,
   178                                const std::string &help,
   179                                void (*func)(void *),void *data)
   180   {
   181     ParData p;
   182     p.func_p.p=func;
   183     p.func_p.data=data;
   184     p.self_delete=false;
   185     p.help=help;
   186     p.type=FUNC;
   187     p.mandatory=false;
   188     _opts[name]=p;
   189     return *this;
   190   }
   191 
   192   ArgParser &ArgParser::optionGroup(const std::string &group,
   193                                     const std::string &opt)
   194   {
   195     Opts::iterator i = _opts.find(opt);
   196     LEMON_ASSERT(i!=_opts.end(), "Unknown option: '"+opt+"'");
   197     LEMON_ASSERT(!(i->second.ingroup),
   198                  "Option already in option group: '"+opt+"'");
   199     GroupData &g=_groups[group];
   200     g.opts.push_back(opt);
   201     i->second.ingroup=true;
   202     return *this;
   203   }
   204 
   205   ArgParser &ArgParser::onlyOneGroup(const std::string &group)
   206   {
   207     GroupData &g=_groups[group];
   208     g.only_one=true;
   209     return *this;
   210   }
   211 
   212   ArgParser &ArgParser::synonym(const std::string &syn,
   213                                 const std::string &opt)
   214   {
   215     Opts::iterator o = _opts.find(opt);
   216     Opts::iterator s = _opts.find(syn);
   217     LEMON_ASSERT(o!=_opts.end(), "Unknown option: '"+opt+"'");
   218     LEMON_ASSERT(s==_opts.end(), "Option already used: '"+syn+"'");
   219     ParData p;
   220     p.help=opt;
   221     p.mandatory=false;
   222     p.syn=true;
   223     _opts[syn]=p;
   224     o->second.has_syn=true;
   225     return *this;
   226   }
   227 
   228   ArgParser &ArgParser::mandatoryGroup(const std::string &group)
   229   {
   230     GroupData &g=_groups[group];
   231     g.mandatory=true;
   232     return *this;
   233   }
   234 
   235   ArgParser &ArgParser::other(const std::string &name,
   236                               const std::string &help)
   237   {
   238     _others_help.push_back(OtherArg(name,help));
   239     return *this;
   240   }
   241 
   242   void ArgParser::show(std::ostream &os,Opts::const_iterator i) const
   243   {
   244     os << "-" << i->first;
   245     if(i->second.has_syn)
   246       for(Opts::const_iterator j=_opts.begin();j!=_opts.end();++j)
   247         if(j->second.syn&&j->second.help==i->first)
   248           os << "|-" << j->first;
   249     switch(i->second.type) {
   250     case STRING:
   251       os << " str";
   252       break;
   253     case INTEGER:
   254       os << " int";
   255       break;
   256     case DOUBLE:
   257       os << " num";
   258       break;
   259     default:
   260       break;
   261     }
   262   }
   263 
   264   void ArgParser::show(std::ostream &os,Groups::const_iterator i) const
   265   {
   266     GroupData::Opts::const_iterator o=i->second.opts.begin();
   267     while(o!=i->second.opts.end()) {
   268       show(os,_opts.find(*o));
   269       ++o;
   270       if(o!=i->second.opts.end()) os<<'|';
   271     }
   272   }
   273 
   274   void ArgParser::showHelp(Opts::const_iterator i) const
   275   {
   276     if(i->second.help.size()==0||i->second.syn) return;
   277     std::cerr << "  ";
   278     show(std::cerr,i);
   279     std::cerr << std::endl;
   280     std::cerr << "     " << i->second.help << std::endl;
   281   }
   282   void ArgParser::showHelp(std::vector<ArgParser::OtherArg>::const_iterator i)
   283     const
   284   {
   285     if(i->help.size()==0) return;
   286     std::cerr << "  " << i->name << std::endl
   287               << "     " << i->help << std::endl;
   288   }
   289 
   290   void ArgParser::shortHelp() const
   291   {
   292     const unsigned int LINE_LEN=77;
   293     const std::string indent("    ");
   294     std::cerr << "Usage:\n  " << _command_name;
   295     int pos=_command_name.size()+2;
   296     for(Groups::const_iterator g=_groups.begin();g!=_groups.end();++g) {
   297       std::ostringstream cstr;
   298       cstr << ' ';
   299       if(!g->second.mandatory) cstr << '[';
   300       show(cstr,g);
   301       if(!g->second.mandatory) cstr << ']';
   302       if(pos+cstr.str().size()>LINE_LEN) {
   303         std::cerr << std::endl << indent;
   304         pos=indent.size();
   305       }
   306       std::cerr << cstr.str();
   307       pos+=cstr.str().size();
   308     }
   309     for(Opts::const_iterator i=_opts.begin();i!=_opts.end();++i)
   310       if(!i->second.ingroup&&!i->second.syn) {
   311         std::ostringstream cstr;
   312         cstr << ' ';
   313         if(!i->second.mandatory) cstr << '[';
   314         show(cstr,i);
   315         if(!i->second.mandatory) cstr << ']';
   316         if(pos+cstr.str().size()>LINE_LEN) {
   317           std::cerr << std::endl << indent;
   318           pos=indent.size();
   319         }
   320         std::cerr << cstr.str();
   321         pos+=cstr.str().size();
   322       }
   323     for(std::vector<OtherArg>::const_iterator i=_others_help.begin();
   324         i!=_others_help.end();++i)
   325       {
   326         std::ostringstream cstr;
   327         cstr << ' ' << i->name;
   328 
   329         if(pos+cstr.str().size()>LINE_LEN) {
   330           std::cerr << std::endl << indent;
   331           pos=indent.size();
   332         }
   333         std::cerr << cstr.str();
   334         pos+=cstr.str().size();
   335       }
   336     std::cerr << std::endl;
   337   }
   338 
   339   void ArgParser::showHelp() const
   340   {
   341     shortHelp();
   342     std::cerr << "Where:\n";
   343     for(std::vector<OtherArg>::const_iterator i=_others_help.begin();
   344         i!=_others_help.end();++i) showHelp(i);
   345     for(Opts::const_iterator i=_opts.begin();i!=_opts.end();++i) showHelp(i);
   346     exit(1);
   347   }
   348 
   349 
   350   void ArgParser::unknownOpt(std::string arg) const
   351   {
   352     std::cerr << "\nUnknown option: " << arg << "\n";
   353     std::cerr << "\nType '" << _command_name <<
   354       " --help' to obtain a short summary on the usage.\n\n";
   355     exit(1);
   356   }
   357 
   358   void ArgParser::requiresValue(std::string arg, OptType t) const
   359   {
   360     std::cerr << "Argument '" << arg << "' requires a";
   361     switch(t) {
   362     case STRING:
   363       std::cerr << " string";
   364       break;
   365     case INTEGER:
   366       std::cerr << "n integer";
   367       break;
   368     case DOUBLE:
   369       std::cerr << " floating point";
   370       break;
   371     default:
   372       break;
   373     }
   374     std::cerr << " value\n\n";
   375     showHelp();
   376   }
   377 
   378 
   379   void ArgParser::checkMandatories() const
   380   {
   381     bool ok=true;
   382     for(Opts::const_iterator i=_opts.begin();i!=_opts.end();++i)
   383       if(i->second.mandatory&&!i->second.set)
   384         {
   385           if(ok)
   386             std::cerr << _command_name
   387                       << ": The following mandatory arguments are missing.\n";
   388           ok=false;
   389           showHelp(i);
   390         }
   391     for(Groups::const_iterator i=_groups.begin();i!=_groups.end();++i)
   392       if(i->second.mandatory||i->second.only_one)
   393         {
   394           int set=0;
   395           for(GroupData::Opts::const_iterator o=i->second.opts.begin();
   396               o!=i->second.opts.end();++o)
   397             if(_opts.find(*o)->second.set) ++set;
   398           if(i->second.mandatory&&!set) {
   399             std::cerr << _command_name <<
   400               ": At least one of the following arguments is mandatory.\n";
   401             ok=false;
   402             for(GroupData::Opts::const_iterator o=i->second.opts.begin();
   403                 o!=i->second.opts.end();++o)
   404               showHelp(_opts.find(*o));
   405           }
   406           if(i->second.only_one&&set>1) {
   407             std::cerr << _command_name <<
   408               ": At most one of the following arguments can be given.\n";
   409             ok=false;
   410             for(GroupData::Opts::const_iterator o=i->second.opts.begin();
   411                 o!=i->second.opts.end();++o)
   412               showHelp(_opts.find(*o));
   413           }
   414         }
   415     if(!ok) {
   416       std::cerr << "\nType '" << _command_name <<
   417         " --help' to obtain a short summary on the usage.\n\n";
   418       exit(1);
   419     }
   420   }
   421 
   422   ArgParser &ArgParser::parse()
   423   {
   424     for(int ar=1; ar<_argc; ++ar) {
   425       std::string arg(_argv[ar]);
   426       if (arg[0] != '-' || arg.size() == 1) {
   427         _file_args.push_back(arg);
   428       }
   429       else {
   430         Opts::iterator i = _opts.find(arg.substr(1));
   431         if(i==_opts.end()) unknownOpt(arg);
   432         else {
   433           if(i->second.syn) i=_opts.find(i->second.help);
   434           ParData &p(i->second);
   435           if (p.type==BOOL) *p.bool_p=true;
   436           else if (p.type==FUNC) p.func_p.p(p.func_p.data);
   437           else if(++ar==_argc) requiresValue(arg, p.type);
   438           else {
   439             std::string val(_argv[ar]);
   440             std::istringstream vals(val);
   441             switch(p.type) {
   442             case STRING:
   443               *p.string_p=val;
   444               break;
   445             case INTEGER:
   446               vals >> *p.int_p;
   447               break;
   448             case DOUBLE:
   449               vals >> *p.double_p;
   450               break;
   451             default:
   452               break;
   453             }
   454             if(p.type!=STRING&&(!vals||!vals.eof()))
   455               requiresValue(arg, p.type);
   456           }
   457           p.set = true;
   458         }
   459       }
   460     }
   461     checkMandatories();
   462 
   463     return *this;
   464   }
   465 
   466 }