| ... |
... |
@@ -9,59 +9,58 @@
|
| 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 |
#ifndef LEMON_ARG_PARSER
|
| 20 |
20 |
#define LEMON_ARG_PARSER
|
| 21 |
21 |
|
| 22 |
22 |
#include <vector>
|
| 23 |
23 |
#include <map>
|
| 24 |
24 |
#include <list>
|
| 25 |
25 |
#include <string>
|
| 26 |
26 |
#include <iostream>
|
| 27 |
27 |
#include <sstream>
|
| 28 |
28 |
#include <algorithm>
|
| 29 |
29 |
#include <lemon/error.h>
|
| 30 |
30 |
|
| 31 |
31 |
///\ingroup misc
|
| 32 |
32 |
///\file
|
| 33 |
|
///\brief A tools to parse command line arguments.
|
| 34 |
|
///
|
| 35 |
|
///\author Alpar Juttner
|
|
33 |
///\brief A tool to parse command line arguments.
|
| 36 |
34 |
|
| 37 |
35 |
namespace lemon {
|
| 38 |
36 |
|
| 39 |
37 |
///Command line arguments parser
|
| 40 |
38 |
|
| 41 |
39 |
///\ingroup misc
|
| 42 |
|
///Command line arguments parser
|
|
40 |
///Command line arguments parser.
|
| 43 |
41 |
///
|
|
42 |
///For a complete example see the \ref arg_parser_demo.cc demo file.
|
| 44 |
43 |
class ArgParser {
|
| 45 |
44 |
|
| 46 |
45 |
static void _showHelp(void *p);
|
| 47 |
46 |
protected:
|
| 48 |
47 |
|
| 49 |
48 |
int _argc;
|
| 50 |
49 |
const char **_argv;
|
| 51 |
50 |
|
| 52 |
51 |
enum OptType { UNKNOWN=0, BOOL=1, STRING=2, DOUBLE=3, INTEGER=4, FUNC=5 };
|
| 53 |
52 |
|
| 54 |
53 |
class ParData {
|
| 55 |
54 |
public:
|
| 56 |
55 |
union {
|
| 57 |
56 |
bool *bool_p;
|
| 58 |
57 |
int *int_p;
|
| 59 |
58 |
double *double_p;
|
| 60 |
59 |
std::string *string_p;
|
| 61 |
60 |
struct {
|
| 62 |
61 |
void (*p)(void *);
|
| 63 |
62 |
void *data;
|
| 64 |
63 |
} func_p;
|
| 65 |
64 |
|
| 66 |
65 |
};
|
| 67 |
66 |
std::string help;
|
| ... |
... |
@@ -144,49 +143,49 @@
|
| 144 |
143 |
const std::string &help,
|
| 145 |
144 |
double value=0, bool obl=false);
|
| 146 |
145 |
|
| 147 |
146 |
///Add a new bool type option
|
| 148 |
147 |
|
| 149 |
148 |
///\param name The name of the option. The leading '-' must be omitted.
|
| 150 |
149 |
///\param help A help string.
|
| 151 |
150 |
///\retval value The value of the argument will be written to this variable.
|
| 152 |
151 |
///\param obl Indicate if the option is mandatory.
|
| 153 |
152 |
////\note A mandatory bool obtion is of very little use.)
|
| 154 |
153 |
ArgParser &boolOption(const std::string &name,
|
| 155 |
154 |
const std::string &help,
|
| 156 |
155 |
bool value=false, bool obl=false);
|
| 157 |
156 |
|
| 158 |
157 |
///Add a new string type option
|
| 159 |
158 |
|
| 160 |
159 |
///\param name The name of the option. The leading '-' must be omitted.
|
| 161 |
160 |
///\param help A help string.
|
| 162 |
161 |
///\retval value The value of the argument will be written to this variable.
|
| 163 |
162 |
///\param obl Indicate if the option is mandatory.
|
| 164 |
163 |
ArgParser &stringOption(const std::string &name,
|
| 165 |
164 |
const std::string &help,
|
| 166 |
165 |
std::string value="", bool obl=false);
|
| 167 |
166 |
|
| 168 |
|
///\name Options with an external strorage.
|
|
167 |
///\name Options with external storage
|
| 169 |
168 |
///Using this functions, the value of the option will be directly written
|
| 170 |
169 |
///into a variable once the option appears in the command line.
|
| 171 |
170 |
|
| 172 |
171 |
///@{
|
| 173 |
172 |
|
| 174 |
173 |
///Add a new integer type option with a storage reference
|
| 175 |
174 |
|
| 176 |
175 |
///\param name The name of the option. The leading '-' must be omitted.
|
| 177 |
176 |
///\param help A help string.
|
| 178 |
177 |
///\retval ref The value of the argument will be written to this variable.
|
| 179 |
178 |
///\param obl Indicate if the option is mandatory.
|
| 180 |
179 |
ArgParser &refOption(const std::string &name,
|
| 181 |
180 |
const std::string &help,
|
| 182 |
181 |
int &ref, bool obl=false);
|
| 183 |
182 |
|
| 184 |
183 |
///Add a new floating type option with a storage reference
|
| 185 |
184 |
|
| 186 |
185 |
///\param name The name of the option. The leading '-' must be omitted.
|
| 187 |
186 |
///\param help A help string.
|
| 188 |
187 |
///\retval ref The value of the argument will be written to this variable.
|
| 189 |
188 |
///\param obl Indicate if the option is mandatory.
|
| 190 |
189 |
ArgParser &refOption(const std::string &name,
|
| 191 |
190 |
const std::string &help,
|
| 192 |
191 |
double &ref, bool obl=false);
|
| ... |
... |
@@ -201,127 +200,127 @@
|
| 201 |
200 |
ArgParser &refOption(const std::string &name,
|
| 202 |
201 |
const std::string &help,
|
| 203 |
202 |
bool &ref, bool obl=false);
|
| 204 |
203 |
|
| 205 |
204 |
///Add a new string type option with a storage reference
|
| 206 |
205 |
|
| 207 |
206 |
///\param name The name of the option. The leading '-' must be omitted.
|
| 208 |
207 |
///\param help A help string.
|
| 209 |
208 |
///\retval ref The value of the argument will be written to this variable.
|
| 210 |
209 |
///\param obl Indicate if the option is mandatory.
|
| 211 |
210 |
ArgParser &refOption(const std::string &name,
|
| 212 |
211 |
const std::string &help,
|
| 213 |
212 |
std::string &ref, bool obl=false);
|
| 214 |
213 |
|
| 215 |
214 |
///@}
|
| 216 |
215 |
|
| 217 |
216 |
///\name Option Groups and Synonyms
|
| 218 |
217 |
///
|
| 219 |
218 |
|
| 220 |
219 |
///@{
|
| 221 |
220 |
|
| 222 |
221 |
///Boundle some options into a group
|
| 223 |
222 |
|
| 224 |
223 |
/// You can group some option by calling this function repeatedly for each
|
| 225 |
|
/// option to be grupped with the same groupname.
|
| 226 |
|
///\param group The group name
|
| 227 |
|
///\param opt The option name
|
|
224 |
/// option to be grouped with the same groupname.
|
|
225 |
///\param group The group name.
|
|
226 |
///\param opt The option name.
|
| 228 |
227 |
ArgParser &optionGroup(const std::string &group,
|
| 229 |
228 |
const std::string &opt);
|
| 230 |
229 |
|
| 231 |
230 |
///Make the members of a group exclusive
|
| 232 |
231 |
|
| 233 |
232 |
///If you call this function for a group, than at most one of them can be
|
| 234 |
233 |
///given at the same time
|
| 235 |
234 |
ArgParser &onlyOneGroup(const std::string &group);
|
| 236 |
235 |
|
| 237 |
236 |
///Make a group mandatory
|
| 238 |
237 |
|
| 239 |
238 |
///Using this function, at least one of the members of \c group
|
| 240 |
239 |
///must be given.
|
| 241 |
240 |
ArgParser &mandatoryGroup(const std::string &group);
|
| 242 |
241 |
|
| 243 |
242 |
///Create synonym to an option
|
| 244 |
243 |
|
| 245 |
|
///With this function you can create a sysnonym called \c sys of the
|
|
244 |
///With this function you can create a synonym \c syn of the
|
| 246 |
245 |
///option \c opt.
|
| 247 |
246 |
ArgParser &synonym(const std::string &syn,
|
| 248 |
247 |
const std::string &opt);
|
| 249 |
248 |
|
| 250 |
249 |
///@}
|
| 251 |
250 |
|
| 252 |
251 |
///Give help string for non-parsed arguments.
|
| 253 |
252 |
|
| 254 |
253 |
///With this function you can give help string for non-parsed arguments.
|
| 255 |
|
///the parameter \c name will be printed in the short usage line, while
|
|
254 |
///The parameter \c name will be printed in the short usage line, while
|
| 256 |
255 |
///\c help gives a more detailed description.
|
| 257 |
256 |
ArgParser &other(const std::string &name,
|
| 258 |
257 |
const std::string &help="");
|
| 259 |
258 |
|
| 260 |
|
///Non option type arguments.
|
|
259 |
///Give back the non-option type arguments.
|
| 261 |
260 |
|
| 262 |
|
///Gives back a reference to a vector consisting of the program arguments
|
|
261 |
///Give back a reference to a vector consisting of the program arguments
|
| 263 |
262 |
///not starting with a '-' character.
|
| 264 |
263 |
std::vector<std::string> &files() { return _file_args; }
|
| 265 |
264 |
|
| 266 |
265 |
///Give back the command name (the 0th argument)
|
| 267 |
266 |
const std::string &commandName() { return _command_name; }
|
| 268 |
267 |
|
| 269 |
268 |
void show(std::ostream &os,Opts::iterator i);
|
| 270 |
269 |
void show(std::ostream &os,Groups::iterator i);
|
| 271 |
270 |
void showHelp(Opts::iterator i);
|
| 272 |
271 |
void showHelp(std::vector<OtherArg>::iterator i);
|
| 273 |
272 |
void shortHelp();
|
| 274 |
273 |
void showHelp();
|
| 275 |
274 |
|
| 276 |
275 |
void unknownOpt(std::string arg);
|
| 277 |
276 |
|
| 278 |
277 |
void requiresValue(std::string arg, OptType t);
|
| 279 |
278 |
void checkMandatories();
|
| 280 |
279 |
|
| 281 |
280 |
///Start the parsing process
|
| 282 |
281 |
ArgParser &parse();
|
| 283 |
282 |
|
| 284 |
283 |
/// Synonym for parse()
|
| 285 |
284 |
ArgParser &run()
|
| 286 |
285 |
{
|
| 287 |
286 |
return parse();
|
| 288 |
287 |
}
|
| 289 |
288 |
|
| 290 |
289 |
///Check if an opion has been given to the command.
|
| 291 |
290 |
bool given(std::string op)
|
| 292 |
291 |
{
|
| 293 |
292 |
Opts::iterator i = _opts.find(op);
|
| 294 |
293 |
return i!=_opts.end()?i->second.set:false;
|
| 295 |
294 |
}
|
| 296 |
295 |
|
| 297 |
296 |
|
| 298 |
297 |
///Magic type for operator[]
|
| 299 |
298 |
|
| 300 |
299 |
///This is the type of the return value of ArgParser::operator[]().
|
| 301 |
|
///It automatically converts to int, double, bool or std::string if
|
| 302 |
|
///the type of the option matches, otherwise it throws an exception.
|
| 303 |
|
///(i.e. it performs runtime type checking).
|
|
300 |
///It automatically converts to \c int, \c double, \c bool or
|
|
301 |
///\c std::string if the type of the option matches, otherwise it
|
|
302 |
///throws an exception (i.e. it performs runtime type checking).
|
| 304 |
303 |
class RefType
|
| 305 |
304 |
{
|
| 306 |
305 |
ArgParser &_parser;
|
| 307 |
306 |
std::string _name;
|
| 308 |
307 |
public:
|
| 309 |
308 |
///\e
|
| 310 |
309 |
RefType(ArgParser &p,const std::string &n) :_parser(p),_name(n) {}
|
| 311 |
310 |
///\e
|
| 312 |
311 |
operator bool()
|
| 313 |
312 |
{
|
| 314 |
313 |
Opts::iterator i = _parser._opts.find(_name);
|
| 315 |
314 |
LEMON_ASSERT(i==_parser._opts.end(),
|
| 316 |
315 |
std::string()+"Unkown option: '"+_name+"'");
|
| 317 |
316 |
LEMON_ASSERT(i->second.type!=ArgParser::BOOL,
|
| 318 |
317 |
std::string()+"'"+_name+"' is a bool option");
|
| 319 |
318 |
return *(i->second.bool_p);
|
| 320 |
319 |
}
|
| 321 |
320 |
///\e
|
| 322 |
321 |
operator std::string()
|
| 323 |
322 |
{
|
| 324 |
323 |
Opts::iterator i = _parser._opts.find(_name);
|
| 325 |
324 |
LEMON_ASSERT(i==_parser._opts.end(),
|
| 326 |
325 |
std::string()+"Unkown option: '"+_name+"'");
|
| 327 |
326 |
LEMON_ASSERT(i->second.type!=ArgParser::STRING,
|
| ... |
... |
@@ -334,37 +333,37 @@
|
| 334 |
333 |
Opts::iterator i = _parser._opts.find(_name);
|
| 335 |
334 |
LEMON_ASSERT(i==_parser._opts.end(),
|
| 336 |
335 |
std::string()+"Unkown option: '"+_name+"'");
|
| 337 |
336 |
LEMON_ASSERT(i->second.type!=ArgParser::DOUBLE &&
|
| 338 |
337 |
i->second.type!=ArgParser::INTEGER,
|
| 339 |
338 |
std::string()+"'"+_name+"' is a floating point option");
|
| 340 |
339 |
return i->second.type==ArgParser::DOUBLE ?
|
| 341 |
340 |
*(i->second.double_p) : *(i->second.int_p);
|
| 342 |
341 |
}
|
| 343 |
342 |
///\e
|
| 344 |
343 |
operator int()
|
| 345 |
344 |
{
|
| 346 |
345 |
Opts::iterator i = _parser._opts.find(_name);
|
| 347 |
346 |
LEMON_ASSERT(i==_parser._opts.end(),
|
| 348 |
347 |
std::string()+"Unkown option: '"+_name+"'");
|
| 349 |
348 |
LEMON_ASSERT(i->second.type!=ArgParser::INTEGER,
|
| 350 |
349 |
std::string()+"'"+_name+"' is an integer option");
|
| 351 |
350 |
return *(i->second.int_p);
|
| 352 |
351 |
}
|
| 353 |
352 |
|
| 354 |
353 |
};
|
| 355 |
354 |
|
| 356 |
355 |
///Give back the value of an option
|
| 357 |
356 |
|
| 358 |
|
///Give back the value of an option
|
|
357 |
///Give back the value of an option.
|
| 359 |
358 |
///\sa RefType
|
| 360 |
359 |
RefType operator[](const std::string &n)
|
| 361 |
360 |
{
|
| 362 |
361 |
return RefType(*this, n);
|
| 363 |
362 |
}
|
| 364 |
363 |
|
| 365 |
364 |
};
|
| 366 |
365 |
}
|
| 367 |
366 |
|
| 368 |
367 |
|
| 369 |
368 |
|
| 370 |
369 |
#endif // LEMON_MAIN_PARAMS
|