From 4aad1940a0865895a861a23d61d09009e722a9c3 Mon Sep 17 00:00:00 2001 From: kalaposfos13 <153381648+kalaposfos13@users.noreply.github.com> Date: Mon, 13 Jan 2025 07:42:04 +0100 Subject: [PATCH] Fix -g flag with --; and avoid overindexing if -- is set but no arguments are added --- src/main.cpp | 21 +++++++++++++++++---- src/qt_gui/main.cpp | 21 +++++++++++++++++---- 2 files changed, 34 insertions(+), 8 deletions(-) diff --git a/src/main.cpp b/src/main.cpp index b427c72ab..cf4eb3e22 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -127,19 +127,32 @@ int main(int argc, char* argv[]) { auto it = arg_map.find(cur_arg); if (it != arg_map.end()) { it->second(i); // Call the associated lambda function + } else if (i == argc - 1 && !has_game_argument) { + // Assume the last argument is the game file if not specified via -g/--game + game_path = argv[i]; + has_game_argument = true; + } else if (std::string(argv[i]) == "--") { + if (i + 1 == argc) { + std::cerr << "Warning: -- is set, but no game arguments are added!\n"; + break; + } + for (int j = i + 1; j < argc; j++) { + game_args.push_back(argv[j]); + } + break; } else if (std::string(argv[i + 1]) == "--") { if (!has_game_argument) { game_path = argv[i]; has_game_argument = true; } + if (i + 2 == argc) { + std::cerr << "Warning: -- is set, but no game arguments are added!\n"; + break; + } for (int j = i + 2; j < argc; j++) { game_args.push_back(argv[j]); } break; - } else if (i == argc - 1 && !has_game_argument) { - // Assume the last argument is the game file if not specified via -g/--game - game_path = argv[i]; - has_game_argument = true; } else { std::cerr << "Unknown argument: " << cur_arg << ", see --help for info.\n"; return 1; diff --git a/src/qt_gui/main.cpp b/src/qt_gui/main.cpp index 88ea8c95d..1cb1b9e0f 100644 --- a/src/qt_gui/main.cpp +++ b/src/qt_gui/main.cpp @@ -131,19 +131,32 @@ int main(int argc, char* argv[]) { auto it = arg_map.find(cur_arg); if (it != arg_map.end()) { it->second(i); // Call the associated lambda function + } else if (i == argc - 1 && !has_game_argument) { + // Assume the last argument is the game file if not specified via -g/--game + game_path = argv[i]; + has_game_argument = true; + } else if (std::string(argv[i]) == "--") { + if (i + 1 == argc) { + std::cerr << "Warning: -- is set, but no game arguments are added!\n"; + break; + } + for (int j = i + 1; j < argc; j++) { + game_args.push_back(argv[j]); + } + break; } else if (std::string(argv[i + 1]) == "--") { if (!has_game_argument) { game_path = argv[i]; has_game_argument = true; } + if (i + 2 == argc) { + std::cerr << "Warning: -- is set, but no game arguments are added!\n"; + break; + } for (int j = i + 2; j < argc; j++) { game_args.push_back(argv[j]); } break; - } else if (i == argc - 1 && !has_game_argument) { - // Assume the last argument is the game file if not specified via -g/--game - game_path = argv[i]; - has_game_argument = true; } else { std::cerr << "Unknown argument: " << cur_arg << ", see --help for info.\n"; return 1;