0
3
0
| ... | ... |
@@ -6,49 +6,49 @@ |
| 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 |
int main(int argc, |
|
| 30 |
int main(int argc, char **argv) |
|
| 31 | 31 |
{
|
| 32 | 32 |
// Initialize the argument parser |
| 33 | 33 |
ArgParser ap(argc, argv); |
| 34 | 34 |
int i; |
| 35 | 35 |
std::string s; |
| 36 | 36 |
double d = 1.0; |
| 37 | 37 |
bool b, nh; |
| 38 | 38 |
bool g1, g2, g3; |
| 39 | 39 |
|
| 40 | 40 |
// Add a mandatory integer option with storage reference |
| 41 | 41 |
ap.refOption("n", "An integer input.", i, true);
|
| 42 | 42 |
// Add a double option with storage reference (the default value is 1.0) |
| 43 | 43 |
ap.refOption("val", "A double input.", d);
|
| 44 | 44 |
// Add a double option without storage reference (the default value is 3.14) |
| 45 | 45 |
ap.doubleOption("val2", "A double input.", 3.14);
|
| 46 | 46 |
// Set synonym for -val option |
| 47 | 47 |
ap.synonym("vals", "val");
|
| 48 | 48 |
// Add a string option |
| 49 | 49 |
ap.refOption("name", "A string input.", s);
|
| 50 | 50 |
// Add bool options |
| 51 | 51 |
ap.refOption("f", "A switch.", b)
|
| 52 | 52 |
.refOption("nohelp", "", nh)
|
| 53 | 53 |
.refOption("gra", "Choice A", g1)
|
| 54 | 54 |
.refOption("grb", "Choice B", g2)
|
| ... | ... |
@@ -5,54 +5,53 @@ |
| 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 |
ArgParser::ArgParser(int argc, const char **argv) :_argc(argc), _argv(argv), |
|
| 30 |
_command_name(argv[0]) {
|
|
| 29 |
ArgParser::ArgParser(int argc, const char * const *argv) |
|
| 30 |
:_argc(argc), _argv(argv), _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 |
|
|
| 35 | 34 |
} |
| 36 | 35 |
|
| 37 | 36 |
ArgParser::~ArgParser() |
| 38 | 37 |
{
|
| 39 | 38 |
for(Opts::iterator i=_opts.begin();i!=_opts.end();++i) |
| 40 | 39 |
if(i->second.self_delete) |
| 41 | 40 |
switch(i->second.type) {
|
| 42 | 41 |
case BOOL: |
| 43 | 42 |
delete i->second.bool_p; |
| 44 | 43 |
break; |
| 45 | 44 |
case STRING: |
| 46 | 45 |
delete i->second.string_p; |
| 47 | 46 |
break; |
| 48 | 47 |
case DOUBLE: |
| 49 | 48 |
delete i->second.double_p; |
| 50 | 49 |
break; |
| 51 | 50 |
case INTEGER: |
| 52 | 51 |
delete i->second.int_p; |
| 53 | 52 |
break; |
| 54 | 53 |
case UNKNOWN: |
| 55 | 54 |
break; |
| 56 | 55 |
case FUNC: |
| 57 | 56 |
break; |
| 58 | 57 |
} |
| ... | ... |
@@ -25,49 +25,49 @@ |
| 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 |
const char **_argv; |
|
| 49 |
const char * const *_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; |
| ... | ... |
@@ -98,49 +98,49 @@ |
| 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 |
///Constructor |
| 122 |
ArgParser(int argc, const char **argv); |
|
| 122 |
ArgParser(int argc, const char * const *argv); |
|
| 123 | 123 |
|
| 124 | 124 |
~ArgParser(); |
| 125 | 125 |
|
| 126 | 126 |
///\name Options |
| 127 | 127 |
/// |
| 128 | 128 |
|
| 129 | 129 |
///@{
|
| 130 | 130 |
|
| 131 | 131 |
///Add a new integer type option |
| 132 | 132 |
|
| 133 | 133 |
///Add a new integer type option. |
| 134 | 134 |
///\param name The name of the option. The leading '-' must be omitted. |
| 135 | 135 |
///\param help A help string. |
| 136 | 136 |
///\param value A default value for the option. |
| 137 | 137 |
///\param obl Indicate if the option is mandatory. |
| 138 | 138 |
ArgParser &intOption(const std::string &name, |
| 139 | 139 |
const std::string &help, |
| 140 | 140 |
int value=0, bool obl=false); |
| 141 | 141 |
|
| 142 | 142 |
///Add a new floating point type option |
| 143 | 143 |
|
| 144 | 144 |
///Add a new floating point type option. |
| 145 | 145 |
///\param name The name of the option. The leading '-' must be omitted. |
| 146 | 146 |
///\param help A help string. |
0 comments (0 inline)