vengine  0.0.1
3D graphics engine
Loading...
Searching...
No Matches
parser.cpp
Go to the documentation of this file.
1#include <iostream>
2#include <sstream>
3
5
6void ven::Parser::handleLongOption(const std::string_view& arg, const std::vector<std::string_view>& argv, size_t& index) {
7 const auto key = arg.substr(2);
8 if (!isValidOption(key)) {
9 throw std::invalid_argument("Unknown option: --" + std::string(key));
10 }
11
12 index + 1 < argv.size() && !argv[index + 1].starts_with('-') ? FUNCTION_MAP_OPT_LONG.at(key)(m_config, argv[++index]) : FUNCTION_MAP_OPT_LONG.at(key)(m_config, "");
13}
14
15void ven::Parser::handleShortOptions(const std::string_view& arg) {
16 const auto key = arg.substr(1);
17 for (size_t j = 0; j < key.length(); ++j) {
18 const std::string_view singleOpt = key.substr(j, 1);
19
20 if (!isValidOption(singleOpt)) {
21 throw std::invalid_argument("Unknown option: -" + std::string(singleOpt));
22 }
23 FUNCTION_MAP_OPT_SHORT.at(singleOpt)(m_config);
24 }
25}
26
27void ven::Parser::parseEnv(const std::unordered_map<std::string, std::string>& envp)
28{
29#ifdef __linux__
30
31 if (const auto display = envp.find("DISPLAY"); display == envp.end()) {
32 throw std::runtime_error("DISPLAY environment variable not set");
33 }
34
35#endif
36}
37
38void ven::Parser::parseArgs(const std::vector<std::string_view>& argv)
39{
40 for (size_t i = 0; i < argv.size(); ++i) {
41 const std::string_view& arg = argv[i];
42 if (arg.empty()) { continue; }
43
44 if (arg.starts_with("--")) {
45 handleLongOption(arg, argv, i);
46 } else if (arg.starts_with('-')) {
47 handleShortOptions(arg);
48 } else {
49 throw std::invalid_argument("Unknown option: " + std::string(arg));
50 }
51 }
52}
53
54ven::Parser::Parser(const int argc, char* argv[], char* envp[])
55{
56 if (envp == nullptr) {
57 throw std::runtime_error("cannot access environment variables");
58 }
59
60 std::unordered_map<std::string, std::string> envVariables;
61 for (int i = 0; envp[i] != nullptr; ++i) {
62 const std::string env(envp[i]);
63 if (const auto pos = env.find('='); pos != std::string_view::npos) {
64 envVariables.insert({ env.substr(0, pos), env.substr(pos + 1) });
65 }
66 }
67 parseEnv(envVariables);
68
69 if (argc > 1) {
70 parseArgs(std::vector<std::string_view>(argv + 1, argv + argc));
71 }
72}
This file contains the Parser class.
Parser(int argc, char *argv[], char *envp[])
Definition parser.cpp:54
static bool isValidOption(const std::string_view &option)
Definition Parser.hpp:161
Config m_config
Definition Parser.hpp:152
void parseEnv(const std::unordered_map< std::string, std::string > &envp)
Definition parser.cpp:27
void handleShortOptions(const std::string_view &arg)
Definition parser.cpp:15
void parseArgs(const std::vector< std::string_view > &argv)
Definition parser.cpp:38
void handleLongOption(const std::string_view &arg, const std::vector< std::string_view > &argv, size_t &index)
Definition parser.cpp:6
static const std::unordered_map< std::string_view, std::function< void(Config &conf, std::string_view arg)> > FUNCTION_MAP_OPT_LONG
Definition Parser.hpp:46
static const std::unordered_map< std::string_view, std::function< void(Config &conf)> > FUNCTION_MAP_OPT_SHORT
Definition Parser.hpp:122