7 const auto key = arg.substr(2);
9 throw std::invalid_argument(
"Unknown option: --" + std::string(key));
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);
20 if (!isValidOption(singleOpt)) {
21 throw std::invalid_argument(
"Unknown option: -" + std::string(singleOpt));
31 if (
const auto display = envp.find(
"DISPLAY"); display == envp.end()) {
32 throw std::runtime_error(
"DISPLAY environment variable not set");
40 for (
size_t i = 0; i < argv.size(); ++i) {
41 const std::string_view& arg = argv[i];
42 if (arg.empty()) {
continue; }
44 if (arg.starts_with(
"--")) {
45 handleLongOption(arg, argv, i);
46 }
else if (arg.starts_with(
'-')) {
47 handleShortOptions(arg);
49 throw std::invalid_argument(
"Unknown option: " + std::string(arg));
56 if (envp ==
nullptr) {
57 throw std::runtime_error(
"cannot access environment variables");
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) });
67 parseEnv(envVariables);
70 parseArgs(std::vector<std::string_view>(argv + 1, argv + argc));
This file contains the Parser class.
Parser(int argc, char *argv[], char *envp[])
static bool isValidOption(const std::string_view &option)
void parseEnv(const std::unordered_map< std::string, std::string > &envp)
void handleShortOptions(const std::string_view &arg)
void parseArgs(const std::vector< std::string_view > &argv)
void handleLongOption(const std::string_view &arg, const std::vector< std::string_view > &argv, size_t &index)
static const std::unordered_map< std::string_view, std::function< void(Config &conf, std::string_view arg)> > FUNCTION_MAP_OPT_LONG
static const std::unordered_map< std::string_view, std::function< void(Config &conf)> > FUNCTION_MAP_OPT_SHORT