-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcli_parser.hpp
More file actions
112 lines (102 loc) · 3.46 KB
/
cli_parser.hpp
File metadata and controls
112 lines (102 loc) · 3.46 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
#ifndef CLI_PARSER_HPP
#define CLI_PARSER_HPP
#include <getopt.h>
#include <cstdlib>
#include <iostream>
#include <string>
#include "version.hpp"
namespace CLI_Parser {
struct GameSettings {
bool asciiOnly_ = false;
bool obstacleDino_ = true;
int keyRepeat_ = 200;
bool skipIntro_ = false;
};
inline void printHelp() {
std::cout << "TermRex Runner Game CLI\n"
<< "=======================\n\n"
<< "Usage:\n"
<< " termrex [options]\n\n"
<< "Options:\n"
<< " -h, --help Show this help menu\n"
<< " -v, --version Show game version\n"
<< " --ascii-only Use ASCII characters only\n"
<< " --unicode Use Unicode characters (default)\n"
<< " --no-obstacle-dino Disable flying dinosaur "
<< "(Pterodactyl) obstacles\n"
<< " --keyrepeat <ms> Set custom key repeat delay in "
<< "milliseconds\n"
<< " --skip-intro Skip the intro screen and start "
<< "game immediately\n";
}
inline void printVersion() {
std::cout << "v" << TERMREX_VERSION_STRING << "\n";
}
inline GameSettings parseArguments(int argc, char* argv[]) {
GameSettings settings;
static const struct option LONG_OPTIONS[] = {
{"help", no_argument, 0, 'h'},
{"version", no_argument, 0, 'v'},
{"ascii-only", no_argument, 0, 0},
{"unicode", no_argument, 0, 0},
{"no-obstacle-dino", no_argument, 0, 0},
{"keyrepeat", required_argument, 0, 0},
{"skip-intro", no_argument, 0, 0},
{0, 0, 0, 0}};
int optionIndex = 0;
int c;
optind = 1;
while ((c = getopt_long(argc, argv, "hv", LONG_OPTIONS, &optionIndex)) !=
-1) {
if (c == 'h') {
printHelp();
exit(0);
} else if (c == 'v') {
printVersion();
exit(0);
} else if (c == 0) {
std::string optname = LONG_OPTIONS[optionIndex].name;
if (optname == "ascii-only") {
settings.asciiOnly_ = true;
} else if (optname == "unicode") {
settings.asciiOnly_ = false;
} else if (optname == "no-obstacle-dino") {
settings.obstacleDino_ = false;
} else if (optname == "skip-intro") {
settings.skipIntro_ = true;
} else if (optname == "keyrepeat") {
if (optarg) {
try {
settings.keyRepeat_ = std::stoi(optarg);
if (settings.keyRepeat_ < 50) {
std::cerr << "Warning: Key repeat delay is very low ("
<< settings.keyRepeat_
<< "ms). Performance may suffer.\n";
exit(1);
} else if (settings.keyRepeat_ > 1200) {
std::cerr << "Warning: Key repeat delay is very high ("
<< settings.keyRepeat_
<< "ms). Performance may suffer.\n";
exit(1);
}
} catch (const std::exception& e) {
std::cerr << "Error: Invalid number for --keyrepeat: " << optarg
<< ". Must be an integer.\n";
exit(1);
}
} else {
std::cerr << "Error: --keyrepeat requires a numeric argument.\n";
exit(1);
}
}
} else if (c == '?') {
exit(1);
} else {
std::cerr << "Unknown error during argument parsing (c=" << c << ").\n";
exit(1);
}
}
return settings;
}
} // namespace CLI_Parser
#endif