From 999c142a312dbc92f808b1849957ec829f5f0c3c Mon Sep 17 00:00:00 2001 From: scawful Date: Mon, 31 Mar 2025 09:42:10 -0400 Subject: [PATCH] remove DECLARE_FLAG macro and add mode parsing --- src/cli/z3ed.cc | 11 ----------- src/util/flag.cc | 46 ++++++++++++++++++++++++++++++++++++++++++++++ src/util/flag.h | 15 ++++----------- 3 files changed, 50 insertions(+), 22 deletions(-) diff --git a/src/cli/z3ed.cc b/src/cli/z3ed.cc index 7d30e613..f722e38f 100644 --- a/src/cli/z3ed.cc +++ b/src/cli/z3ed.cc @@ -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."); diff --git a/src/util/flag.cc b/src/util/flag.cc index d34e9f95..9a9a8b64 100644 --- a/src/util/flag.cc +++ b/src/util/flag.cc @@ -1,5 +1,9 @@ #include "flag.h" +#include + +#include "yaze_config.h" + namespace yaze { namespace util { @@ -39,6 +43,28 @@ void FlagParser::Parse(std::vector* 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* 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 \ No newline at end of file diff --git a/src/util/flag.h b/src/util/flag.h index b3ce41a0..9a8128f7 100644 --- a/src/util/flag.h +++ b/src/util/flag.h @@ -102,8 +102,6 @@ inline FlagRegistry* global_flag_registry() { return registry; } -#define DECLARE_FLAG(type, name) extern yaze::util::Flag* FLAGS_##name - // Defines a global Flag* FLAGS_ and registers it. #define DEFINE_FLAG(type, name, default_val, help_text) \ yaze::util::Flag* 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