0
2
0
1 | 1 |
/* -*- C++ -*- |
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 | 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 | 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 | 38 |
ap.refOption("n", "An integer input.", i, true) |
39 | 39 |
.refOption("val", "A double input.", d) |
40 | 40 |
.doubleOption("val2", "A double input.", d) |
41 | 41 |
.synonym("vals","val") |
42 | 42 |
.refOption("name", "A string input.", s) |
43 | 43 |
.refOption("f", "A switch.", b) |
44 | 44 |
.refOption("nohelp", "", sil) |
45 | 45 |
.refOption("gra","Choice A",g1) |
46 | 46 |
.refOption("grb","Choice B",g2) |
47 | 47 |
.refOption("grc","Choice C",g3) |
48 | 48 |
.optionGroup("gr","gra") |
49 |
.optionGroup("gr"," |
|
49 |
.optionGroup("gr","grb") |
|
50 | 50 |
.optionGroup("gr","grc") |
51 | 51 |
.mandatoryGroup("gr") |
52 | 52 |
.onlyOneGroup("gr") |
53 | 53 |
.other("infile","The input file.") |
54 | 54 |
.other("..."); |
55 | 55 |
|
56 | 56 |
ap.parse(); |
57 | 57 |
|
58 | 58 |
std::cout << "Parameters of '" << ap.commandName() << "':\n"; |
59 | 59 |
|
60 | 60 |
if(ap.given("n")) std::cout << " Value of -n: " << i << std::endl; |
61 | 61 |
if(ap.given("val")) std::cout << " Value of -val: " << d << std::endl; |
62 | 62 |
if(ap.given("name")) std::cout << " Value of -name: " << s << std::endl; |
63 | 63 |
if(ap.given("f")) std::cout << " -f is given\n"; |
64 | 64 |
if(ap.given("nohelp")) std::cout << " Value of -nohelp: " << sil << std::endl; |
65 | 65 |
if(ap.given("gra")) std::cout << " -gra is given\n"; |
66 | 66 |
if(ap.given("grb")) std::cout << " -grb is given\n"; |
67 | 67 |
if(ap.given("grc")) std::cout << " -grc is given\n"; |
68 | 68 |
|
69 | 69 |
switch(ap.files().size()) { |
70 | 70 |
case 0: |
71 | 71 |
std::cout << " No file argument was given.\n"; |
72 | 72 |
break; |
73 | 73 |
case 1: |
74 | 74 |
std::cout << " 1 file argument was given. It is:\n"; |
75 | 75 |
break; |
76 | 76 |
default: |
77 | 77 |
std::cout << " " |
78 | 78 |
<< ap.files().size() << " file arguments were given. They are:\n"; |
79 | 79 |
} |
80 | 80 |
for(unsigned int i=0;i<ap.files().size();++i) |
81 | 81 |
std::cout << " '" << ap.files()[i] << "'\n"; |
82 | 82 |
|
83 | 83 |
} |
... | ... |
@@ -149,97 +149,96 @@ |
149 | 149 |
{ |
150 | 150 |
ParData p; |
151 | 151 |
p.bool_p=&ref; |
152 | 152 |
p.self_delete=false; |
153 | 153 |
p.help=help; |
154 | 154 |
p.type=BOOL; |
155 | 155 |
p.mandatory=obl; |
156 | 156 |
_opts[name]=p; |
157 | 157 |
|
158 | 158 |
ref = false; |
159 | 159 |
|
160 | 160 |
return *this; |
161 | 161 |
} |
162 | 162 |
|
163 | 163 |
ArgParser &ArgParser::refOption(const std::string &name, |
164 | 164 |
const std::string &help, |
165 | 165 |
std::string &ref, bool obl) |
166 | 166 |
{ |
167 | 167 |
ParData p; |
168 | 168 |
p.string_p=&ref; |
169 | 169 |
p.self_delete=false; |
170 | 170 |
p.help=help; |
171 | 171 |
p.type=STRING; |
172 | 172 |
p.mandatory=obl; |
173 | 173 |
_opts[name]=p; |
174 | 174 |
return *this; |
175 | 175 |
} |
176 | 176 |
|
177 | 177 |
ArgParser &ArgParser::funcOption(const std::string &name, |
178 | 178 |
const std::string &help, |
179 | 179 |
void (*func)(void *),void *data) |
180 | 180 |
{ |
181 | 181 |
ParData p; |
182 | 182 |
p.func_p.p=func; |
183 | 183 |
p.func_p.data=data; |
184 | 184 |
p.self_delete=false; |
185 | 185 |
p.help=help; |
186 | 186 |
p.type=FUNC; |
187 | 187 |
p.mandatory=false; |
188 | 188 |
_opts[name]=p; |
189 | 189 |
return *this; |
190 | 190 |
} |
191 | 191 |
|
192 | 192 |
ArgParser &ArgParser::optionGroup(const std::string &group, |
193 | 193 |
const std::string &opt) |
194 | 194 |
{ |
195 | 195 |
Opts::iterator i = _opts.find(opt); |
196 | 196 |
LEMON_ASSERT(i!=_opts.end(), "Unknown option: '"+opt+"'"); |
197 |
if(i==_opts.end()) std::cout << "JAJJJJJJJJ\n"; |
|
198 | 197 |
LEMON_ASSERT(!(i->second.ingroup), |
199 | 198 |
"Option already in option group: '"+opt+"'"); |
200 | 199 |
GroupData &g=_groups[group]; |
201 | 200 |
g.opts.push_back(opt); |
202 | 201 |
i->second.ingroup=true; |
203 | 202 |
return *this; |
204 | 203 |
} |
205 | 204 |
|
206 | 205 |
ArgParser &ArgParser::onlyOneGroup(const std::string &group) |
207 | 206 |
{ |
208 | 207 |
GroupData &g=_groups[group]; |
209 | 208 |
g.only_one=true; |
210 | 209 |
return *this; |
211 | 210 |
} |
212 | 211 |
|
213 | 212 |
ArgParser &ArgParser::synonym(const std::string &syn, |
214 | 213 |
const std::string &opt) |
215 | 214 |
{ |
216 | 215 |
Opts::iterator o = _opts.find(opt); |
217 | 216 |
Opts::iterator s = _opts.find(syn); |
218 | 217 |
LEMON_ASSERT(o!=_opts.end(), "Unknown option: '"+opt+"'"); |
219 | 218 |
LEMON_ASSERT(s==_opts.end(), "Option already used: '"+syn+"'"); |
220 | 219 |
ParData p; |
221 | 220 |
p.help=opt; |
222 | 221 |
p.mandatory=false; |
223 | 222 |
p.syn=true; |
224 | 223 |
_opts[syn]=p; |
225 | 224 |
o->second.has_syn=true; |
226 | 225 |
return *this; |
227 | 226 |
} |
228 | 227 |
|
229 | 228 |
ArgParser &ArgParser::mandatoryGroup(const std::string &group) |
230 | 229 |
{ |
231 | 230 |
GroupData &g=_groups[group]; |
232 | 231 |
g.mandatory=true; |
233 | 232 |
return *this; |
234 | 233 |
} |
235 | 234 |
|
236 | 235 |
ArgParser &ArgParser::other(const std::string &name, |
237 | 236 |
const std::string &help) |
238 | 237 |
{ |
239 | 238 |
_others_help.push_back(OtherArg(name,help)); |
240 | 239 |
return *this; |
241 | 240 |
} |
242 | 241 |
|
243 | 242 |
void ArgParser::show(std::ostream &os,Opts::iterator i) |
244 | 243 |
{ |
245 | 244 |
os << "-" << i->first; |
0 comments (0 inline)