-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathcmdline_options.h
336 lines (288 loc) · 7.66 KB
/
cmdline_options.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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
#ifndef CMDLINE_OPTIONS_H_
#define CMDLINE_OPTIONS_H_
#include <filesystem>
#include <string>
#include <sstream>
#include <vector>
#include <getopt.h>
using namespace std::string_literals;
class cmdline_options
{
public:
// result gem representations
struct {
unsigned parens:1;
unsigned tree:1;
unsigned equations:1;
unsigned table:1;
} print = {0, 0, 0, 0};
// output verbosity
struct {
unsigned quiet:1;
unsigned info:1;
unsigned debug:1;
} output = {0, 0, 0};
// skills
struct {
int TC;
int amps;
} skills = {0, 0};
// amps
struct {
int number_per_gem;
double average_gems_seen;
} amps = {0, 1};
// tuning
struct {
double combine_growth;
int spec_limit;
double max_ag_cost_ratio;
int final_eq_grade;
} tuning = {0, 0, 1, 30};
// gem tables paths
std::vector<std::string> tables;
// stuff to do
struct {
// extra search
unsigned upto:1;
unsigned chain:1;
// extra args parsing
unsigned read_pool_zero:1;
unsigned read_lenc:1;
// gem sizes (positional args)
int len;
int lenc;
uint pool_zero;
} target = {0, 0, 0, 0, 0, 16, 1};
private:
struct {
std::string short_options;
std::vector<struct option> long_options;
} getopt;
std::string help_text;
unsigned int num_tables_ = 0;
public:
cmdline_options()
{
this->add_option({"help", no_argument, NULL, 'h'}, "show this help message and exit");
this->add_option({"log-quiet", no_argument, NULL, 'q'}, "log less, skip unneded computation");
this->add_option({"verbose", no_argument, NULL, 'v'}, "log a lot more");
}
void has_printing()
{
this->add_option({"print-parens", no_argument, NULL, 'p'}, "print parens representations");
this->add_option({"print-tree", no_argument, NULL, 't'}, "print tree representations");
this->add_option({"print-equations", no_argument, NULL, 'e'}, "print equations representations");
this->add_option({"print-table", no_argument, NULL, 'c'}, "print table of values");
}
void has_extra_search()
{
this->add_option({"upto", no_argument, NULL, 'u'}, "return best result up to bound");
this->add_option({"add-red", no_argument, NULL, 'r'}, "add a single \"red\" gem to the result");
}
void has_amps()
{
this->add_option({"skill-amps", required_argument, NULL, 'A'}, "value of the amp skill");
this->add_option({"amps-per-gem", required_argument, NULL, 'Q'}, "number of amps per gem");
this->add_option({"avg-gems-seen", required_argument, NULL, 'G'}, "average gems seens by each amp");
this->add_option({"max-cost-ratio", required_argument, NULL, 'R'}, "max cost ratio amp/gem");
}
void has_nonpures()
{
this->add_option({"skill-TC", required_argument, NULL, 'T'}, "value of the TC skill");
}
void has_combine_growth()
{
this->add_option({"combine-growth", required_argument, NULL, 'g'}, "value of the combine growth");
}
void has_spec_limit()
{
this->add_option({"limit-spec", required_argument, NULL, 'l'}, "max spec size");
}
void has_final_eq_grade()
{
this->add_option({"final-eq-grade", required_argument, NULL, 'F'}, "final equivalent grade");
}
void set_num_tables(uint num_tables)
{
this->num_tables_ = num_tables;
this->add_option({"table-file", required_argument, NULL, 'f'}, "table[,tableA][,tableC]");
}
void has_lenc(uint default_ = 16)
{
this->target.read_lenc = true;
this->target.lenc = default_;
}
void has_pool_zero(uint default_ = 1)
{
this->target.read_pool_zero = true;
this->target.pool_zero = default_;
}
inline bool read_cmdline_opt(int opt, const char* optarg)
{
switch(opt) {
case 'h':
this->print_help();
return false;
case 'q':
this->output.quiet = true;
break;
case 'v':
if (this->output.info)
this->output.debug = true;
else
this->output.info = true;
break;
case 'p':
this->print.parens = true;
break;
case 't':
this->print.tree = true;
break;
case 'e':
this->print.equations = true;
break;
case 'c':
this->print.table = true;
break;
case 'u':
this->target.upto = true;
break;
case 'r':
this->target.chain = true;
break;
case 'A':
this->skills.amps = atoi(optarg);
break;
case 'Q':
this->amps.number_per_gem = atof(optarg);
break;
case 'G':
this->amps.average_gems_seen = atof(optarg);
break;
case 'R':
this->tuning.max_ag_cost_ratio = atof(optarg);
break;
case 'T':
this->skills.TC = atoi(optarg);
break;
case 'g':
this->tuning.combine_growth = atof(optarg);
break;
case 'l':
this->tuning.spec_limit = atoi(optarg);
break;
case 'F':
this->tuning.final_eq_grade = atoi(optarg);
break;
case 'f': {
std::stringstream ss(optarg);
std::string s;
while (std::getline(ss, s, ',')) {
this->tables.push_back(s);
}
if (this->tables.size() > this->num_tables_) {
printf("Too many table names, expected %u\n", this->num_tables_);
return false;
}
break;
}
case '?':
return false;
default:
break;
}
return true;
}
inline void parse_len(const char* arg) {
char* p;
this->target.len = strtol(arg, &p, 10);
if (this->target.read_pool_zero) {
if (*p == 'c')
this->target.pool_zero = 1;
else if (*p == 's')
this->target.pool_zero = 2;
}
}
// I assume defaults have been set before calling this
bool parse_args(int argc, char*const* argv)
{
int opt;
while ((opt = getopt_long(argc, argv, this->getopt.short_options.c_str(), this->getopt.long_options.data(), NULL)) != -1) {
if (!this->read_cmdline_opt(opt, optarg))
return false;
}
if (optind == argc) {
printf("No length specified\n");
return false;
}
if (optind + 1 == argc) {
parse_len(argv[optind]);
}
else if (optind + 2 == argc && target.read_lenc) {
parse_len(argv[optind]);
this->target.lenc = atoi(argv[optind + 1]);
}
else {
printf("Too many arguments:\n");
while (argv[optind] != NULL) {
printf("%s ", argv[optind]);
optind++;
}
printf("\n");
return false;
}
if (this->target.len == 0 || (target.read_lenc && this->target.lenc == 0)) {
printf("Improper gem number\n");
return false;
}
// fill up to desired size with empty strings
this->tables.resize(this->num_tables_);
return true;
}
void print_help() const
{
// can (and should) be improved
if (!target.read_pool_zero)
printf("len\t\tlength of result\n");
else
printf("len[cs]\t\tlength of result, the c/s prefix selects between specs and combines\n");
if (target.read_lenc)
printf("lenc\t\tlength of combine\n");
printf("%s\n", this->help_text.c_str());
}
void table_selection(unsigned int num, const std::string& default_name)
{
std::string& filename = this->tables[num];
if (!filename.empty())
;
else if (std::filesystem::exists(default_name)) {
filename = default_name;
}
else {
std::string other_path = "gem_tables/" + default_name;
if (std::filesystem::exists(other_path))
filename = other_path;
else
// we know it'll fail, but at least the error message will make sense
filename = default_name;
}
// print table list at last iteration, when all are known
if (this->output.debug && num == this->num_tables_ - 1) {
printf("Selected table(s):");
for (auto& s : this->tables)
printf(" %s", s.c_str());
printf("\n");
}
}
private:
void add_option(struct option&& option, const std::string& help_text)
{
this->getopt.short_options += (char)option.val;
this->getopt.short_options += std::string(option.has_arg, ':');
this->getopt.long_options.push_back(option);
this->help_text += "-"s + (char)option.val + " --" + option.name + (option.has_arg ? " X" : "") +
" \t" + help_text + '\n';
}
};
#endif /* CMDLINE_OPTIONS_H_ */