-
Notifications
You must be signed in to change notification settings - Fork 4
/
OptionParser.cc
66 lines (55 loc) · 2.06 KB
/
OptionParser.cc
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
#include <getopt.h>
#include <cstring>
#include <cstdlib>
#include "OptionParser.h"
#include "Options.h"
OptionParser::Option::Option(bool is_set_, int takes_arg_, const char *name_,
const char *def_val_, const char *descrip_,
const char *value_)
: is_set(is_set_), takes_arg(takes_arg_), name(name_), def_val(def_val_),
descrip(descrip_), value(value_) {}
OptionParser::OptionParser(int argc, char *argv[]) : options() {
size_t num_opts = sizeof(cmdline_options) / sizeof(struct _cmdline_options);
struct option opts[num_opts + 1];
// prepare structures for getopt()
std::memset(opts, 0, sizeof(opts));
for (size_t i = 0; i < num_opts; i++) {
opts[i].name = cmdline_options[i].name;
opts[i].has_arg = cmdline_options[i].takes_arg;
options.push_back(Option(
false, cmdline_options[i].takes_arg,
cmdline_options[i].name ? cmdline_options[i].name : "",
cmdline_options[i].def_val ? cmdline_options[i].def_val : "",
cmdline_options[i].descrip ? cmdline_options[i].descrip : "", ""));
}
for (int i; !getopt_long(argc, argv, "", opts, &i);) {
options[i].is_set = true;
if (optarg)
options[i].value = optarg;
}
}
OptionParser::~OptionParser() {}
const std::string &OptionParser::getString(const std::string &name) const {
const Option &o = find(name);
return (o.is_set ? o.value : o.def_val);
}
int OptionParser::getInt(const std::string &name) const {
const Option &o = find(name);
return std::atoi(o.is_set ? o.value.c_str() : o.def_val.c_str());
}
float OptionParser::getFloat(const std::string &name) const {
const Option &o = find(name);
return std::atof(o.is_set ? o.value.c_str() : o.def_val.c_str());
}
bool OptionParser::isset(const std::string &name) const {
const Option &o = find(name);
return o.is_set;
}
const OptionParser::Option &OptionParser::find(const std::string &name) const {
for (const auto &elem : options) {
if (elem.name == name)
return elem;
}
throw "Unkown option name.";
}
/* vim: set sw=2 sts=2 : */