remove DECLARE_FLAG macro and add mode parsing

This commit is contained in:
scawful
2025-03-31 09:42:10 -04:00
parent 3cf8edb467
commit 999c142a31
3 changed files with 50 additions and 22 deletions

View File

@@ -11,17 +11,6 @@
#include "util/flag.h"
#include "util/macro.h"
DECLARE_FLAG(std::string, rom_file);
DECLARE_FLAG(std::string, bps_file);
DECLARE_FLAG(std::string, src_file);
DECLARE_FLAG(std::string, modified_file);
DECLARE_FLAG(std::string, bin_file);
DECLARE_FLAG(std::string, address);
DECLARE_FLAG(std::string, length);
DECLARE_FLAG(std::string, file_size);
DECLARE_FLAG(std::string, dest_rom);
DECLARE_FLAG(std::string, tile32_id_list);
DEFINE_FLAG(std::string, rom_file, "", "The ROM file to load.");
DEFINE_FLAG(std::string, bps_file, "", "The BPS file to apply.");

View File

@@ -1,5 +1,9 @@
#include "flag.h"
#include <iostream>
#include "yaze_config.h"
namespace yaze {
namespace util {
@@ -39,6 +43,28 @@ void FlagParser::Parse(std::vector<std::string>* tokens) {
// Set the parsed value on the matching flag.
flag_ptr->ParseValue(value_string);
} else if (token.rfind("-", 0) == 0) {
if (token == "-v" || token == "-version") {
std::cout << "Version: " << YAZE_VERSION_MAJOR << "."
<< YAZE_VERSION_MINOR << "." << YAZE_VERSION_PATCH << "\n";
exit(0);
}
// Check for -h or -help
if (token == "-h" || token == "-help") {
std::cout << "Available flags:\n";
for (const auto& flag :
yaze::util::global_flag_registry()->AllFlags()) {
std::cout << flag->name() << ": " << flag->help() << "\n";
}
exit(0);
}
std::string flag_name;
if (!ExtractFlag(token, &flag_name)) {
throw std::runtime_error("Unrecognized flag: " + token);
}
} else {
leftover.push_back(token);
}
@@ -46,5 +72,25 @@ void FlagParser::Parse(std::vector<std::string>* tokens) {
*tokens = leftover;
}
bool FlagParser::ExtractFlagAndValue(const std::string& token,
std::string* flag_name,
std::string* value_string) {
const size_t eq_pos = token.find('=');
if (eq_pos == std::string::npos) {
return false;
}
*flag_name = token.substr(0, eq_pos);
*value_string = token.substr(eq_pos + 1);
return true;
}
bool FlagParser::ExtractFlag(const std::string& token, std::string* flag_name) {
if (token.rfind("-", 0) == 0) {
*flag_name = token;
return true;
}
return false;
}
} // namespace util
} // namespace yaze

View File

@@ -102,8 +102,6 @@ inline FlagRegistry* global_flag_registry() {
return registry;
}
#define DECLARE_FLAG(type, name) extern yaze::util::Flag<type>* FLAGS_##name
// Defines a global Flag<type>* FLAGS_<name> and registers it.
#define DEFINE_FLAG(type, name, default_val, help_text) \
yaze::util::Flag<type>* FLAGS_##name = \
@@ -138,15 +136,10 @@ class FlagParser {
// value. e.g. "--count=42" -> flag_name = "--count", value_string = "42"
// returns true if '=' was found
bool ExtractFlagAndValue(const std::string& token, std::string* flag_name,
std::string* value_string) {
const size_t eq_pos = token.find('=');
if (eq_pos == std::string::npos) {
return false;
}
*flag_name = token.substr(0, eq_pos);
*value_string = token.substr(eq_pos + 1);
return true;
}
std::string* value_string);
// Mode flag '-'
bool ExtractFlag(const std::string& token, std::string* flag_name);
};
} // namespace util