-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcmdlp.cpp
More file actions
234 lines (219 loc) · 6.31 KB
/
cmdlp.cpp
File metadata and controls
234 lines (219 loc) · 6.31 KB
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
#include "cmdlp.hpp"
#include "paragraph.hpp"
#include <sstream>
#include <fstream>
const char* com::masaers::cmdlp::unescape_until(const char* first, const char* terminators, std::string& out) {
static const char* quotes = "'\"";
for (/**/; first != nullptr && *first != '\0' && strchr(terminators, *first) == nullptr; ++first) {
if (strchr(quotes, *first) == nullptr) {
if (*first == '\\') {
++first;
}
if (*first != '\0') {
out.push_back(*first);
} else {
first = nullptr;
}
} else {
const char quote = *first++;
for (/**/; first != nullptr && *first != '\0' && *first != quote; ++ first) {
out.push_back(*first);
}
if (*first != quote) {
first = nullptr;
}
}
}
return first;
}
void com::masaers::cmdlp::escape_str(const char quote, const std::string& str, std::ostream& out) {
if (quote == '\'' || quote == '"') {
out << quote;
for (const char& c : str) {
if (c == quote) {
out << '\\';
}
out << c;
}
out << quote;
} else {
for (const char& c : str) {
if (c == ' ') {
out << '\\';
}
out << c;
}
}
}
void com::masaers::cmdlp::value_option<com::masaers::cmdlp::config_files>::assign(const char* str) {
base_class::read()(*config_files_m, str);
base_class::parser_ptr()->error_count_m += base_class::parser_ptr()->parse_file(config_files_m->filenames().back().c_str());
}
void com::masaers::cmdlp::value_option<com::masaers::cmdlp::config_files>::evaluate(std::ostream& out) const {
const auto& fns = config_files_m->filenames();
for (auto it = fns.begin(); it != fns.end(); ++it) {
if (it != fns.begin()) {
out << ' ';
}
escape_str(' ', *it, out);
}
}
com::masaers::cmdlp::parser::~parser() {
using namespace std;
for (auto it = begin(options_m); it != end(options_m); ++it) {
delete *it;
*it = NULL;
}
}
std::string com::masaers::cmdlp::parser::usage() const {
using namespace std;
ostringstream s;
for (const auto& opt : options_m) {
if (opt->in_usage()) {
auto it = bindings_m.find(opt);
if (it != bindings_m.end()) {
s << ' ';
print_call(s, it->second.first, it->second.second, false);
}
}
}
return s.str();
}
std::string com::masaers::cmdlp::parser::help() const {
using namespace std;
ostringstream s;
for (const auto& opt : options_m) {
auto it = bindings_m.find(opt);
if (it != bindings_m.end()) {
print_call(s, it->second.first, it->second.second, true);
} else {
s << "n/n";
}
s << '=';
opt->evaluate(s);
s << endl;
{
const auto p = paragraph(s, 80, 4, 3);
opt->describe(s);
s << endl;
}
}
return s.str();
}
void com::masaers::cmdlp::parser::dumpto_stream(std::ostream& out, bool include_meta) const {
using namespace std;
for (const auto& opt : options_m) {
if (include_meta || ! opt->is_meta()) {
auto it = bindings_m.find(opt);
if (it != bindings_m.end()) {
if (! it->second.first.empty()) {
out << it->second.first.front();
} else {
out << it->second.second.front();
}
} else {
out << "<unnamed option>";
}
out << '=';
opt->evaluate(out);
out << endl;
}
}
}
bool com::masaers::cmdlp::parser::bind(option_i* opt, const char flag) {
auto p = flags_m.insert(std::make_pair(flag, opt));
if (! p.second) {
std::ostringstream s;
s << "Failed to bind option to flag '-" << flag << "'. "
<< "It already exists.";
throw std::runtime_error(s.str().c_str());
} else {
bindings_m[opt].second.push_back(flag);
}
return p.second;
}
bool com::masaers::cmdlp::parser::bind(option_i* opt, const std::string& name) {
auto p = names_m.insert(std::make_pair(name, opt));
if (! p.second) {
std::ostringstream s;
s << "Failed to bind option to name '--" << name << "'. "
<< "It already exists.";
throw std::runtime_error(s.str().c_str());
} else {
bindings_m[opt].first.push_back(name);
}
return p.second;
}
void com::masaers::cmdlp::parser::print_call(std::ostream& s, const std::vector<std::string>& names, std::vector<char> flags, bool print_all) {
using namespace std;
for (auto it = begin(names); it != end(names); ++it) {
if (it != begin(names)) {
s << "|";
}
const auto pos = find(begin(flags), end(flags), it->front());
if (pos != end(flags)) {
flags.erase(pos);
s << "-[-" << it->front() << "]" << it->substr(1);
} else {
s << "--" << *it;
}
}
for (auto it = begin(flags); it != end(flags); ++it) {
if (names.size() != 0 || it != begin(flags)) {
s << "|";
}
s << "-" << *it;
}
}
std::size_t com::masaers::cmdlp::parser::parse_file(const char* filename) const {
using namespace std;
size_t error_count = 0;
ifstream ifs(filename);
if (ifs) {
for (std::string line; getline(ifs, line); ) {
const auto div = line.find('=');
const auto it = names_m.find(line.substr(0, div));
if (it != names_m.end()) {
option_i* opt = it->second;
try {
string unesc;
const char* at = unescape_until(line.c_str() + div + 1, " \t", unesc);
for (; at != nullptr && ! (unesc.empty() && *at == '\0'); at = unescape_until(at, " \t", unesc)) {
if (unesc.empty()) {
++at;
} else {
opt->observe();
opt->assign(unesc.c_str());
unesc.clear();
}
}
if (at == nullptr || *at != '\0') {
throw std::runtime_error("Malformed value");
}
} catch(const std::exception& e) {
*erros_m << e.what() << std::endl;
++error_count;
}
}
}
} else {
*erros_m << "Failed to open file: '" << filename << "'" << endl;
++error_count;
}
return error_count;
}
std::size_t com::masaers::cmdlp::parser::validate() const {
std::size_t result = 0;
for (const auto& opt : options_m) {
if (! opt->validate()) {
auto it = bindings_m.find(opt);
if (it != bindings_m.end()) {
*erros_m << "Required option '";
print_call(*erros_m, it->second.first, it->second.second, false);
*erros_m << "' not set." << std::endl;
++result;
}
}
}
return result;
}