Learn how easy it is to sync an existing GitHub or Google Code repo to a SourceForge project! See Demo

Close

[b27671]: libopt++ / popt_options.cpp Maximize Restore History

Download this file

popt_options.cpp    328 lines (250 with data), 7.8 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
/**
* @file popt_options.cpp
* option parsing
*
* @remark Copyright 2002 OProfile authors
* @remark Read the file COPYING
*
* @author Philippe Elie
* @author John Levon
*/
#include <iostream>
#include "op_popt.h"
#include "op_version.h"
#include "popt_options.h"
#include "string_manip.h"
using namespace std;
namespace popt {
/**
* option_base - base class for implementation of a command line option
*
* Every command line option added before calling parse_options()
* is of this type.
*/
class option_base {
public:
/**
* option_base - construct an option with the given options.
* @param option_name name part of long form e.g. --option
* @param short_name short form name e.g. -o
* @param help_str short description of the option
* @param arg_help_str short description of the argument (if any)
* @param data a pointer to the data to fill in
* @param popt_flags the popt library data type
*/
option_base(char const * option_name, char short_name,
char const * help_str, char const * arg_help_str,
void * data, unsigned int popt_flags);
virtual ~option_base() {}
/**
* post_process - perform any necessary post-processing
*/
virtual void post_process() {}
protected:
char const * option_name;
};
/** the popt array singleton options */
static vector<poptOption> & popt_options(void)
{
static vector<poptOption> *x = new(vector<poptOption>);
return *x;
}
static vector<option_base *> & options_list(void)
{
static vector<option_base *> *x = new(vector<option_base *>);
return *x;
}
static int showvers;
static struct poptOption appended_options[] = {
{ "version", 'v', POPT_ARG_NONE, &showvers, 0, "show version", NULL, },
POPT_AUTOHELP
POPT_TABLEEND
};
/* options parameter can't be a local variable because caller can use the
* returned poptContext which contains pointer inside the options array */
static poptContext do_parse_options(int argc, char const ** argv,
vector<poptOption> & options,
vector<string> & additional_params)
{
options = popt_options();
int const nr_appended_options =
sizeof(appended_options) / sizeof(appended_options[0]);
options.insert(options.end(), appended_options,
appended_options + nr_appended_options);
poptContext con = op_poptGetContext(NULL, argc, argv, &options[0], 0);
if (showvers)
show_version(argv[0]);
char const * file;
while ((file = poptGetArg(con)) != 0)
additional_params.push_back(file);
for (size_t i = 0 ; i < options_list().size() ; ++i)
options_list()[i]->post_process();
return con;
}
void parse_options(int argc, char const ** argv,
vector<string> & additional_params)
{
vector<poptOption> options;
poptContext con =
do_parse_options(argc, argv, options, additional_params);
poptFreeContext(con);
}
template <typename T> class option_imp;
/**
* option<void> - a binary option
*
* Use this option type for constructing specified / not-specified
* options e.g. --frob
*/
template <> class option_imp<void> : public option_base {
public:
option_imp(bool & value, char const * option_name, char short_name,
char const * help_str);
~option_imp() {}
void post_process();
private:
bool & value;
int popt_value;
};
/**
* option<int> - a integer option
*
* Use this for options taking an integer e.g. --frob 6
*/
template <> class option_imp<int> : public option_base {
public:
option_imp(int & value, char const * option_name, char short_name,
char const * help_str, char const * arg_help_str);
~option_imp() {}
};
/**
* option<string> - a string option
*
* Use this for options taking a string e.g. --frob parsley
*/
template <> class option_imp<string> : public option_base {
public:
option_imp(string & value, char const * option_name,
char short_name, char const * help_str,
char const * arg_help_str);
void post_process();
~option_imp() {}
private:
// we need an intermediate char array to pass to popt libs
char * popt_value;
string & value;
};
/**
* option< vector<string> > - a string vector option
*
* Use this for options taking a number of string arguments,
* separated by the given separator.
*/
template <> class option_imp< vector<string> > : public option_base {
public:
option_imp(vector<string> & value,
char const * option_name, char short_name,
char const * help_str, char const * arg_help_str,
char separator = ',');
void post_process();
~option_imp() {}
private:
vector<string> & value;
// we need an intermediate char array to pass to popt libs
char * popt_value;
char const separator;
};
option::~option()
{
delete the_option;
}
/// non templatized ctor for boolean option
option::option(bool & value, char const * name, char short_name, char const * help)
: the_option(new option_imp<void>(value, name, short_name, help))
{
}
/// specialization of option ctor for integer option
template <>
option::option(int & value, char const * name, char short_name,
char const * help, char const * arg_help)
: the_option(new option_imp<int>
(value, name, short_name, help, arg_help))
{
}
/// specialization of option ctor for string option
template <>
option::option(string & value, char const * name, char short_name,
char const * help, char const * arg_help)
: the_option(new option_imp<string>
(value, name, short_name, help, arg_help))
{
}
/// specialization of option ctor for vector<string> option
template <>
option::option(vector<string> & value, char const * name, char short_name,
char const * help, char const * arg_help)
: the_option(new option_imp< vector<string> >
(value, name, short_name, help, arg_help))
{
}
option_base::option_base(char const * name, char short_name,
char const * help, char const * arg_help,
void * data, unsigned int popt_flags)
: option_name(name)
{
poptOption const opt = { name, short_name, popt_flags,
data, 0, help, arg_help };
popt_options().push_back(opt);
options_list().push_back(this);
}
option_imp<void>::option_imp(bool & val, char const * name, char short_name,
char const * help)
: option_base(name, short_name, help, 0, &popt_value, POPT_ARG_NONE),
value(val), popt_value(0)
{
}
void option_imp<void>::post_process()
{
if (popt_value) {
if (is_prefix(option_name, "no-"))
value = !popt_value;
else
value = popt_value;
}
}
option_imp<int>::option_imp(int & value, char const * name, char short_name,
char const * help, char const * arg_help)
: option_base(name, short_name, help, arg_help, &value, POPT_ARG_INT)
{
}
option_imp<string>::option_imp(string & val, char const * name, char short_name,
char const * help, char const * arg_help)
: option_base(name, short_name, help, arg_help,
&popt_value, POPT_ARG_STRING),
popt_value(0), value(val)
{
}
void option_imp<string>::post_process()
{
if (popt_value) {
value = popt_value;
popt_value = 0;
}
}
option_imp< vector<string> >::option_imp(vector<string> & val,
char const * name, char short_name,
char const * help,
char const * arg_help, char sepchar)
: option_base(name, short_name, help, arg_help,
&popt_value, POPT_ARG_STRING),
value(val), popt_value(0), separator(sepchar)
{
}
void option_imp< vector<string> >::post_process()
{
if (popt_value) {
value = separate_token(popt_value, separator);
popt_value = 0;
}
}
} // namespace popt