Refactor console ending this saga

This commit is contained in:
2022-05-18 00:32:10 +02:00
parent 7f5643dcae
commit 59b5a16d6f
13 changed files with 274 additions and 64 deletions

View File

@ -0,0 +1,46 @@
#include <string>
#include "../utils/nt.hpp"
#include "../utils/string.hpp"
#include "flags.hpp"
#include <cassert>
#include <shellapi.h>
namespace utils::flags {
void parse_flags(std::vector<std::string>& flags) {
int num_args;
auto* const argv = CommandLineToArgvW(GetCommandLineW(), &num_args);
assert(flags.empty());
if (argv == nullptr) {
return;
}
for (auto i = 0; i < num_args; ++i) {
std::wstring wide_flag(argv[i]);
if (wide_flag[0] == L'-') {
wide_flag.erase(wide_flag.begin());
const auto converted_string = string::convert(wide_flag);
flags.emplace_back(string::to_lower(converted_string));
}
}
LocalFree(argv);
}
bool has_flag(const std::string& flag) {
static auto parsed = false;
static std::vector<std::string> enabled_flags;
if (!parsed) {
parse_flags(enabled_flags);
parsed = true;
}
return std::ranges::any_of(
enabled_flags.cbegin(), enabled_flags.cend(),
[flag](const auto& elem) { return elem == string::to_lower(flag); });
}
} // namespace utils::flags

View File

@ -0,0 +1,5 @@
#pragma once
namespace utils::flags {
bool has_flag(const std::string& flag);
}