Location: LEMON/LEMON-official/lemon/arg_parser.cc - annotation

Load file history
gravatar
deba@inf.elte.hu
Fixes for MSVC 2008 in grap_adaptors.h and edge_set.h (#194) Several renamings and changes in adaptors and edge sets - Fixing scope usage for MSVC - ResidualDigraph based on SubDigraph instead of FilterArcs - Use initialize() in adaptors and edge sets - Wrap ListDigraph for edge set tests
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
 r209:765619b7cbb2
  r85:3453d20a82cd
 r209:765619b7cbb2
  r85:3453d20a82cd
 r463:88ed40ad0d4f
  r85:3453d20a82cd
  r85:3453d20a82cd
  r85:3453d20a82cd
  r85:3453d20a82cd
  r85:3453d20a82cd
  r85:3453d20a82cd
  r85:3453d20a82cd
  r85:3453d20a82cd
  r85:3453d20a82cd
  r85:3453d20a82cd
  r85:3453d20a82cd
  r85:3453d20a82cd
  r85:3453d20a82cd
  r85:3453d20a82cd
  r85:3453d20a82cd
  r85:3453d20a82cd
  r85:3453d20a82cd
  r85:3453d20a82cd
  r85:3453d20a82cd
  r85:3453d20a82cd
  r85:3453d20a82cd
  r85:3453d20a82cd
  r85:3453d20a82cd
 r311:c887e703b566
 r311:c887e703b566
  r85:3453d20a82cd
  r85:3453d20a82cd
  r85:3453d20a82cd
  r85:3453d20a82cd
  r85:3453d20a82cd
  r85:3453d20a82cd
  r85:3453d20a82cd
  r85:3453d20a82cd
  r85:3453d20a82cd
 r209:765619b7cbb2
 r209:765619b7cbb2
 r209:765619b7cbb2
 r209:765619b7cbb2
 r209:765619b7cbb2
 r209:765619b7cbb2
 r209:765619b7cbb2
 r209:765619b7cbb2
 r209:765619b7cbb2
 r209:765619b7cbb2
 r209:765619b7cbb2
 r209:765619b7cbb2
 r209:765619b7cbb2
 r209:765619b7cbb2
 r209:765619b7cbb2
 r209:765619b7cbb2
 r209:765619b7cbb2
 r209:765619b7cbb2
  r85:3453d20a82cd
 r209:765619b7cbb2
  r85:3453d20a82cd
  r85:3453d20a82cd
 r209:765619b7cbb2
 r209:765619b7cbb2
  r85:3453d20a82cd
  r85:3453d20a82cd
  r85:3453d20a82cd
  r85:3453d20a82cd
  r85:3453d20a82cd
  r85:3453d20a82cd
  r85:3453d20a82cd
  r85:3453d20a82cd
  r85:3453d20a82cd
  r85:3453d20a82cd
  r85:3453d20a82cd
  r85:3453d20a82cd
 r209:765619b7cbb2
 r209:765619b7cbb2
  r85:3453d20a82cd
  r85:3453d20a82cd
  r85:3453d20a82cd
  r85:3453d20a82cd
  r85:3453d20a82cd
  r85:3453d20a82cd
  r85:3453d20a82cd
  r85:3453d20a82cd
  r85:3453d20a82cd
  r85:3453d20a82cd
  r85:3453d20a82cd
  r85:3453d20a82cd
 r209:765619b7cbb2
 r209:765619b7cbb2
  r85:3453d20a82cd
  r85:3453d20a82cd
  r85:3453d20a82cd
  r85:3453d20a82cd
  r85:3453d20a82cd
  r85:3453d20a82cd
  r85:3453d20a82cd
  r85:3453d20a82cd
  r85:3453d20a82cd
  r85:3453d20a82cd
  r85:3453d20a82cd
  r85:3453d20a82cd
 r209:765619b7cbb2
 r209:765619b7cbb2
  r85:3453d20a82cd
  r85:3453d20a82cd
  r85:3453d20a82cd
  r85:3453d20a82cd
  r85:3453d20a82cd
  r85:3453d20a82cd
  r85:3453d20a82cd
  r85:3453d20a82cd
  r85:3453d20a82cd
  r85:3453d20a82cd
  r85:3453d20a82cd
  r85:3453d20a82cd
 r209:765619b7cbb2
 r209:765619b7cbb2
  r85:3453d20a82cd
  r85:3453d20a82cd
  r85:3453d20a82cd
  r85:3453d20a82cd
  r85:3453d20a82cd
  r85:3453d20a82cd
  r85:3453d20a82cd
  r85:3453d20a82cd
  r85:3453d20a82cd
  r85:3453d20a82cd
  r85:3453d20a82cd
  r85:3453d20a82cd
  r85:3453d20a82cd
  r85:3453d20a82cd
  r85:3453d20a82cd
  r85:3453d20a82cd
  r85:3453d20a82cd
  r85:3453d20a82cd
  r85:3453d20a82cd
  r85:3453d20a82cd
  r85:3453d20a82cd
  r85:3453d20a82cd
  r85:3453d20a82cd
  r85:3453d20a82cd
  r85:3453d20a82cd
  r85:3453d20a82cd
  r85:3453d20a82cd
  r85:3453d20a82cd
  r85:3453d20a82cd
  r85:3453d20a82cd
  r85:3453d20a82cd
  r85:3453d20a82cd
  r85:3453d20a82cd
  r85:3453d20a82cd
  r85:3453d20a82cd
  r85:3453d20a82cd
  r85:3453d20a82cd
  r85:3453d20a82cd
  r85:3453d20a82cd
  r85:3453d20a82cd
  r85:3453d20a82cd
  r85:3453d20a82cd
  r85:3453d20a82cd
 r209:765619b7cbb2
 r209:765619b7cbb2
  r85:3453d20a82cd
  r85:3453d20a82cd
  r85:3453d20a82cd
  r85:3453d20a82cd
  r85:3453d20a82cd
  r85:3453d20a82cd
  r85:3453d20a82cd
  r85:3453d20a82cd
  r85:3453d20a82cd
  r85:3453d20a82cd
  r85:3453d20a82cd
  r85:3453d20a82cd
 r209:765619b7cbb2
 r209:765619b7cbb2
  r85:3453d20a82cd
  r85:3453d20a82cd
  r85:3453d20a82cd
  r85:3453d20a82cd
  r85:3453d20a82cd
  r85:3453d20a82cd
  r85:3453d20a82cd
  r85:3453d20a82cd
  r85:3453d20a82cd
  r85:3453d20a82cd
  r85:3453d20a82cd
  r85:3453d20a82cd
  r85:3453d20a82cd
 r209:765619b7cbb2
  r85:3453d20a82cd
  r85:3453d20a82cd
  r85:3453d20a82cd
 r209:765619b7cbb2
  r85:3453d20a82cd
  r85:3453d20a82cd
  r85:3453d20a82cd
  r85:3453d20a82cd
  r85:3453d20a82cd
  r85:3453d20a82cd
  r85:3453d20a82cd
  r85:3453d20a82cd
  r85:3453d20a82cd
  r85:3453d20a82cd
  r85:3453d20a82cd
  r85:3453d20a82cd
  r85:3453d20a82cd
  r85:3453d20a82cd
  r85:3453d20a82cd
 r209:765619b7cbb2
  r85:3453d20a82cd
  r85:3453d20a82cd
  r85:3453d20a82cd
  r85:3453d20a82cd
  r85:3453d20a82cd
  r85:3453d20a82cd
  r85:3453d20a82cd
  r85:3453d20a82cd
  r85:3453d20a82cd
  r85:3453d20a82cd
  r85:3453d20a82cd
  r85:3453d20a82cd
  r85:3453d20a82cd
  r85:3453d20a82cd
  r85:3453d20a82cd
  r85:3453d20a82cd
  r85:3453d20a82cd
  r85:3453d20a82cd
  r85:3453d20a82cd
  r85:3453d20a82cd
  r85:3453d20a82cd
  r85:3453d20a82cd
 r209:765619b7cbb2
  r85:3453d20a82cd
  r85:3453d20a82cd
  r85:3453d20a82cd
  r85:3453d20a82cd
  r85:3453d20a82cd
 r214:60eecd3fe37a
  r85:3453d20a82cd
  r85:3453d20a82cd
  r85:3453d20a82cd
 r214:60eecd3fe37a
 r209:765619b7cbb2
 r209:765619b7cbb2
  r85:3453d20a82cd
  r85:3453d20a82cd
  r85:3453d20a82cd
  r85:3453d20a82cd
  r85:3453d20a82cd
  r85:3453d20a82cd
  r85:3453d20a82cd
  r85:3453d20a82cd
  r85:3453d20a82cd
  r85:3453d20a82cd
  r85:3453d20a82cd
  r85:3453d20a82cd
  r85:3453d20a82cd
  r85:3453d20a82cd
  r85:3453d20a82cd
 r214:60eecd3fe37a
  r85:3453d20a82cd
 r214:60eecd3fe37a
  r85:3453d20a82cd
  r85:3453d20a82cd
  r85:3453d20a82cd
  r85:3453d20a82cd
  r85:3453d20a82cd
  r85:3453d20a82cd
 r209:765619b7cbb2
 r214:60eecd3fe37a
  r85:3453d20a82cd
  r85:3453d20a82cd
  r85:3453d20a82cd
  r85:3453d20a82cd
  r85:3453d20a82cd
  r85:3453d20a82cd
  r85:3453d20a82cd
 r214:60eecd3fe37a
 r214:60eecd3fe37a
  r85:3453d20a82cd
  r85:3453d20a82cd
  r85:3453d20a82cd
 r209:765619b7cbb2
  r85:3453d20a82cd
 r209:765619b7cbb2
 r214:60eecd3fe37a
  r85:3453d20a82cd
  r85:3453d20a82cd
  r85:3453d20a82cd
  r85:3453d20a82cd
  r85:3453d20a82cd
 r214:60eecd3fe37a
  r85:3453d20a82cd
  r85:3453d20a82cd
  r85:3453d20a82cd
  r85:3453d20a82cd
  r85:3453d20a82cd
  r85:3453d20a82cd
 r209:765619b7cbb2
 r209:765619b7cbb2
  r85:3453d20a82cd
  r85:3453d20a82cd
  r85:3453d20a82cd
  r85:3453d20a82cd
 r214:60eecd3fe37a
  r85:3453d20a82cd
 r209:765619b7cbb2
 r209:765619b7cbb2
 r209:765619b7cbb2
 r209:765619b7cbb2
 r209:765619b7cbb2
 r209:765619b7cbb2
 r209:765619b7cbb2
 r209:765619b7cbb2
 r209:765619b7cbb2
 r209:765619b7cbb2
 r209:765619b7cbb2
  r85:3453d20a82cd
 r214:60eecd3fe37a
 r209:765619b7cbb2
  r85:3453d20a82cd
 r209:765619b7cbb2
 r209:765619b7cbb2
 r209:765619b7cbb2
 r209:765619b7cbb2
 r209:765619b7cbb2
 r209:765619b7cbb2
 r209:765619b7cbb2
 r209:765619b7cbb2
 r209:765619b7cbb2
  r85:3453d20a82cd
  r85:3453d20a82cd
  r85:3453d20a82cd
 r209:765619b7cbb2
 r214:60eecd3fe37a
  r85:3453d20a82cd
  r85:3453d20a82cd
  r85:3453d20a82cd
 r214:60eecd3fe37a
 r209:765619b7cbb2
 r214:60eecd3fe37a
  r85:3453d20a82cd
  r85:3453d20a82cd
 r209:765619b7cbb2
 r209:765619b7cbb2
 r214:60eecd3fe37a
  r85:3453d20a82cd
  r85:3453d20a82cd
  r85:3453d20a82cd
  r85:3453d20a82cd
  r85:3453d20a82cd
  r85:3453d20a82cd
 r209:765619b7cbb2
 r214:60eecd3fe37a
  r85:3453d20a82cd
  r85:3453d20a82cd
  r85:3453d20a82cd
  r85:3453d20a82cd
  r85:3453d20a82cd
  r85:3453d20a82cd
  r85:3453d20a82cd
  r85:3453d20a82cd
  r85:3453d20a82cd
  r85:3453d20a82cd
  r85:3453d20a82cd
  r85:3453d20a82cd
  r85:3453d20a82cd
  r85:3453d20a82cd
  r85:3453d20a82cd
  r85:3453d20a82cd
  r85:3453d20a82cd
  r85:3453d20a82cd
 r209:765619b7cbb2
  r85:3453d20a82cd
 r214:60eecd3fe37a
  r85:3453d20a82cd
  r85:3453d20a82cd
 r214:60eecd3fe37a
 r209:765619b7cbb2
 r209:765619b7cbb2
 r209:765619b7cbb2
 r209:765619b7cbb2
 r209:765619b7cbb2
 r209:765619b7cbb2
 r209:765619b7cbb2
 r209:765619b7cbb2
 r214:60eecd3fe37a
  r85:3453d20a82cd
 r209:765619b7cbb2
 r209:765619b7cbb2
 r214:60eecd3fe37a
 r209:765619b7cbb2
 r209:765619b7cbb2
 r209:765619b7cbb2
 r210:81cfc04531e8
 r210:81cfc04531e8
 r209:765619b7cbb2
 r214:60eecd3fe37a
 r209:765619b7cbb2
 r209:765619b7cbb2
 r209:765619b7cbb2
 r209:765619b7cbb2
 r210:81cfc04531e8
 r210:81cfc04531e8
 r209:765619b7cbb2
 r214:60eecd3fe37a
 r209:765619b7cbb2
 r209:765619b7cbb2
 r209:765619b7cbb2
 r209:765619b7cbb2
  r85:3453d20a82cd
  r85:3453d20a82cd
 r209:765619b7cbb2
  r85:3453d20a82cd
  r85:3453d20a82cd
  r85:3453d20a82cd
  r85:3453d20a82cd
  r85:3453d20a82cd
  r85:3453d20a82cd
  r85:3453d20a82cd
  r85:3453d20a82cd
  r85:3453d20a82cd
 r209:765619b7cbb2
  r85:3453d20a82cd
  r85:3453d20a82cd
 r209:765619b7cbb2
 r209:765619b7cbb2
 r209:765619b7cbb2
 r209:765619b7cbb2
 r209:765619b7cbb2
 r209:765619b7cbb2
 r209:765619b7cbb2
 r209:765619b7cbb2
 r209:765619b7cbb2
 r209:765619b7cbb2
 r209:765619b7cbb2
 r209:765619b7cbb2
 r209:765619b7cbb2
 r209:765619b7cbb2
 r209:765619b7cbb2
 r209:765619b7cbb2
 r209:765619b7cbb2
 r209:765619b7cbb2
 r209:765619b7cbb2
 r209:765619b7cbb2
 r209:765619b7cbb2
 r209:765619b7cbb2
 r209:765619b7cbb2
 r209:765619b7cbb2
 r209:765619b7cbb2
 r209:765619b7cbb2
 r209:765619b7cbb2
 r209:765619b7cbb2
 r209:765619b7cbb2
  r85:3453d20a82cd
  r85:3453d20a82cd
  r85:3453d20a82cd
  r85:3453d20a82cd
  r85:3453d20a82cd
 r209:765619b7cbb2
  r85:3453d20a82cd
  r85:3453d20a82cd
/* -*- mode: C++; indent-tabs-mode: nil; -*-
 *
 * This file is a part of LEMON, a generic C++ optimization library.
 *
 * Copyright (C) 2003-2009
 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
 * (Egervary Research Group on Combinatorial Optimization, EGRES).
 *
 * Permission to use, modify and distribute this software is granted
 * provided that this copyright notice appears in all copies. For
 * precise terms see the accompanying LICENSE file.
 *
 * This software is provided "AS IS" with no warranty of any kind,
 * express or implied, and with no claim as to its suitability for any
 * purpose.
 *
 */

#include <lemon/arg_parser.h>

namespace lemon {

  void ArgParser::_showHelp(void *p)
  {
    (static_cast<ArgParser*>(p))->showHelp();
    exit(1);
  }

  ArgParser::ArgParser(int argc, const char * const *argv)
    :_argc(argc), _argv(argv), _command_name(argv[0]) {
    funcOption("-help","Print a short help message",_showHelp,this);
    synonym("help","-help");
    synonym("h","-help");
  }

  ArgParser::~ArgParser()
  {
    for(Opts::iterator i=_opts.begin();i!=_opts.end();++i)
      if(i->second.self_delete)
        switch(i->second.type) {
        case BOOL:
          delete i->second.bool_p;
          break;
        case STRING:
          delete i->second.string_p;
          break;
        case DOUBLE:
          delete i->second.double_p;
          break;
        case INTEGER:
          delete i->second.int_p;
          break;
        case UNKNOWN:
          break;
        case FUNC:
          break;
        }
  }


  ArgParser &ArgParser::intOption(const std::string &name,
                               const std::string &help,
                               int value, bool obl)
  {
    ParData p;
    p.int_p=new int(value);
    p.self_delete=true;
    p.help=help;
    p.type=INTEGER;
    p.mandatory=obl;
    _opts[name]=p;
    return *this;
  }

  ArgParser &ArgParser::doubleOption(const std::string &name,
                               const std::string &help,
                               double value, bool obl)
  {
    ParData p;
    p.double_p=new double(value);
    p.self_delete=true;
    p.help=help;
    p.type=DOUBLE;
    p.mandatory=obl;
    _opts[name]=p;
    return *this;
  }

  ArgParser &ArgParser::boolOption(const std::string &name,
                               const std::string &help,
                               bool value, bool obl)
  {
    ParData p;
    p.bool_p=new bool(value);
    p.self_delete=true;
    p.help=help;
    p.type=BOOL;
    p.mandatory=obl;
    _opts[name]=p;
    return *this;
  }

  ArgParser &ArgParser::stringOption(const std::string &name,
                               const std::string &help,
                               std::string value, bool obl)
  {
    ParData p;
    p.string_p=new std::string(value);
    p.self_delete=true;
    p.help=help;
    p.type=STRING;
    p.mandatory=obl;
    _opts[name]=p;
    return *this;
  }

  ArgParser &ArgParser::refOption(const std::string &name,
                               const std::string &help,
                               int &ref, bool obl)
  {
    ParData p;
    p.int_p=&ref;
    p.self_delete=false;
    p.help=help;
    p.type=INTEGER;
    p.mandatory=obl;
    _opts[name]=p;
    return *this;
  }

  ArgParser &ArgParser::refOption(const std::string &name,
                                  const std::string &help,
                                  double &ref, bool obl)
  {
    ParData p;
    p.double_p=&ref;
    p.self_delete=false;
    p.help=help;
    p.type=DOUBLE;
    p.mandatory=obl;
    _opts[name]=p;
    return *this;
  }

  ArgParser &ArgParser::refOption(const std::string &name,
                                  const std::string &help,
                                  bool &ref, bool obl)
  {
    ParData p;
    p.bool_p=&ref;
    p.self_delete=false;
    p.help=help;
    p.type=BOOL;
    p.mandatory=obl;
    _opts[name]=p;

    ref = false;

    return *this;
  }

  ArgParser &ArgParser::refOption(const std::string &name,
                               const std::string &help,
                               std::string &ref, bool obl)
  {
    ParData p;
    p.string_p=&ref;
    p.self_delete=false;
    p.help=help;
    p.type=STRING;
    p.mandatory=obl;
    _opts[name]=p;
    return *this;
  }

  ArgParser &ArgParser::funcOption(const std::string &name,
                               const std::string &help,
                               void (*func)(void *),void *data)
  {
    ParData p;
    p.func_p.p=func;
    p.func_p.data=data;
    p.self_delete=false;
    p.help=help;
    p.type=FUNC;
    p.mandatory=false;
    _opts[name]=p;
    return *this;
  }

  ArgParser &ArgParser::optionGroup(const std::string &group,
                                    const std::string &opt)
  {
    Opts::iterator i = _opts.find(opt);
    LEMON_ASSERT(i!=_opts.end(), "Unknown option: '"+opt+"'");
    LEMON_ASSERT(!(i->second.ingroup),
                 "Option already in option group: '"+opt+"'");
    GroupData &g=_groups[group];
    g.opts.push_back(opt);
    i->second.ingroup=true;
    return *this;
  }

  ArgParser &ArgParser::onlyOneGroup(const std::string &group)
  {
    GroupData &g=_groups[group];
    g.only_one=true;
    return *this;
  }

  ArgParser &ArgParser::synonym(const std::string &syn,
                                const std::string &opt)
  {
    Opts::iterator o = _opts.find(opt);
    Opts::iterator s = _opts.find(syn);
    LEMON_ASSERT(o!=_opts.end(), "Unknown option: '"+opt+"'");
    LEMON_ASSERT(s==_opts.end(), "Option already used: '"+syn+"'");
    ParData p;
    p.help=opt;
    p.mandatory=false;
    p.syn=true;
    _opts[syn]=p;
    o->second.has_syn=true;
    return *this;
  }

  ArgParser &ArgParser::mandatoryGroup(const std::string &group)
  {
    GroupData &g=_groups[group];
    g.mandatory=true;
    return *this;
  }

  ArgParser &ArgParser::other(const std::string &name,
                              const std::string &help)
  {
    _others_help.push_back(OtherArg(name,help));
    return *this;
  }

  void ArgParser::show(std::ostream &os,Opts::const_iterator i) const
  {
    os << "-" << i->first;
    if(i->second.has_syn)
      for(Opts::const_iterator j=_opts.begin();j!=_opts.end();++j)
        if(j->second.syn&&j->second.help==i->first)
          os << "|-" << j->first;
    switch(i->second.type) {
    case STRING:
      os << " str";
      break;
    case INTEGER:
      os << " int";
      break;
    case DOUBLE:
      os << " num";
      break;
    default:
      break;
    }
  }

  void ArgParser::show(std::ostream &os,Groups::const_iterator i) const
  {
    GroupData::Opts::const_iterator o=i->second.opts.begin();
    while(o!=i->second.opts.end()) {
      show(os,_opts.find(*o));
      ++o;
      if(o!=i->second.opts.end()) os<<'|';
    }
  }

  void ArgParser::showHelp(Opts::const_iterator i) const
  {
    if(i->second.help.size()==0||i->second.syn) return;
    std::cerr << "  ";
    show(std::cerr,i);
    std::cerr << std::endl;
    std::cerr << "     " << i->second.help << std::endl;
  }
  void ArgParser::showHelp(std::vector<ArgParser::OtherArg>::const_iterator i)
    const
  {
    if(i->help.size()==0) return;
    std::cerr << "  " << i->name << std::endl
              << "     " << i->help << std::endl;
  }

  void ArgParser::shortHelp() const
  {
    const unsigned int LINE_LEN=77;
    const std::string indent("    ");
    std::cerr << "Usage:\n  " << _command_name;
    int pos=_command_name.size()+2;
    for(Groups::const_iterator g=_groups.begin();g!=_groups.end();++g) {
      std::ostringstream cstr;
      cstr << ' ';
      if(!g->second.mandatory) cstr << '[';
      show(cstr,g);
      if(!g->second.mandatory) cstr << ']';
      if(pos+cstr.str().size()>LINE_LEN) {
        std::cerr << std::endl << indent;
        pos=indent.size();
      }
      std::cerr << cstr.str();
      pos+=cstr.str().size();
    }
    for(Opts::const_iterator i=_opts.begin();i!=_opts.end();++i)
      if(!i->second.ingroup&&!i->second.syn) {
        std::ostringstream cstr;
        cstr << ' ';
        if(!i->second.mandatory) cstr << '[';
        show(cstr,i);
        if(!i->second.mandatory) cstr << ']';
        if(pos+cstr.str().size()>LINE_LEN) {
          std::cerr << std::endl << indent;
          pos=indent.size();
        }
        std::cerr << cstr.str();
        pos+=cstr.str().size();
      }
    for(std::vector<OtherArg>::const_iterator i=_others_help.begin();
        i!=_others_help.end();++i)
      {
        std::ostringstream cstr;
        cstr << ' ' << i->name;

        if(pos+cstr.str().size()>LINE_LEN) {
          std::cerr << std::endl << indent;
          pos=indent.size();
        }
        std::cerr << cstr.str();
        pos+=cstr.str().size();
      }
    std::cerr << std::endl;
  }

  void ArgParser::showHelp() const
  {
    shortHelp();
    std::cerr << "Where:\n";
    for(std::vector<OtherArg>::const_iterator i=_others_help.begin();
        i!=_others_help.end();++i) showHelp(i);
    for(Opts::const_iterator i=_opts.begin();i!=_opts.end();++i) showHelp(i);
    exit(1);
  }


  void ArgParser::unknownOpt(std::string arg) const
  {
    std::cerr << "\nUnknown option: " << arg << "\n";
    std::cerr << "\nType '" << _command_name <<
      " --help' to obtain a short summary on the usage.\n\n";
    exit(1);
  }

  void ArgParser::requiresValue(std::string arg, OptType t) const
  {
    std::cerr << "Argument '" << arg << "' requires a";
    switch(t) {
    case STRING:
      std::cerr << " string";
      break;
    case INTEGER:
      std::cerr << "n integer";
      break;
    case DOUBLE:
      std::cerr << " floating point";
      break;
    default:
      break;
    }
    std::cerr << " value\n\n";
    showHelp();
  }


  void ArgParser::checkMandatories() const
  {
    bool ok=true;
    for(Opts::const_iterator i=_opts.begin();i!=_opts.end();++i)
      if(i->second.mandatory&&!i->second.set)
        {
          if(ok)
            std::cerr << _command_name
                      << ": The following mandatory arguments are missing.\n";
          ok=false;
          showHelp(i);
        }
    for(Groups::const_iterator i=_groups.begin();i!=_groups.end();++i)
      if(i->second.mandatory||i->second.only_one)
        {
          int set=0;
          for(GroupData::Opts::const_iterator o=i->second.opts.begin();
              o!=i->second.opts.end();++o)
            if(_opts.find(*o)->second.set) ++set;
          if(i->second.mandatory&&!set) {
            std::cerr << _command_name <<
              ": At least one of the following arguments is mandatory.\n";
            ok=false;
            for(GroupData::Opts::const_iterator o=i->second.opts.begin();
                o!=i->second.opts.end();++o)
              showHelp(_opts.find(*o));
          }
          if(i->second.only_one&&set>1) {
            std::cerr << _command_name <<
              ": At most one of the following arguments can be given.\n";
            ok=false;
            for(GroupData::Opts::const_iterator o=i->second.opts.begin();
                o!=i->second.opts.end();++o)
              showHelp(_opts.find(*o));
          }
        }
    if(!ok) {
      std::cerr << "\nType '" << _command_name <<
        " --help' to obtain a short summary on the usage.\n\n";
      exit(1);
    }
  }

  ArgParser &ArgParser::parse()
  {
    for(int ar=1; ar<_argc; ++ar) {
      std::string arg(_argv[ar]);
      if (arg[0] != '-' || arg.size() == 1) {
        _file_args.push_back(arg);
      }
      else {
        Opts::iterator i = _opts.find(arg.substr(1));
        if(i==_opts.end()) unknownOpt(arg);
        else {
          if(i->second.syn) i=_opts.find(i->second.help);
          ParData &p(i->second);
          if (p.type==BOOL) *p.bool_p=true;
          else if (p.type==FUNC) p.func_p.p(p.func_p.data);
          else if(++ar==_argc) requiresValue(arg, p.type);
          else {
            std::string val(_argv[ar]);
            std::istringstream vals(val);
            switch(p.type) {
            case STRING:
              *p.string_p=val;
              break;
            case INTEGER:
              vals >> *p.int_p;
              break;
            case DOUBLE:
              vals >> *p.double_p;
              break;
            default:
              break;
            }
            if(p.type!=STRING&&(!vals||!vals.eof()))
              requiresValue(arg, p.type);
          }
          p.set = true;
        }
      }
    }
    checkMandatories();

    return *this;
  }

}