Add flag handling for ROM file in main application

This commit is contained in:
scawful
2025-01-21 22:13:00 -05:00
parent 1764ca0dde
commit 01db131adb
2 changed files with 22 additions and 9 deletions

View File

@@ -5,6 +5,7 @@
#include "absl/debugging/failure_signal_handler.h" #include "absl/debugging/failure_signal_handler.h"
#include "absl/debugging/symbolize.h" #include "absl/debugging/symbolize.h"
#include "app/core/controller.h" #include "app/core/controller.h"
#include "util/flag.h"
/** /**
* @namespace yaze * @namespace yaze
@@ -12,19 +13,22 @@
*/ */
using namespace yaze; using namespace yaze;
DECLARE_FLAG(std::string, rom_file);
DEFINE_FLAG(std::string, rom_file, "", "The ROM file to load.");
int main(int argc, char** argv) { int main(int argc, char** argv) {
absl::InitializeSymbolizer(argv[0]); absl::InitializeSymbolizer(argv[0]);
absl::FailureSignalHandlerOptions options; absl::FailureSignalHandlerOptions options;
options.symbolize_stacktrace = true; options.symbolize_stacktrace = true;
options.use_alternate_stack = true; options.use_alternate_stack = true;
options.alarm_on_failure_secs = true; options.alarm_on_failure_secs = true;
options.call_previous_handler = true; options.call_previous_handler = true;
absl::InstallFailureSignalHandler(options); absl::InstallFailureSignalHandler(options);
yaze::util::FlagParser parser(yaze::util::global_flag_registry());
std::string rom_filename; RETURN_IF_EXCEPTION(parser.Parse(argc, argv));
if (argc > 1) { std::string rom_filename = "";
rom_filename = argv[1]; if (!FLAGS_rom_file.empty()) {
rom_filename = FLAGS_rom_file;
} }
#ifdef __APPLE__ #ifdef __APPLE__

View File

@@ -96,7 +96,6 @@ class FlagRegistry {
std::unordered_map<std::string, std::unique_ptr<IFlag>> flags_; std::unordered_map<std::string, std::unique_ptr<IFlag>> flags_;
}; };
inline FlagRegistry* global_flag_registry() { inline FlagRegistry* global_flag_registry() {
// Guaranteed to be initialized once per process. // Guaranteed to be initialized once per process.
static FlagRegistry* registry = new FlagRegistry(); static FlagRegistry* registry = new FlagRegistry();
@@ -106,9 +105,10 @@ inline FlagRegistry* global_flag_registry() {
#define DECLARE_FLAG(type, name) extern yaze::util::Flag<type>* FLAGS_##name #define DECLARE_FLAG(type, name) extern yaze::util::Flag<type>* FLAGS_##name
// Defines a global Flag<type>* FLAGS_<name> and registers it. // Defines a global Flag<type>* FLAGS_<name> and registers it.
#define DEFINE_FLAG(type, name, default_val, help_text) \ #define DEFINE_FLAG(type, name, default_val, help_text) \
yaze::util::Flag<type>* FLAGS_##name = \ yaze::util::Flag<type>* FLAGS_##name = \
yaze::util::global_flag_registry()->RegisterFlag<type>("--" #name, (default_val), (help_text)) yaze::util::global_flag_registry()->RegisterFlag<type>( \
"--" #name, (default_val), (help_text))
// Retrieves the current value of a declared flag. // Retrieves the current value of a declared flag.
#define FLAG_VALUE(name) (FLAGS_##name->Get()) #define FLAG_VALUE(name) (FLAGS_##name->Get())
@@ -117,6 +117,15 @@ class FlagParser {
public: public:
explicit FlagParser(FlagRegistry* registry) : registry_(registry) {} explicit FlagParser(FlagRegistry* registry) : registry_(registry) {}
// Parses flags out of the given command line arguments.
void Parse(int argc, char** argv) {
std::vector<std::string> tokens;
for (int i = 0; i < argc; i++) {
tokens.push_back(argv[i]);
}
Parse(&tokens);
}
// Parses flags out of the given token list. Recognizes forms: // Parses flags out of the given token list. Recognizes forms:
// --flag=value or --flag value // --flag=value or --flag value
// Any token not recognized as a flag is left in `leftover`. // Any token not recognized as a flag is left in `leftover`.