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
|
#pragma once
#include <iostream>
#include <cassert>
#include <functional>
#include <vector>
#include <deque>
#include <unordered_map>
#include <string>
#include <cstring>
#include <getopt.h>
namespace dg {
using Handle = std::function<int()>;
class Options {
public:
/// @param name name of the option
/// @param has_arg kind of arguments that are used (no_argument, required_argument, optional_argument)
/// @param val identifies the option (see getopt documentation)
/// @param help_text help text for this option
/// @param handle lambda that is invoked when the option occures
void add(std::string const & name, int has_arg, int val, std::string const & help_text, Handle handle) {
add(name, has_arg, nullptr, val, help_text, handle);
}
/// @param name name of the option
/// @param has_arg kind of arguments that are used (no_argument, required_argument, optional_argument)
/// @param flag pointer that is set after the option occured
/// @param val value for the flag to be set
/// @param help_text help text for this option
/// @param handle lambda that is invoked when the option occures
void add(std::string const & name, int has_arg, int* flag, int val, std::string const & help_text, Handle handle) {
names.push_back(name);
// create a new option from the args
options.push_back({names.back().data(), has_arg, flag, val});
help_texts.push_back(help_text);
int index = val; // let val be the option's unique identifier
if (flag != nullptr) {
// flag is not null, so the val is not used as identifier
// so pick another one
index = num_flags++;
}
// store the handle
handles[index] = handle;
}
int process(int argc, char* argv[]) {
int ret = 0;
std::string shortopts;
for (auto const & option: options) {
if (option.flag != nullptr) {
continue;
}
shortopts += static_cast<char>(option.val);
switch (option.has_arg) {
case no_argument:
break;
case required_argument:
shortopts += ":";
break;
case optional_argument:
shortopts += "::";
break;
}
}
// add termination option
options.push_back({0, 0, 0, 0});
// handle arguments
while (true) {
int index = 0;
int key = getopt_long(argc, argv, shortopts.data(), options.data(), &index);
if (key == -1) {
break;
} else if (key == '?') {
return 1;
} else if (key == ':') {
return 1;
} else if (key == 0) {
// call flag's handle
ret = handles.at(index)();
} else {
// call option's handle
ret = handles.at(key)();
}
if(ret) {
return ret;
}
}
for (int i = optind; i < argc; ++i) {
args.push_back(argv[i]);
}
// remove terminating option
options.pop_back();
assert(options.size() == handles.size());
assert(options.size() == help_texts.size());
return 0;
}
const std::vector<std::string> arguments() const { return args; }
void help() {
std::size_t width = 0;
for(auto const & opt : options) {
if(opt.name == nullptr) {
continue; // skip terminating option
}
width = std::max(width, std::strlen(opt.name) + (opt.has_arg != no_argument ? 0 : 3));
}
width += 1;
int index = 0;
for(auto const & help_text : help_texts) {
if(index >= (int)options.size()) {
break;
}
auto const & opt = options.at(index);
std::string args;
switch(opt.has_arg) {
case required_argument:
args = "<x>";
break;
case optional_argument:
args = "[x]";
break;
case no_argument:
default:
break;
}
std::string padding;
std::size_t pad_size = width - (std::strlen(opt.name) + args.size());
padding.append(pad_size, ' ');
if(opt.val >= 33 && opt.val <= 126)
{
std::cout << " -" << static_cast<char>(opt.val) << ", --" << opt.name << " " << args << padding;
}
else
{
std::cout << " --" << opt.name << " " << args << padding;
}
std::cout << help_text << std::endl;
++index;
}
}
private:
std::size_t num_flags{};
std::vector<option> options{};
std::deque<std::string> names{};
std::vector<std::string> help_texts{};
std::vector<std::string> args;
std::unordered_map<int, Handle> handles{};
};
}
|