-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathargh.h
278 lines (240 loc) · 7.32 KB
/
argh.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
#pragma once
#include <algorithm>
#include <fstream>
#include <iomanip>
#include <sstream>
#include <string>
#include <vector>
class Option {
public:
Option() : parsed(false), required(false) {}
virtual ~Option() {};
virtual std::string getDefault() = 0;
virtual std::string getMessage() = 0;
virtual std::string getName() = 0;
virtual void setValue(std::string const& val) = 0;
bool getParsed() { return parsed; }
bool getRequired() { return required; }
virtual void setParsed(bool parsed) { this->parsed = parsed; }
protected:
bool parsed, required;
};
template<typename T>
class OptionImpl : public Option {
public:
OptionImpl(T& var, T default_val, std::string const& name, bool required, std::string const& msg) : var(var)
{
this->default_val = default_val;
this->name = name;
this->required = required;
this->msg = msg;
this->var = default_val;
}
virtual std::string getDefault() { std::stringstream ss; ss << default_val; return ss.str(); }
std::string getName() { return name; }
std::string getMessage() { return msg; }
virtual void setValue(std::string const& val) { std::stringstream ss(val); ss >> var; }
protected:
T default_val;
T& var;
std::string name;
std::string msg;
};
class OptionStringImpl : public OptionImpl<std::string>
{
public:
OptionStringImpl(std::string& var, std::string const& default_val, std::string const& name, bool required, std::string const& msg) :
OptionImpl(var, default_val, name, required, msg)
{}
std::string getDefault() { std::stringstream ss; ss << "\"" << default_val << "\""; return ss.str(); }
void setValue(std::string const& val) { var = val; }
};
template<typename T>
class MultiOptionImpl : public Option
{
public:
MultiOptionImpl(std::vector<T>& var, std::string const& default_vals, std::string const& name, bool required, std::string const& msg, char delim) : var(var)
{
this->default_vals = default_vals;
this->name = name;
this->required = required;
this->msg = msg;
this->delim = delim;
setValue(default_vals);
}
std::string getDefault()
{
std::stringstream ss;
ss << "\"";
ss << default_vals;
ss << "\"";
return ss.str();
}
std::string getName() { return name; }
std::string getMessage() { return msg; }
virtual void setValue(std::string const& val) {
var.clear();
std::stringstream ss(val);
T elem;
for (std::string val_str; std::getline(ss, val_str, delim);) {
std::stringstream st(val_str);
st >> elem;
var.push_back(elem);
}
}
protected:
std::string default_vals;
std::vector<T>& var;
std::string name;
std::string msg;
char delim;
};
class MultiOptionStringImpl : public MultiOptionImpl<std::string>
{
public:
MultiOptionStringImpl(std::vector<std::string>& var, std::string const& default_vals, std::string const& name, bool required, std::string const& msg, char delim) :
MultiOptionImpl(var, default_vals, name, required, msg, delim)
{}
void setValue(std::string const& val) {
var.clear();
std::stringstream ss(val);
for (std::string val_str; std::getline(ss, val_str, delim);) {
var.push_back(val_str);
}
}
};
class FlagImpl : public Option {
public:
FlagImpl(bool& flag, std::string const& name, std::string const& msg) :
flag(flag),
name(name),
msg(msg)
{
flag = false;
}
std::string getDefault() { return ""; }
std::string getName() { return name; }
std::string getMessage() { return msg; }
void setParsed(bool parsed) { Option::setParsed(parsed); flag = parsed; }
void setValue(std::string const& val) {}
protected:
bool& flag;
std::string name;
std::string msg;
};
class Argh {
public:
Argh(char delim = ',') : delim(delim) {}
~Argh() { for (auto o : options) { delete o; } options.clear(); }
void parse(int argc, char const* argv[]) {
for (int i = 0; i < argc; ++i) {
for (auto o : options) {
if (std::string(argv[i]) == o->getName()) {
o->setParsed(true);
if (i + 1 < argc) {
o->setValue(argv[i + 1]);
}
}
}
}
}
void parseEnv() {
for (auto o : options) {
auto str = getenv(o->getName().c_str());
if (str) {
o->setParsed(true);
o->setValue(str);
}
}
}
template<typename T>
void addOption(T& var, T const& default_val, std::string const& name, bool required = false, std::string const& msg = "") {
options.push_back(new OptionImpl<T>(var, default_val, name, required, msg));
}
void addOption(std::string& var, std::string const& default_val, std::string const& name, bool required = false, std::string const& msg = "") {
options.push_back(new OptionStringImpl(var, default_val, name, required, msg));
}
template<typename T>
void addMultiOption(std::vector<T>& var, std::string const& default_vals, std::string const& name, bool required = false, std::string const& msg = "") {
options.push_back(new MultiOptionImpl<T>(var, default_vals, name, required, msg, delim));
}
void addMultiOption(std::vector<std::string>& var, std::string const& default_vals, std::string const& name, bool required = false, std::string const& msg = "") {
options.push_back(new MultiOptionStringImpl(var, default_vals, name, required, msg, delim));
}
void addFlag(bool& flag, std::string const& name, std::string const& msg = "") {
options.push_back(new FlagImpl(flag, name, msg));
}
std::string getUsage() {
size_t name_space = getLongestName() + 1;
size_t default_space = getLongestDefault() + 1;
size_t msg_space = getLongestMessage() + 1;
std::stringstream ret;
ret << std::left;
for (auto o : options) {
ret
<< std::setw(static_cast<int>(name_space)) << o->getName()
<< std::setw(static_cast<int>(default_space)) << o->getDefault()
<< std::setw(static_cast<int>(msg_space)) << o->getMessage()
<< (o->getRequired() ? "REQUIRED" : "NOT REQUIRED")
<< std::endl;
}
return ret.str();
}
bool isParsed(std::string const& name) {
for (auto o : options) {
if (name == o->getName() && o->getParsed()) {
return true;
}
}
return false;
}
std::vector<std::string> missingRequired() {
std::vector<std::string> missing;
for (auto o : options) {
if (o->getRequired() && !o->getParsed())
missing.push_back(o->getName());
}
return missing;
}
bool load(std::string const& filename) {
std::ifstream ifs(filename);
if (!ifs.good()) { return false; }
int argc = 0;
std::vector<std::string> argv_str;
std::vector<char const*> argv;
std::string arg;
while (std::getline(ifs, arg)) {
argv_str.push_back(arg);
++argc;
}
for (int i = 0; i < argc; ++i) {
argv.push_back(argv_str[i].c_str());
}
parse(argc, &*argv.begin());
return true;
}
protected:
size_t getLongestName() {
size_t ret = 0;
for (auto o : options) {
ret = std::max(ret, o->getName().length());
}
return ret;
}
size_t getLongestDefault() {
size_t ret = 0;
for (auto o : options) {
ret = std::max(ret, o->getDefault().length());
}
return ret;
}
size_t getLongestMessage() {
size_t ret = 0;
for (auto o : options) {
ret = std::max(ret, o->getMessage().length());
}
return ret;
}
std::vector<Option*> options;
char delim;
};