gravatar
kpeter (Peter Kovacs)
kpeter@inf.elte.hu
Bug fix in arg_parser.h (fix ticket #31) and doc improvements
0 1 0
default
1 file changed with 16 insertions and 16 deletions:
↑ Collapse diff ↑
Ignore white space 192 line context
... ...
@@ -34,336 +34,336 @@
34 34

	
35 35
namespace lemon {
36 36

	
37 37
  ///Command line arguments parser
38 38

	
39 39
  ///\ingroup misc
40 40
  ///Command line arguments parser.
41 41
  ///
42 42
  ///For a complete example see the \ref arg_parser_demo.cc demo file.
43 43
  class ArgParser {
44 44
    
45 45
    static void _showHelp(void *p);
46 46
  protected:
47 47
    
48 48
    int _argc;
49 49
    const char **_argv;
50 50
    
51 51
    enum OptType { UNKNOWN=0, BOOL=1, STRING=2, DOUBLE=3, INTEGER=4, FUNC=5 };
52 52
    
53 53
    class ParData {
54 54
    public:
55 55
      union {
56 56
	bool *bool_p;
57 57
	int *int_p;
58 58
	double *double_p;
59 59
	std::string *string_p;
60 60
	struct {
61 61
	  void (*p)(void *);
62 62
	  void *data;
63 63
	} func_p;
64 64
	  
65 65
      };
66 66
      std::string help;
67 67
      bool mandatory;
68 68
      OptType type;
69 69
      bool set;
70 70
      bool ingroup;
71 71
      bool has_syn;
72 72
      bool syn;
73 73
      bool self_delete;
74 74
      ParData() : mandatory(false), type(UNKNOWN), set(false), ingroup(false),
75 75
		  has_syn(false), syn(false), self_delete(false) {}
76 76
    };
77 77

	
78 78
    typedef std::map<std::string,ParData> Opts;
79 79
    Opts _opts;
80 80

	
81 81
    class GroupData 
82 82
    {
83 83
    public:
84 84
      typedef std::list<std::string> Opts;
85 85
      Opts opts;
86 86
      bool only_one;
87 87
      bool mandatory;
88 88
      GroupData() :only_one(false), mandatory(false) {}
89 89
    };
90 90
      
91 91
    typedef std::map<std::string,GroupData> Groups;
92 92
    Groups _groups;
93 93

	
94 94
    struct OtherArg
95 95
    {
96 96
      std::string name;
97 97
      std::string help;
98 98
      OtherArg(std::string n, std::string h) :name(n), help(h) {}
99 99

	
100 100
    };
101 101
      
102 102
    std::vector<OtherArg> _others_help;
103 103
    std::vector<std::string> _file_args;
104 104
    std::string _command_name;
105 105

	
106 106
    
107 107
  private:
108 108
    //Bind a function to an option.
109 109

	
110 110
    //\param name The name of the option. The leading '-' must be omitted.
111 111
    //\param help A help string.
112 112
    //\retval func The function to be called when the option is given. It
113 113
    //  must be of type "void f(void *)"
114 114
    //\param data Data to be passed to \c func
115 115
    ArgParser &funcOption(const std::string &name,
116 116
		    const std::string &help,
117 117
		    void (*func)(void *),void *data);
118 118
    
119 119
  public:
120 120

	
121 121
    ///\e
122 122
    ArgParser(int argc, const char **argv);
123 123

	
124 124
    ~ArgParser();
125 125

	
126 126
    ///Add a new integer type option
127 127

	
128 128
    ///\param name The name of the option. The leading '-' must be omitted.
129 129
    ///\param help A help string.
130
    ///\param value A default value for the option
130
    ///\param value A default value for the option.
131 131
    ///\param obl Indicate if the option is mandatory.
132 132
    ArgParser &intOption(const std::string &name,
133 133
		    const std::string &help,
134 134
		    int value=0, bool obl=false);
135 135

	
136 136
    ///Add a new floating point type option
137 137

	
138 138
    ///\param name The name of the option. The leading '-' must be omitted.
139 139
    ///\param help A help string.
140
    ///\param value A default value for the option
140
    ///\param value A default value for the option.
141 141
    ///\param obl Indicate if the option is mandatory.
142 142
    ArgParser &doubleOption(const std::string &name,
143 143
		      const std::string &help,
144 144
		      double value=0, bool obl=false);
145 145

	
146 146
    ///Add a new bool type option
147 147

	
148 148
    ///\param name The name of the option. The leading '-' must be omitted.
149 149
    ///\param help A help string.
150
    ///\param value A default value for the option
150
    ///\param value A default value for the option.
151 151
    ///\param obl Indicate if the option is mandatory.
152
    ////\note A mandatory bool obtion is of very little use.)
152
    ///\note A mandatory bool obtion is of very little use.
153 153
    ArgParser &boolOption(const std::string &name,
154 154
		      const std::string &help,
155 155
		      bool value=false, bool obl=false);
156 156

	
157 157
    ///Add a new string type option
158 158

	
159 159
    ///\param name The name of the option. The leading '-' must be omitted.
160 160
    ///\param help A help string.
161
    ///\param value A default value for the option
161
    ///\param value A default value for the option.
162 162
    ///\param obl Indicate if the option is mandatory.
163 163
    ArgParser &stringOption(const std::string &name,
164 164
		      const std::string &help,
165 165
		      std::string value="", bool obl=false);
166 166

	
167 167
    ///\name Options with external storage
168 168
    ///Using this functions, the value of the option will be directly written
169 169
    ///into a variable once the option appears in the command line.
170 170

	
171 171
    ///@{
172 172

	
173 173
    ///Add a new integer type option with a storage reference
174 174

	
175 175
    ///\param name The name of the option. The leading '-' must be omitted.
176 176
    ///\param help A help string.
177 177
    ///\param obl Indicate if the option is mandatory.
178 178
    ///\retval ref The value of the argument will be written to this variable.
179 179
    ArgParser &refOption(const std::string &name,
180 180
		    const std::string &help,
181 181
		    int &ref, bool obl=false);
182 182

	
183 183
    ///Add a new floating type option with a storage reference
184 184

	
185 185
    ///\param name The name of the option. The leading '-' must be omitted.
186 186
    ///\param help A help string.
187 187
    ///\param obl Indicate if the option is mandatory.
188 188
    ///\retval ref The value of the argument will be written to this variable.
189 189
    ArgParser &refOption(const std::string &name,
190 190
		      const std::string &help,
191 191
		      double &ref, bool obl=false);
192 192

	
193 193
    ///Add a new bool type option with a storage reference
194 194

	
195 195
    ///\param name The name of the option. The leading '-' must be omitted.
196 196
    ///\param help A help string.
197 197
    ///\param obl Indicate if the option is mandatory.
198 198
    ///\retval ref The value of the argument will be written to this variable.
199
    ////\note A mandatory bool obtion is of very little use.)
199
    ///\note A mandatory bool obtion is of very little use.
200 200
    ArgParser &refOption(const std::string &name,
201 201
		      const std::string &help,
202 202
		      bool &ref, bool obl=false);
203 203

	
204 204
    ///Add a new string type option with a storage reference
205 205

	
206 206
    ///\param name The name of the option. The leading '-' must be omitted.
207 207
    ///\param help A help string.
208
    ///\param obl Indicate if the option is mandatory.
208 209
    ///\retval ref The value of the argument will be written to this variable.
209
    ///\param obl Indicate if the option is mandatory.
210 210
    ArgParser &refOption(const std::string &name,
211 211
		      const std::string &help,
212 212
		      std::string &ref, bool obl=false);
213 213
    
214 214
    ///@}
215 215

	
216 216
    ///\name Option Groups and Synonyms
217 217
    ///
218 218
    
219 219
    ///@{
220 220

	
221 221
    ///Boundle some options into a group
222 222

	
223 223
    /// You can group some option by calling this function repeatedly for each
224 224
    /// option to be grouped with the same groupname.
225 225
    ///\param group The group name.
226 226
    ///\param opt The option name.
227 227
    ArgParser &optionGroup(const std::string &group,
228 228
			   const std::string &opt);
229 229

	
230 230
    ///Make the members of a group exclusive
231 231

	
232 232
    ///If you call this function for a group, than at most one of them can be
233 233
    ///given at the same time
234 234
    ArgParser &onlyOneGroup(const std::string &group);
235 235
  
236 236
    ///Make a group mandatory
237 237

	
238 238
    ///Using this function, at least one of the members of \c group
239 239
    ///must be given.
240 240
    ArgParser &mandatoryGroup(const std::string &group);
241 241
    
242 242
    ///Create synonym to an option
243 243

	
244 244
    ///With this function you can create a synonym \c syn of the
245 245
    ///option \c opt.
246 246
    ArgParser &synonym(const std::string &syn,
247 247
			   const std::string &opt);
248 248
    
249 249
    ///@}
250 250

	
251 251
    ///Give help string for non-parsed arguments.
252 252

	
253 253
    ///With this function you can give help string for non-parsed arguments.
254 254
    ///The parameter \c name will be printed in the short usage line, while
255 255
    ///\c help gives a more detailed description.
256 256
    ArgParser &other(const std::string &name,
257 257
		     const std::string &help="");
258 258
    
259 259
    ///Give back the non-option type arguments.
260 260

	
261 261
    ///Give back a reference to a vector consisting of the program arguments
262 262
    ///not starting with a '-' character.
263 263
    std::vector<std::string> &files() { return _file_args; }
264 264

	
265 265
    ///Give back the command name (the 0th argument)
266 266
    const std::string &commandName() { return _command_name; }
267 267

	
268 268
    void show(std::ostream &os,Opts::iterator i);
269 269
    void show(std::ostream &os,Groups::iterator i);
270 270
    void showHelp(Opts::iterator i);
271 271
    void showHelp(std::vector<OtherArg>::iterator i);
272 272
    void shortHelp();
273 273
    void showHelp();
274 274

	
275 275
    void unknownOpt(std::string arg);
276 276

	
277 277
    void requiresValue(std::string arg, OptType t);
278 278
    void checkMandatories();
279 279
    
280 280
    ///Start the parsing process
281 281
    ArgParser &parse();
282 282

	
283 283
    /// Synonym for parse()
284 284
    ArgParser &run() 
285 285
    {
286 286
      return parse();
287 287
    }
288 288
    
289 289
    ///Check if an opion has been given to the command.
290 290
    bool given(std::string op) 
291 291
    {
292 292
      Opts::iterator i = _opts.find(op);
293 293
      return i!=_opts.end()?i->second.set:false;
294 294
    }
295 295

	
296 296

	
297 297
    ///Magic type for operator[]
298 298
    
299 299
    ///This is the type of the return value of ArgParser::operator[]().
300 300
    ///It automatically converts to \c int, \c double, \c bool or
301 301
    ///\c std::string if the type of the option matches, otherwise it
302 302
    ///throws an exception (i.e. it performs runtime type checking).
303 303
    class RefType 
304 304
    {
305 305
      ArgParser &_parser;
306 306
      std::string _name;
307 307
    public:
308 308
      ///\e
309 309
      RefType(ArgParser &p,const std::string &n) :_parser(p),_name(n) {}
310 310
      ///\e
311 311
      operator bool() 
312 312
      {
313 313
	Opts::iterator i = _parser._opts.find(_name);
314
	LEMON_ASSERT(i==_parser._opts.end(),
314
	LEMON_ASSERT(i!=_parser._opts.end(),
315 315
		     std::string()+"Unkown option: '"+_name+"'");
316
	LEMON_ASSERT(i->second.type!=ArgParser::BOOL,
316
	LEMON_ASSERT(i->second.type==ArgParser::BOOL,
317 317
		     std::string()+"'"+_name+"' is a bool option");
318 318
	return *(i->second.bool_p);
319 319
      }
320 320
      ///\e
321 321
      operator std::string()
322 322
      {
323 323
	Opts::iterator i = _parser._opts.find(_name);
324
	LEMON_ASSERT(i==_parser._opts.end(),
324
	LEMON_ASSERT(i!=_parser._opts.end(),
325 325
		     std::string()+"Unkown option: '"+_name+"'");
326
	LEMON_ASSERT(i->second.type!=ArgParser::STRING,
326
	LEMON_ASSERT(i->second.type==ArgParser::STRING,
327 327
		     std::string()+"'"+_name+"' is a string option");
328 328
	return *(i->second.string_p);
329 329
      }
330 330
      ///\e
331 331
      operator double() 
332 332
      {
333 333
	Opts::iterator i = _parser._opts.find(_name);
334
	LEMON_ASSERT(i==_parser._opts.end(),
334
	LEMON_ASSERT(i!=_parser._opts.end(),
335 335
		     std::string()+"Unkown option: '"+_name+"'");
336
	LEMON_ASSERT(i->second.type!=ArgParser::DOUBLE &&
337
		     i->second.type!=ArgParser::INTEGER,
336
	LEMON_ASSERT(i->second.type==ArgParser::DOUBLE ||
337
		     i->second.type==ArgParser::INTEGER,
338 338
		     std::string()+"'"+_name+"' is a floating point option");
339 339
	return i->second.type==ArgParser::DOUBLE ?
340 340
	  *(i->second.double_p) : *(i->second.int_p);
341 341
      }
342 342
      ///\e
343 343
      operator int() 
344 344
      {
345 345
	Opts::iterator i = _parser._opts.find(_name);
346
	LEMON_ASSERT(i==_parser._opts.end(),
346
	LEMON_ASSERT(i!=_parser._opts.end(),
347 347
		     std::string()+"Unkown option: '"+_name+"'");
348
	LEMON_ASSERT(i->second.type!=ArgParser::INTEGER,
348
	LEMON_ASSERT(i->second.type==ArgParser::INTEGER,
349 349
		     std::string()+"'"+_name+"' is an integer option");
350 350
	return *(i->second.int_p);
351 351
      }
352 352

	
353 353
    };
354 354

	
355 355
    ///Give back the value of an option
356 356
    
357 357
    ///Give back the value of an option.
358 358
    ///\sa RefType
359 359
    RefType operator[](const std::string &n)
360 360
    {
361 361
      return RefType(*this, n);
362 362
    }    
363 363
 
364 364
  };
365 365
}
366 366

	
367 367
    
368 368

	
369 369
#endif // LEMON_MAIN_PARAMS
0 comments (0 inline)