| 1 | 1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
| 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 |
#ifndef LEMON_ARG_PARSER |
|
| 20 |
#define LEMON_ARG_PARSER |
|
| 19 |
#ifndef LEMON_ARG_PARSER_H |
|
| 20 |
#define LEMON_ARG_PARSER_H |
|
| 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/assert.h> |
| 30 | 30 |
|
| 31 | 31 |
///\ingroup misc |
| 32 | 32 |
///\file |
| 33 | 33 |
///\brief A tool to parse command line arguments. |
| 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, |
| ... | ... |
@@ -289,97 +289,97 @@ |
| 289 | 289 |
///Start the parsing process |
| 290 | 290 |
ArgParser &parse(); |
| 291 | 291 |
|
| 292 | 292 |
/// Synonym for parse() |
| 293 | 293 |
ArgParser &run() |
| 294 | 294 |
{
|
| 295 | 295 |
return parse(); |
| 296 | 296 |
} |
| 297 | 297 |
|
| 298 | 298 |
///Give back the command name (the 0th argument) |
| 299 | 299 |
const std::string &commandName() const { return _command_name; }
|
| 300 | 300 |
|
| 301 | 301 |
///Check if an opion has been given to the command. |
| 302 | 302 |
bool given(std::string op) const |
| 303 | 303 |
{
|
| 304 | 304 |
Opts::const_iterator i = _opts.find(op); |
| 305 | 305 |
return i!=_opts.end()?i->second.set:false; |
| 306 | 306 |
} |
| 307 | 307 |
|
| 308 | 308 |
|
| 309 | 309 |
///Magic type for operator[] |
| 310 | 310 |
|
| 311 | 311 |
///This is the type of the return value of ArgParser::operator[](). |
| 312 | 312 |
///It automatically converts to \c int, \c double, \c bool or |
| 313 | 313 |
///\c std::string if the type of the option matches, otherwise it |
| 314 | 314 |
///throws an exception (i.e. it performs runtime type checking). |
| 315 | 315 |
class RefType |
| 316 | 316 |
{
|
| 317 | 317 |
const ArgParser &_parser; |
| 318 | 318 |
std::string _name; |
| 319 | 319 |
public: |
| 320 | 320 |
///\e |
| 321 | 321 |
RefType(const ArgParser &p,const std::string &n) :_parser(p),_name(n) {}
|
| 322 | 322 |
///\e |
| 323 | 323 |
operator bool() |
| 324 | 324 |
{
|
| 325 | 325 |
Opts::const_iterator i = _parser._opts.find(_name); |
| 326 | 326 |
LEMON_ASSERT(i!=_parser._opts.end(), |
| 327 | 327 |
std::string()+"Unkown option: '"+_name+"'"); |
| 328 | 328 |
LEMON_ASSERT(i->second.type==ArgParser::BOOL, |
| 329 | 329 |
std::string()+"'"+_name+"' is a bool option"); |
| 330 | 330 |
return *(i->second.bool_p); |
| 331 | 331 |
} |
| 332 | 332 |
///\e |
| 333 | 333 |
operator std::string() |
| 334 | 334 |
{
|
| 335 | 335 |
Opts::const_iterator i = _parser._opts.find(_name); |
| 336 | 336 |
LEMON_ASSERT(i!=_parser._opts.end(), |
| 337 | 337 |
std::string()+"Unkown option: '"+_name+"'"); |
| 338 | 338 |
LEMON_ASSERT(i->second.type==ArgParser::STRING, |
| 339 | 339 |
std::string()+"'"+_name+"' is a string option"); |
| 340 | 340 |
return *(i->second.string_p); |
| 341 | 341 |
} |
| 342 | 342 |
///\e |
| 343 | 343 |
operator double() |
| 344 | 344 |
{
|
| 345 | 345 |
Opts::const_iterator i = _parser._opts.find(_name); |
| 346 | 346 |
LEMON_ASSERT(i!=_parser._opts.end(), |
| 347 | 347 |
std::string()+"Unkown option: '"+_name+"'"); |
| 348 | 348 |
LEMON_ASSERT(i->second.type==ArgParser::DOUBLE || |
| 349 | 349 |
i->second.type==ArgParser::INTEGER, |
| 350 | 350 |
std::string()+"'"+_name+"' is a floating point option"); |
| 351 | 351 |
return i->second.type==ArgParser::DOUBLE ? |
| 352 | 352 |
*(i->second.double_p) : *(i->second.int_p); |
| 353 | 353 |
} |
| 354 | 354 |
///\e |
| 355 | 355 |
operator int() |
| 356 | 356 |
{
|
| 357 | 357 |
Opts::const_iterator i = _parser._opts.find(_name); |
| 358 | 358 |
LEMON_ASSERT(i!=_parser._opts.end(), |
| 359 | 359 |
std::string()+"Unkown option: '"+_name+"'"); |
| 360 | 360 |
LEMON_ASSERT(i->second.type==ArgParser::INTEGER, |
| 361 | 361 |
std::string()+"'"+_name+"' is an integer option"); |
| 362 | 362 |
return *(i->second.int_p); |
| 363 | 363 |
} |
| 364 | 364 |
|
| 365 | 365 |
}; |
| 366 | 366 |
|
| 367 | 367 |
///Give back the value of an option |
| 368 | 368 |
|
| 369 | 369 |
///Give back the value of an option. |
| 370 | 370 |
///\sa RefType |
| 371 | 371 |
RefType operator[](const std::string &n) const |
| 372 | 372 |
{
|
| 373 | 373 |
return RefType(*this, n); |
| 374 | 374 |
} |
| 375 | 375 |
|
| 376 | 376 |
///Give back the non-option type arguments. |
| 377 | 377 |
|
| 378 | 378 |
///Give back a reference to a vector consisting of the program arguments |
| 379 | 379 |
///not starting with a '-' character. |
| 380 | 380 |
const std::vector<std::string> &files() const { return _file_args; }
|
| 381 | 381 |
|
| 382 | 382 |
}; |
| 383 | 383 |
} |
| 384 | 384 |
|
| 385 |
#endif // |
|
| 385 |
#endif // LEMON_ARG_PARSER_H |
0 comments (0 inline)