| 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; |
| ... | ... |
@@ -337,49 +337,49 @@ |
| 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)