gravatar
alpar (Alpar Juttner)
alpar@cs.elte.hu
Some cleanup in ArgParser API (ticket #116)
0 2 0
default
2 files changed with 45 insertions and 41 deletions:
↑ Collapse diff ↑
Ignore white space 4 line context
... ...
@@ -240,9 +240,9 @@
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 244
    os << "-" << i->first;
245 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 247
        if(j->second.syn&&j->second.help==i->first)
248 248
          os << "|-" << j->first;
... ...
@@ -262,7 +262,7 @@
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 267
    while(o!=i->second.opts.end()) {
268 268
      show(os,_opts.find(*o));
... ...
@@ -272,5 +272,5 @@
272 272
  }
273 273

	
274
  void ArgParser::showHelp(Opts::iterator i)
274
  void ArgParser::showHelp(Opts::const_iterator i) const
275 275
  {
276 276
    if(i->second.help.size()==0||i->second.syn) return;
... ...
@@ -280,5 +280,6 @@
280 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 285
    if(i->help.size()==0) return;
... ...
@@ -287,5 +288,5 @@
287 288
  }
288 289

	
289
  void ArgParser::shortHelp()
290
  void ArgParser::shortHelp() const
290 291
  {
291 292
    const unsigned int LINE_LEN=77;
... ...
@@ -293,5 +294,5 @@
293 294
    std::cerr << "Usage:\n  " << _command_name;
294 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 297
      std::ostringstream cstr;
297 298
      cstr << ' ';
... ...
@@ -306,5 +307,5 @@
306 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 310
      if(!i->second.ingroup&&!i->second.syn) {
310 311
        std::ostringstream cstr;
... ...
@@ -320,5 +321,5 @@
320 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 324
        i!=_others_help.end();++i)
324 325
      {
... ...
@@ -336,16 +337,16 @@
336 337
  }
337 338

	
338
  void ArgParser::showHelp()
339
  void ArgParser::showHelp() const
339 340
  {
340 341
    shortHelp();
341 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 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 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 352
    std::cerr << "\nUnknown option: " << arg << "\n";
... ...
@@ -355,5 +356,5 @@
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 360
    std::cerr << "Argument '" << arg << "' requires a";
... ...
@@ -376,8 +377,8 @@
376 377

	
377 378

	
378
  void ArgParser::checkMandatories()
379
  void ArgParser::checkMandatories() const
379 380
  {
380 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 383
      if(i->second.mandatory&&!i->second.set)
383 384
        {
... ...
@@ -388,9 +389,9 @@
388 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 392
      if(i->second.mandatory||i->second.only_one)
392 393
        {
393 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 396
              o!=i->second.opts.end();++o)
396 397
            if(_opts.find(*o)->second.set) ++set;
... ...
@@ -399,5 +400,5 @@
399 400
              ": At least one of the following arguments is mandatory.\n";
400 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 403
                o!=i->second.opts.end();++o)
403 404
              showHelp(_opts.find(*o));
... ...
@@ -407,5 +408,5 @@
407 408
              ": At most one of the following arguments can be given.\n";
408 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 411
                o!=i->second.opts.end();++o)
411 412
              showHelp(_opts.find(*o));
Ignore white space 4 line context
... ...
@@ -272,15 +272,18 @@
272 272
    ///@}
273 273

	
274
    void show(std::ostream &os,Opts::iterator i);
275
    void show(std::ostream &os,Groups::iterator i);
276
    void showHelp(Opts::iterator i);
277
    void showHelp(std::vector<OtherArg>::iterator i);
278
    void shortHelp();
279
    void showHelp();
274
  private:
275
    void show(std::ostream &os,Opts::const_iterator i) const;
276
    void show(std::ostream &os,Groups::const_iterator i) const;
277
    void showHelp(Opts::const_iterator i) const;
278
    void showHelp(std::vector<OtherArg>::const_iterator i) const;
280 279

	
281
    void unknownOpt(std::string arg);
280
    void unknownOpt(std::string arg) const;
282 281

	
283
    void requiresValue(std::string arg, OptType t);
284
    void checkMandatories();
282
    void requiresValue(std::string arg, OptType t) const;
283
    void checkMandatories() const;
284

	
285
    void shortHelp() const;
286
    void showHelp() const;
287
  public:
285 288

	
286 289
    ///Start the parsing process
... ...
@@ -294,10 +297,10 @@
294 297

	
295 298
    ///Give back the command name (the 0th argument)
296
    const std::string &commandName() { return _command_name; }
299
    const std::string &commandName() const { return _command_name; }
297 300

	
298 301
    ///Check if an opion has been given to the command.
299
    bool given(std::string op)
302
    bool given(std::string op) const
300 303
    {
301
      Opts::iterator i = _opts.find(op);
304
      Opts::const_iterator i = _opts.find(op);
302 305
      return i!=_opts.end()?i->second.set:false;
303 306
    }
... ...
@@ -312,13 +315,13 @@
312 315
    class RefType
313 316
    {
314
      ArgParser &_parser;
317
      const ArgParser &_parser;
315 318
      std::string _name;
316 319
    public:
317 320
      ///\e
318
      RefType(ArgParser &p,const std::string &n) :_parser(p),_name(n) {}
321
      RefType(const ArgParser &p,const std::string &n) :_parser(p),_name(n) {}
319 322
      ///\e
320 323
      operator bool()
321 324
      {
322
        Opts::iterator i = _parser._opts.find(_name);
325
        Opts::const_iterator i = _parser._opts.find(_name);
323 326
        LEMON_ASSERT(i!=_parser._opts.end(),
324 327
                     std::string()+"Unkown option: '"+_name+"'");
... ...
@@ -330,5 +333,5 @@
330 333
      operator std::string()
331 334
      {
332
        Opts::iterator i = _parser._opts.find(_name);
335
        Opts::const_iterator i = _parser._opts.find(_name);
333 336
        LEMON_ASSERT(i!=_parser._opts.end(),
334 337
                     std::string()+"Unkown option: '"+_name+"'");
... ...
@@ -340,5 +343,5 @@
340 343
      operator double()
341 344
      {
342
        Opts::iterator i = _parser._opts.find(_name);
345
        Opts::const_iterator i = _parser._opts.find(_name);
343 346
        LEMON_ASSERT(i!=_parser._opts.end(),
344 347
                     std::string()+"Unkown option: '"+_name+"'");
... ...
@@ -352,5 +355,5 @@
352 355
      operator int()
353 356
      {
354
        Opts::iterator i = _parser._opts.find(_name);
357
        Opts::const_iterator i = _parser._opts.find(_name);
355 358
        LEMON_ASSERT(i!=_parser._opts.end(),
356 359
                     std::string()+"Unkown option: '"+_name+"'");
... ...
@@ -366,5 +369,5 @@
366 369
    ///Give back the value of an option.
367 370
    ///\sa RefType
368
    RefType operator[](const std::string &n)
371
    RefType operator[](const std::string &n) const
369 372
    {
370 373
      return RefType(*this, n);
... ...
@@ -375,5 +378,5 @@
375 378
    ///Give back a reference to a vector consisting of the program arguments
376 379
    ///not starting with a '-' character.
377
    std::vector<std::string> &files() { return _file_args; }
380
    const std::vector<std::string> &files() const { return _file_args; }
378 381

	
379 382
  };
0 comments (0 inline)