0
3
0
... | ... |
@@ -17,54 +17,57 @@ |
17 | 17 |
*/ |
18 | 18 |
|
19 | 19 |
///\ingroup demos |
20 | 20 |
///\file |
21 | 21 |
///\brief Argument parser demo |
22 | 22 |
/// |
23 |
/// This example shows how |
|
23 |
/// This example shows how the argument parser can be used. |
|
24 | 24 |
/// |
25 |
/// \include |
|
25 |
/// \include arg_parser_demo.cc |
|
26 | 26 |
|
27 | 27 |
#include <lemon/arg_parser.h> |
28 | 28 |
|
29 | 29 |
using namespace lemon; |
30 | 30 |
int main(int argc, const char **argv) |
31 | 31 |
{ |
32 | 32 |
ArgParser ap(argc,argv); |
33 | 33 |
int i; |
34 | 34 |
std::string s; |
35 | 35 |
double d; |
36 | 36 |
bool b,sil; |
37 | 37 |
bool g1,g2,g3; |
38 |
ap.refOption("n", "an integer input", i, true) |
|
39 |
.refOption("val", "a double input", d) |
|
38 |
ap.refOption("n", "An integer input.", i, true) |
|
39 |
.refOption("val", "A double input.", d) |
|
40 | 40 |
.synonym("vals","val") |
41 |
.refOption("name", "a string input", s) |
|
42 |
.refOption("f", "a switch", b) |
|
41 |
.refOption("name", "A string input.", s) |
|
42 |
.refOption("f", "A switch.", b) |
|
43 | 43 |
.refOption("nohelp", "", sil) |
44 |
.refOption("gra","Choise A",g1) |
|
45 |
.refOption("grb","Choise B",g2) |
|
46 |
.refOption(" |
|
44 |
.refOption("gra","Choice A",g1) |
|
45 |
.refOption("grb","Choice B",g2) |
|
46 |
.refOption("grc","Choice C",g3) |
|
47 | 47 |
.optionGroup("gr","gra") |
48 | 48 |
.optionGroup("gr","grb") |
49 | 49 |
.optionGroup("gr","grc") |
50 | 50 |
.mandatoryGroup("gr") |
51 | 51 |
.onlyOneGroup("gr") |
52 |
.other("infile","The input file") |
|
52 |
.other("infile","The input file.") |
|
53 | 53 |
.other("..."); |
54 | 54 |
|
55 | 55 |
ap.parse(); |
56 | 56 |
|
57 | 57 |
std::cout << "Parameters of '" << ap.commandName() << "':\n"; |
58 | 58 |
|
59 | 59 |
if(ap.given("n")) std::cout << " Value of -n: " << i << std::endl; |
60 | 60 |
if(ap.given("val")) std::cout << " Value of -val: " << d << std::endl; |
61 | 61 |
if(ap.given("name")) std::cout << " Value of -name: " << s << std::endl; |
62 | 62 |
if(ap.given("f")) std::cout << " -f is given\n"; |
63 | 63 |
if(ap.given("nohelp")) std::cout << " Value of -nohelp: " << sil << std::endl; |
64 |
|
|
64 |
if(ap.given("gra")) std::cout << " -gra is given\n"; |
|
65 |
if(ap.given("grb")) std::cout << " -grb is given\n"; |
|
66 |
if(ap.given("grc")) std::cout << " -grc is given\n"; |
|
67 |
|
|
65 | 68 |
switch(ap.files().size()) { |
66 | 69 |
case 0: |
67 | 70 |
std::cout << " No file argument was given.\n"; |
68 | 71 |
break; |
69 | 72 |
case 1: |
70 | 73 |
std::cout << " 1 file argument was given. It is:\n"; |
... | ... |
@@ -66,13 +66,12 @@ |
66 | 66 |
ParData p; |
67 | 67 |
p.int_p=new int(value); |
68 | 68 |
p.self_delete=true; |
69 | 69 |
p.help=help; |
70 | 70 |
p.type=INTEGER; |
71 | 71 |
p.mandatory=obl; |
72 |
p.self_delete=true; |
|
73 | 72 |
_opts[name]=p; |
74 | 73 |
return *this; |
75 | 74 |
} |
76 | 75 |
|
77 | 76 |
ArgParser &ArgParser::doubleOption(const std::string &name, |
78 | 77 |
const std::string &help, |
... | ... |
@@ -96,15 +95,12 @@ |
96 | 95 |
p.bool_p=new bool(value); |
97 | 96 |
p.self_delete=true; |
98 | 97 |
p.help=help; |
99 | 98 |
p.type=BOOL; |
100 | 99 |
p.mandatory=obl; |
101 | 100 |
_opts[name]=p; |
102 |
|
|
103 |
value = false; |
|
104 |
|
|
105 | 101 |
return *this; |
106 | 102 |
} |
107 | 103 |
|
108 | 104 |
ArgParser &ArgParser::stringOption(const std::string &name, |
109 | 105 |
const std::string &help, |
110 | 106 |
std::string value, bool obl) |
... | ... |
@@ -27,23 +27,22 @@ |
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 |
///\ |
|
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; |
... | ... |
@@ -162,13 +161,13 @@ |
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 |
|
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 |
... | ... |
@@ -219,15 +218,15 @@ |
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 |
/// |
|
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 |
... | ... |
@@ -239,30 +238,30 @@ |
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 |
|
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 |
/// |
|
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 |
/// |
|
259 |
///Give back the non-option type arguments. |
|
261 | 260 |
|
262 |
/// |
|
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 |
|
... | ... |
@@ -295,15 +294,15 @@ |
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 |
/// |
|
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 |
... | ... |
@@ -352,13 +351,13 @@ |
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 |
|
0 comments (0 inline)