-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathoptions.cpp
102 lines (87 loc) · 2.51 KB
/
options.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
#include <iostream>
#include <unistd.h>
#include "decl.hpp"
#include "options.hpp"
namespace rematrix {
namespace {
vec3 parse_color(string_view s)
{
const runtime_error err { "invalid color" };
if (s.size() != 7 || s[0] != '#') {
throw err;
}
size_t n { 0 };
// XXX: allows "#+12345" or "#-12345"
const auto color { stoul(string(s.substr(1)), &n, 16) };
if (n != 6 || color > 0xFFFFFF) {
throw err;
}
return {
((color >> 16) & 0xFF) / 255.0f,
((color >> 8) & 0xFF) / 255.0f,
(color & 0xFF) / 255.0f
};
}
} // namespace
options parse_options(int argc, char* argv[])
{
using std::cerr;
using std::endl;
options ans;
int opt { -1 };
while ((opt = getopt(argc, argv, "c:f:l:m:owr:")) != -1) {
switch (opt) {
case 'c':
ans.char_color = parse_color(optarg);
break;
case 'f':
ans.feeder_color = parse_color(optarg);
break;
case 'l':
ans.clear_color = parse_color(optarg);
break;
case 'm': {
const string_view s { optarg };
if (s == "bin" || s == "binary") {
ans.mode = options::binary;
} else if (s == "dna") {
ans.mode = options::dna;
} else if (s == "dec" || s == "decimal") {
ans.mode = options::decimal;
} else if (s == "hex" || s == "hexadecimal") {
ans.mode = options::hexadecimal;
} else if (s == "matrix") {
ans.mode = options::matrix;
} else {
throw runtime_error("invalid mode");
}
} break;
case 'o':
ans.enable_fog = !ans.enable_fog;
break;
case 'w':
ans.enable_waves = !ans.enable_waves;
break;
case 'r':
ans.frame_rate = stoul(optarg, nullptr);
if (ans.frame_rate > 30) {
throw runtime_error("invalid frame rate");
}
break;
default:
cerr << "Usage: " << argv[0]
<< " [-c char_color]"
<< " [-f feeder_color]"
<< " [-l clear_color]"
<< " [-m binary|dna|decimal|hexadecimal|matrix]"
<< " [-o]"
<< " [-w]"
<< " [-r frame-rate]"
<< endl;
exit(EXIT_FAILURE);
break;
}
}
return ans;
}
} // namespace rematrix