-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathProgramOptions.h
300 lines (212 loc) · 6.54 KB
/
ProgramOptions.h
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
#ifndef COMMANDLINEPARSER_H_
#define COMMANDLINEPARSER_H_
#include <iostream>
#include <sstream>
#include <string>
#include <set>
#include <map>
#include <boost/filesystem.hpp>
#include <boost/lexical_cast.hpp>
#include <boost/parameter.hpp>
#include <boost/program_options.hpp>
#include <util/typename.h>
#include <util/exceptions.h>
namespace util {
// forward declarations
template <typename TargetType>
struct OptionConverter;
class program_option_impl;
struct ProgramOptionsCleanup;
// for sorting in the option set
struct is_less {
bool operator()(program_option_impl* left, program_option_impl* right);
};
BOOST_PARAMETER_NAME(module)
BOOST_PARAMETER_NAME(long_name)
BOOST_PARAMETER_NAME(short_name)
BOOST_PARAMETER_NAME(description_text)
BOOST_PARAMETER_NAME(argument_sketch)
BOOST_PARAMETER_NAME(default_value)
BOOST_PARAMETER_NAME(is_positional)
class ProgramOptions {
public:
/**
* Init ProgramOptions from command line arguments.
*/
static void init(int argc, char** argv, bool ignoreUnknown = false);
/**
* Init ProgramOptions from a config file.
*/
static void init(std::string configFile);
static bool isOptionSet(const program_option_impl& option);
static std::string getOptionValue(const program_option_impl& option);
static void setOptionValue(std::string longName, std::string value);
static void addProgramOption(program_option_impl* option);
static void printUsage();
private:
friend struct ProgramOptionsCleanup;
ProgramOptions() {};
static void init(int argc, char** argv, std::string configFileName, bool ignoreUnknown = false);
static void readFromFile(boost::filesystem::path configFile, boost::program_options::variables_map& values);
static std::set<program_option_impl*, is_less>* Options;
static std::set<std::string>* KnownLongParams;
static std::map<const program_option_impl*, std::string> Values;
static boost::program_options::options_description* CommandLineOptions;
static boost::program_options::positional_options_description* Positional;
static boost::program_options::options_description* ConfigFileOptions;
static std::string BinaryName;
};
class program_option_impl {
public:
template <class ArgumentPack>
program_option_impl(const ArgumentPack& args) :
_moduleName(args[_module | ""]),
_longParam(args[_long_name]),
_shortParam(args[_short_name | ""]),
_description(args[_description_text | ""]),
_argumentSketch(args[_argument_sketch | ""]),
_defaultValue(boost::lexical_cast<std::string>(args[_default_value | "0"])),
_isPositional(args[_is_positional | false]) {
ProgramOptions::addProgramOption(this);
}
std::string getModuleName() const;
std::string getLongParam() const;
std::string getShortParam() const;
std::string getDescription() const;
std::string getArgumentSketch() const;
std::string getDefaultValue() const;
bool isPositional() const;
/**
* Defines a lexicographic order on command line
* options.
*/
bool operator<(program_option_impl* right);
/**
* Implicit conversion to ValueType. This allows to convert the value of
* the program option into whatever type you expect. For instance, you
* can write for a ProgramOption fooOption
*
* int foo = fooOption;
*
* as long as the argument given to fooOption can be converted to int
* (otherwise, an exception will be thrown).
*
* This conversion also allows to check if an option is set. The implicit
* conversion to bool ensures:
*
* if (fooOption) {
*
* // fooOption is set to a value different to 'false'
*
* } else {
*
* // fooOption was not set or set to 'false'
* }
*/
template <typename ValueType>
operator ValueType() const {
OptionConverter<ValueType> converter;
return converter(*this);
}
/**
* Like type converter above, but to be used in boolean expressions.
*/
explicit operator bool() const {
// option is not set
if (!ProgramOptions::isOptionSet(*this)) {
try {
// is its default "true"?
if (boost::lexical_cast<bool>(ProgramOptions::getOptionValue(*this)))
return true;
} catch (boost::bad_lexical_cast&) {
UTIL_THROW_EXCEPTION(
UsageError,
"program option " << getLongParam() <<
" with value " << ProgramOptions::getOptionValue(*this) <<
" can not be (lexically) cast into bool");
}
// otherwise, we say false
return false;
}
if (ProgramOptions::getOptionValue(*this) == "false")
return false;
return true;
}
/**
* Explicit conversion to ValueType.
*/
template <typename ValueType>
ValueType as() {
OptionConverter<ValueType> converter;
return converter(*this);
}
private:
std::string _moduleName;
std::string _longParam;
std::string _shortParam;
std::string _description;
std::string _argumentSketch;
std::string _defaultValue;
bool _isPositional;
};
class ProgramOption : public program_option_impl {
public:
BOOST_PARAMETER_CONSTRUCTOR(
ProgramOption,
(program_option_impl),
tag,
(required
(long_name, *))
(optional
(short_name, *)
(module, *)
(description_text, *)
(argument_sketch, *)))
};
/**
* Default type conversion for options. Tries to cast the value string to the
* desired type.
*/
template <typename TargetType>
struct OptionConverter {
TargetType operator()(const program_option_impl& option) const {
try {
return boost::lexical_cast<TargetType>(ProgramOptions::getOptionValue(option));
} catch (boost::bad_lexical_cast&) {
UTIL_THROW_EXCEPTION(
UsageError,
"program option " << option.getLongParam() <<
" with value " << ProgramOptions::getOptionValue(option) <<
" can not be (lexically) cast into " << typeName(TargetType()));
}
}
};
/**
* Template specialisation for conversions to bool. Returns true, iff the
* option is set and its value is not 'false'.
*/
template <>
struct OptionConverter<bool> {
bool operator()(const program_option_impl& option) const {
return static_cast<bool>(option);
}
};
struct ProgramOptionsCleanup {
~ProgramOptionsCleanup() {
if (ProgramOptions::Options != 0)
delete ProgramOptions::Options;
if (ProgramOptions::KnownLongParams != 0)
delete ProgramOptions::KnownLongParams;
if (ProgramOptions::CommandLineOptions != 0)
delete ProgramOptions::CommandLineOptions;
if (ProgramOptions::Positional != 0)
delete ProgramOptions::Positional;
if (ProgramOptions::ConfigFileOptions != 0)
delete ProgramOptions::ConfigFileOptions;
}
};
extern ProgramOptionsCleanup programOptionsCleanup;
std::ostream&
operator<<(std::ostream& os, program_option_impl& option);
} // namespace util
#endif /*COMMANDLINEPARSER_H_*/