gravatar
alpar (Alpar Juttner)
alpar@cs.elte.hu
Backout some rubbish from chageset 7cd965d2257f
0 2 0
default
2 files changed with 1 insertions and 2 deletions:
↑ Collapse diff ↑
Ignore white space 6 line context
1 1
/* -*- C++ -*-
2 2
 *
3 3
 * This file is a part of LEMON, a generic C++ optimization library
4 4
 *
5 5
 * Copyright (C) 2003-2008
6 6
 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
7 7
 * (Egervary Research Group on Combinatorial Optimization, EGRES).
8 8
 *
9 9
 * Permission to use, modify and distribute this software is granted
10 10
 * provided that this copyright notice appears in all copies. For
11 11
 * precise terms see the accompanying LICENSE file.
12 12
 *
13 13
 * This software is provided "AS IS" with no warranty of any kind,
14 14
 * express or implied, and with no claim as to its suitability for any
15 15
 * purpose.
16 16
 *
17 17
 */
18 18

	
19 19
///\ingroup demos
20 20
///\file
21 21
///\brief Argument parser demo
22 22
///
23 23
/// This example shows how the argument parser can be used.
24 24
///
25 25
/// \include arg_parser_demo.cc
26 26

	
27 27
#include <lemon/arg_parser.h>
28 28

	
29 29
using namespace lemon;
30 30
int main(int argc, const char **argv)
31 31
{
32 32
  ArgParser ap(argc,argv);
33 33
  int i;
34 34
  std::string s;
35 35
  double d;
36 36
  bool b,sil;
37 37
  bool g1,g2,g3;
38 38
  ap.refOption("n", "An integer input.", i, true)
39 39
    .refOption("val", "A double input.", d)
40 40
    .doubleOption("val2", "A double input.", d)
41 41
    .synonym("vals","val")
42 42
    .refOption("name", "A string input.", s)
43 43
    .refOption("f", "A switch.", b)
44 44
    .refOption("nohelp", "", sil)
45 45
    .refOption("gra","Choice A",g1)
46 46
    .refOption("grb","Choice B",g2)
47 47
    .refOption("grc","Choice C",g3)
48 48
    .optionGroup("gr","gra")
49
    .optionGroup("gr","grbkk")
49
    .optionGroup("gr","grb")
50 50
    .optionGroup("gr","grc")
51 51
    .mandatoryGroup("gr")
52 52
    .onlyOneGroup("gr")
53 53
    .other("infile","The input file.")
54 54
    .other("...");
55 55
  
56 56
  ap.parse();
57 57

	
58 58
  std::cout << "Parameters of '" << ap.commandName() << "':\n";
59 59

	
60 60
  if(ap.given("n")) std::cout << "  Value of -n: " << i << std::endl;
61 61
  if(ap.given("val")) std::cout << "  Value of -val: " << d << std::endl;
62 62
  if(ap.given("name")) std::cout << "  Value of -name: " << s << std::endl;
63 63
  if(ap.given("f")) std::cout << "  -f is given\n";
64 64
  if(ap.given("nohelp")) std::cout << "  Value of -nohelp: " << sil << std::endl;
65 65
  if(ap.given("gra")) std::cout << "  -gra is given\n";
66 66
  if(ap.given("grb")) std::cout << "  -grb is given\n";
67 67
  if(ap.given("grc")) std::cout << "  -grc is given\n";
68 68
                                     
69 69
  switch(ap.files().size()) {
70 70
  case 0:
71 71
    std::cout << "  No file argument was given.\n";
72 72
    break;
73 73
  case 1:
74 74
    std::cout << "  1 file argument was given. It is:\n";
75 75
    break;
76 76
  default:
77 77
    std::cout << "  "
78 78
	      << ap.files().size() << " file arguments were given. They are:\n";
79 79
  }
80 80
  for(unsigned int i=0;i<ap.files().size();++i)
81 81
    std::cout << "    '" << ap.files()[i] << "'\n";
82 82
  
83 83
}
Ignore white space 384 line context
... ...
@@ -5,385 +5,384 @@
5 5
 * Copyright (C) 2003-2008
6 6
 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
7 7
 * (Egervary Research Group on Combinatorial Optimization, EGRES).
8 8
 *
9 9
 * Permission to use, modify and distribute this software is granted
10 10
 * provided that this copyright notice appears in all copies. For
11 11
 * precise terms see the accompanying LICENSE file.
12 12
 *
13 13
 * This software is provided "AS IS" with no warranty of any kind,
14 14
 * express or implied, and with no claim as to its suitability for any
15 15
 * purpose.
16 16
 *
17 17
 */
18 18

	
19 19
#include <lemon/arg_parser.h>
20 20

	
21 21
namespace lemon {
22 22

	
23 23
  void ArgParser::_showHelp(void *p)
24 24
  {
25 25
    (static_cast<ArgParser*>(p))->showHelp();
26 26
    exit(1);
27 27
  }
28 28

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

	
35 35
  }
36 36

	
37 37
  ArgParser::~ArgParser()
38 38
  {
39 39
    for(Opts::iterator i=_opts.begin();i!=_opts.end();++i)
40 40
      if(i->second.self_delete)
41 41
	switch(i->second.type) {
42 42
	case BOOL:
43 43
	  delete i->second.bool_p;
44 44
	  break;
45 45
	case STRING:
46 46
	  delete i->second.string_p;
47 47
	  break;
48 48
	case DOUBLE:
49 49
	  delete i->second.double_p;
50 50
	  break;
51 51
	case INTEGER:
52 52
	  delete i->second.int_p;
53 53
	  break;
54 54
	case UNKNOWN:
55 55
	  break;
56 56
	case FUNC:
57 57
	  break;
58 58
	}
59 59
  }
60 60
  
61 61

	
62 62
  ArgParser &ArgParser::intOption(const std::string &name,
63 63
			       const std::string &help,
64 64
			       int value, bool obl)
65 65
  {
66 66
    ParData p;
67 67
    p.int_p=new int(value);
68 68
    p.self_delete=true;
69 69
    p.help=help;
70 70
    p.type=INTEGER;
71 71
    p.mandatory=obl;
72 72
    _opts[name]=p;
73 73
    return *this;
74 74
  }
75 75

	
76 76
  ArgParser &ArgParser::doubleOption(const std::string &name,
77 77
			       const std::string &help,
78 78
			       double value, bool obl)
79 79
  {
80 80
    ParData p;
81 81
    p.double_p=new double(value);
82 82
    p.self_delete=true;
83 83
    p.help=help;
84 84
    p.type=DOUBLE;
85 85
    p.mandatory=obl;
86 86
    _opts[name]=p;
87 87
    return *this;
88 88
  }
89 89

	
90 90
  ArgParser &ArgParser::boolOption(const std::string &name,
91 91
			       const std::string &help,
92 92
			       bool value, bool obl)
93 93
  {
94 94
    ParData p;
95 95
    p.bool_p=new bool(value);
96 96
    p.self_delete=true;
97 97
    p.help=help;
98 98
    p.type=BOOL;
99 99
    p.mandatory=obl;
100 100
    _opts[name]=p;
101 101
    return *this;
102 102
  }
103 103

	
104 104
  ArgParser &ArgParser::stringOption(const std::string &name,
105 105
			       const std::string &help,
106 106
			       std::string value, bool obl)
107 107
  {
108 108
    ParData p;
109 109
    p.string_p=new std::string(value);
110 110
    p.self_delete=true;
111 111
    p.help=help;
112 112
    p.type=STRING;
113 113
    p.mandatory=obl;
114 114
    _opts[name]=p;
115 115
    return *this;
116 116
  }
117 117

	
118 118
  ArgParser &ArgParser::refOption(const std::string &name,
119 119
			       const std::string &help,
120 120
			       int &ref, bool obl)
121 121
  {
122 122
    ParData p;
123 123
    p.int_p=&ref;
124 124
    p.self_delete=false;
125 125
    p.help=help;
126 126
    p.type=INTEGER;
127 127
    p.mandatory=obl;
128 128
    _opts[name]=p;
129 129
    return *this;
130 130
  }
131 131

	
132 132
  ArgParser &ArgParser::refOption(const std::string &name,
133 133
                                  const std::string &help,
134 134
                                  double &ref, bool obl)
135 135
  {
136 136
    ParData p;
137 137
    p.double_p=&ref;
138 138
    p.self_delete=false;
139 139
    p.help=help;
140 140
    p.type=DOUBLE;
141 141
    p.mandatory=obl;
142 142
    _opts[name]=p;
143 143
    return *this;
144 144
  }
145 145

	
146 146
  ArgParser &ArgParser::refOption(const std::string &name,
147 147
                                  const std::string &help,
148 148
                                  bool &ref, bool obl)
149 149
  {
150 150
    ParData p;
151 151
    p.bool_p=&ref;
152 152
    p.self_delete=false;
153 153
    p.help=help;
154 154
    p.type=BOOL;
155 155
    p.mandatory=obl;
156 156
    _opts[name]=p;
157 157

	
158 158
    ref = false;
159 159

	
160 160
    return *this;
161 161
  }
162 162

	
163 163
  ArgParser &ArgParser::refOption(const std::string &name,
164 164
			       const std::string &help,
165 165
			       std::string &ref, bool obl)
166 166
  {
167 167
    ParData p;
168 168
    p.string_p=&ref;
169 169
    p.self_delete=false;
170 170
    p.help=help;
171 171
    p.type=STRING;
172 172
    p.mandatory=obl;
173 173
    _opts[name]=p;
174 174
    return *this;
175 175
  }
176 176

	
177 177
  ArgParser &ArgParser::funcOption(const std::string &name,
178 178
			       const std::string &help,
179 179
			       void (*func)(void *),void *data)
180 180
  {
181 181
    ParData p;
182 182
    p.func_p.p=func;
183 183
    p.func_p.data=data;
184 184
    p.self_delete=false;
185 185
    p.help=help;
186 186
    p.type=FUNC;
187 187
    p.mandatory=false;
188 188
    _opts[name]=p;
189 189
    return *this;
190 190
  }
191 191

	
192 192
  ArgParser &ArgParser::optionGroup(const std::string &group,
193 193
				    const std::string &opt)
194 194
  {
195 195
    Opts::iterator i = _opts.find(opt);
196 196
    LEMON_ASSERT(i!=_opts.end(), "Unknown option: '"+opt+"'");
197
    if(i==_opts.end()) std::cout << "JAJJJJJJJJ\n";
198 197
    LEMON_ASSERT(!(i->second.ingroup), 
199 198
                 "Option already in option group: '"+opt+"'");
200 199
    GroupData &g=_groups[group];
201 200
    g.opts.push_back(opt);
202 201
    i->second.ingroup=true;
203 202
    return *this;
204 203
  }
205 204

	
206 205
  ArgParser &ArgParser::onlyOneGroup(const std::string &group)
207 206
  {
208 207
    GroupData &g=_groups[group];
209 208
    g.only_one=true;
210 209
    return *this;
211 210
  }
212 211

	
213 212
  ArgParser &ArgParser::synonym(const std::string &syn,
214 213
				const std::string &opt)
215 214
  {
216 215
    Opts::iterator o = _opts.find(opt);
217 216
    Opts::iterator s = _opts.find(syn);
218 217
    LEMON_ASSERT(o!=_opts.end(), "Unknown option: '"+opt+"'");
219 218
    LEMON_ASSERT(s==_opts.end(), "Option already used: '"+syn+"'");
220 219
    ParData p;
221 220
    p.help=opt;
222 221
    p.mandatory=false;
223 222
    p.syn=true;
224 223
    _opts[syn]=p;
225 224
    o->second.has_syn=true;
226 225
    return *this;
227 226
  }
228 227

	
229 228
  ArgParser &ArgParser::mandatoryGroup(const std::string &group)
230 229
  {
231 230
    GroupData &g=_groups[group];
232 231
    g.mandatory=true;
233 232
    return *this;
234 233
  }
235 234

	
236 235
  ArgParser &ArgParser::other(const std::string &name,
237 236
			      const std::string &help)
238 237
  {
239 238
    _others_help.push_back(OtherArg(name,help));
240 239
    return *this;
241 240
  }
242 241

	
243 242
  void ArgParser::show(std::ostream &os,Opts::iterator i)
244 243
  {
245 244
    os << "-" << i->first;
246 245
    if(i->second.has_syn)
247 246
      for(Opts::iterator j=_opts.begin();j!=_opts.end();++j)
248 247
	if(j->second.syn&&j->second.help==i->first)
249 248
	  os << "|-" << j->first;
250 249
    switch(i->second.type) {
251 250
    case STRING:
252 251
      os << " str";
253 252
      break;
254 253
    case INTEGER:
255 254
      os << " int";
256 255
      break;
257 256
    case DOUBLE:
258 257
      os << " num";
259 258
      break;
260 259
    default:
261 260
      break;
262 261
    }
263 262
  }
264 263

	
265 264
  void ArgParser::show(std::ostream &os,Groups::iterator i)
266 265
  {
267 266
    GroupData::Opts::iterator o=i->second.opts.begin();
268 267
    while(o!=i->second.opts.end()) {
269 268
      show(os,_opts.find(*o));
270 269
      ++o;
271 270
      if(o!=i->second.opts.end()) os<<'|';
272 271
    }
273 272
  }
274 273
    
275 274
  void ArgParser::showHelp(Opts::iterator i)
276 275
  {
277 276
    if(i->second.help.size()==0||i->second.syn) return;
278 277
    std::cerr << "  ";
279 278
    show(std::cerr,i);
280 279
    std::cerr << std::endl;
281 280
    std::cerr << "     " << i->second.help << std::endl;
282 281
  }
283 282
  void ArgParser::showHelp(std::vector<ArgParser::OtherArg>::iterator i)
284 283
  {
285 284
    if(i->help.size()==0) return;
286 285
    std::cerr << "  " << i->name << std::endl
287 286
	      << "     " << i->help << std::endl;
288 287
  }
289 288
    
290 289
  void ArgParser::shortHelp()
291 290
  {
292 291
    const unsigned int LINE_LEN=77;
293 292
    const std::string indent("    ");
294 293
    std::cerr << "Usage:\n  " << _command_name;
295 294
    int pos=_command_name.size()+2;
296 295
    for(Groups::iterator g=_groups.begin();g!=_groups.end();++g) {
297 296
      std::ostringstream cstr;
298 297
      cstr << ' ';
299 298
      if(!g->second.mandatory) cstr << '[';
300 299
      show(cstr,g);
301 300
      if(!g->second.mandatory) cstr << ']';
302 301
      if(pos+cstr.str().size()>LINE_LEN) {
303 302
	std::cerr << std::endl << indent;
304 303
	pos=indent.size();
305 304
      }
306 305
      std::cerr << cstr.str();
307 306
      pos+=cstr.str().size();
308 307
    }
309 308
    for(Opts::iterator i=_opts.begin();i!=_opts.end();++i)
310 309
      if(!i->second.ingroup&&!i->second.syn) {
311 310
	std::ostringstream cstr;
312 311
	cstr << ' ';
313 312
	if(!i->second.mandatory) cstr << '[';
314 313
	show(cstr,i);
315 314
	if(!i->second.mandatory) cstr << ']';
316 315
	if(pos+cstr.str().size()>LINE_LEN) {
317 316
	  std::cerr << std::endl << indent;
318 317
	  pos=indent.size();
319 318
	}
320 319
	std::cerr << cstr.str();
321 320
	pos+=cstr.str().size();
322 321
      }
323 322
    for(std::vector<OtherArg>::iterator i=_others_help.begin();
324 323
	i!=_others_help.end();++i)
325 324
      {
326 325
	std::ostringstream cstr;
327 326
	cstr << ' ' << i->name;
328 327
      
329 328
	if(pos+cstr.str().size()>LINE_LEN) {
330 329
	  std::cerr << std::endl << indent;
331 330
	  pos=indent.size();
332 331
	}
333 332
	std::cerr << cstr.str();
334 333
	pos+=cstr.str().size();
335 334
      }
336 335
    std::cerr << std::endl;
337 336
  }
338 337
    
339 338
  void ArgParser::showHelp()
340 339
  {
341 340
    shortHelp();
342 341
    std::cerr << "Where:\n";
343 342
    for(std::vector<OtherArg>::iterator i=_others_help.begin();
344 343
	i!=_others_help.end();++i) showHelp(i);
345 344
    for(Opts::iterator i=_opts.begin();i!=_opts.end();++i) showHelp(i);
346 345
    exit(1);
347 346
  }
348 347
    
349 348
      
350 349
  void ArgParser::unknownOpt(std::string arg) 
351 350
  {
352 351
    std::cerr << "\nUnknown option: " << arg << "\n";
353 352
    std::cerr << "\nType '" << _command_name <<
354 353
      " --help' to obtain a short summary on the usage.\n\n";
355 354
    exit(1);
356 355
  }
357 356
    
358 357
  void ArgParser::requiresValue(std::string arg, OptType t) 
359 358
  {
360 359
    std::cerr << "Argument '" << arg << "' requires a";
361 360
    switch(t) {
362 361
    case STRING:
363 362
      std::cerr << " string";
364 363
      break;
365 364
    case INTEGER:
366 365
      std::cerr << "n integer";
367 366
      break;
368 367
    case DOUBLE:
369 368
      std::cerr << " floating point";
370 369
      break;
371 370
    default:
372 371
      break;
373 372
    }
374 373
    std::cerr << " value\n\n";
375 374
    showHelp();
376 375
  }
377 376
    
378 377

	
379 378
  void ArgParser::checkMandatories()
380 379
  {
381 380
    bool ok=true;
382 381
    for(Opts::iterator i=_opts.begin();i!=_opts.end();++i)
383 382
      if(i->second.mandatory&&!i->second.set) 
384 383
	{
385 384
	  if(ok)
386 385
	    std::cerr << _command_name 
387 386
		      << ": The following mandatory arguments are missing.\n";
388 387
	  ok=false;
389 388
	  showHelp(i);
0 comments (0 inline)