-
Notifications
You must be signed in to change notification settings - Fork 0
/
cmd_args.cpp
192 lines (168 loc) · 4 KB
/
cmd_args.cpp
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
#include "cmd_args.h"
#include <cerrno>
#include <string>
#include <iostream>
#include <sstream>
using namespace std;
struct AbstractFlag {
AbstractFlag(const string& name) : name(name) {}
const string name;
virtual bool Parse(const string& value) = 0;
virtual string String() const = 0;
};
template <class T>
struct BaseFlag : public AbstractFlag {
BaseFlag(const string& name, const T& def) : AbstractFlag(name), value(def) {}
T value;
const T* Ptr() {
return &this->value;
}
virtual bool Parse(const string& value) = 0;
virtual string String() const {
stringstream stream;
stream << value;
return stream.str();
}
};
struct IntFlag : public BaseFlag<int> {
IntFlag(const string& name, int def) : BaseFlag(name, def) {}
virtual bool Parse(const string& value) {
errno = 0;
char* end = nullptr;
const char* str = value.c_str();
int size = value.size();
this->value = strtol(str, &end, 10);
return (errno == 0 && end == str + size);
}
};
struct StringFlag : public BaseFlag<string> {
StringFlag(const string& name, const string& def) : BaseFlag(name, def) {}
virtual bool Parse(const string& value) {
this->value = value;
return true;
}
};
struct BoolFlag : public BaseFlag<bool> {
BoolFlag(const string& name, bool def) : BaseFlag(name, def) {}
virtual bool Parse(const string& value) {
if (value == "" || value == "true") {
this->value = true;
} else if (value == "false") {
this->value = false;
} else {
return false;
}
return true;
}
};
static AbstractFlag* all_flags[30];
static int num_flags = 0;
const int* NewIntFlag(const string& name, int def) {
auto flag = new IntFlag(name, def);
all_flags[num_flags++] = flag;
return flag->Ptr();
}
const string* NewStringFlag(const string& name, const string& def) {
auto flag = new StringFlag(name, def);
all_flags[num_flags++] = flag;
return flag->Ptr();
}
const bool* NewBoolFlag(const string& name, const bool def) {
auto flag = new BoolFlag(name, def);
all_flags[num_flags++] = flag;
return flag->Ptr();
}
AbstractFlag* FindFlag(const string& name) {
for (int i = 0; all_flags[i] != nullptr; i++) {
if (all_flags[i]->name == name) {
return all_flags[i];
}
}
return nullptr;
}
bool isDoubleDash(const string& v) {
if (v.size() < 2) {
return false;
}
return v[0] == '-' && v[1] == '-';
}
bool isSingleDash(const string& v) {
if (v.empty()) {
return false;
}
if (v[0] != '-') {
return false;
}
return !isDoubleDash(v);
}
bool isArg(const string& v) {
return isDoubleDash(v) || isSingleDash(v);
}
string StripDash(const string& v) {
if (isSingleDash(v)) {
return v.substr(1);
}
if (isDoubleDash(v)) {
return v.substr(2);
}
return v;
}
bool HasValue(const string& v) {
for (const char c : v) {
if (c == '=') {
return true;
}
}
return false;
}
string GetValue(const string& v) {
for (int i = 0; i < int(v.size()); i++) {
if (v[i] == '=') {
return v.substr(i+1);
}
}
return "";
}
string StripValue(const string& v) {
for (int i = 0; i < int(v.size()); i++) {
if (v[i] == '=') {
return v.substr(0, i);
}
}
return "";
}
bool ParseFlags(int argc, const char** argv) {
string v;
for (int i = 1; i < argc; i++) {
v = argv[i];
if (!isArg(v)) {
cerr << "Loose argument: " << v << endl;
return false;
}
v = StripDash(v);
auto flag = FindFlag(v);
if (flag == nullptr) {
cerr << "Unknown flag: --" << v << endl;
}
string value;
if (HasValue(v)) {
value = GetValue(v);
v = StripValue(v);
} else {
if (i+1 == argc || isArg(argv[i+1])) {
value = "";
} else {
i++;
value = argv[i];
}
}
if (!flag->Parse(value)) {
cerr << "Failed to parse flag --" << v << endl;
return false;
}
}
for (int i = 0; i < num_flags; i++) {
cerr << "Flag " << all_flags[i]->name << " " << all_flags[i]->String() << endl;
}
return true;
}