impr: Rework command line argument parsing (#2440)

The current CLI argument parsing did not work as documented and had a
number of issues related to multi-flag (subcommand) parsing. I've
reworked the logic in such a way that should maintain full compatibility
with any existing scripts/use-cases but with added functionality.

---------

Co-authored-by: Nik <werwolv98@gmail.com>
This commit is contained in:
Zackary Newman
2025-09-12 15:07:33 -04:00
committed by GitHub
parent 6169078c04
commit dcbba9cbfc
2 changed files with 65 additions and 56 deletions

View File

@@ -99,29 +99,22 @@ namespace hex::plugin::builtin {
}
std::vector<std::string> fullPaths;
bool doubleDashFound = false;
for (auto &arg : args) {
for (const auto &arg : args) {
try {
std::fs::path path;
// Skip the first argument named `--`
if (arg == "--" && !doubleDashFound) {
doubleDashFound = true;
} else {
try {
std::fs::path path;
try {
path = std::fs::weakly_canonical(arg);
} catch(std::fs::filesystem_error &) {
path = arg;
}
if (path.empty())
continue;
fullPaths.push_back(wolv::util::toUTF8String(path));
} catch (std::exception &e) {
log::error("Failed to open file '{}'\n {}", arg, e.what());
path = std::fs::weakly_canonical(arg);
} catch(std::fs::filesystem_error &) {
path = arg;
}
if (path.empty())
continue;
fullPaths.push_back(wolv::util::toUTF8String(path));
} catch (std::exception &e) {
log::error("Failed to open file '{}'\n {}", arg, e.what());
}
}
@@ -541,9 +534,6 @@ namespace hex::plugin::builtin {
void registerCommandForwarders() {
hex::subcommands::registerSubCommand("open", [](const std::vector<std::string> &args){
for (auto &arg : args) {
if (arg.starts_with("--"))
break;
RequestOpenFile::post(arg);
}
});
@@ -586,4 +576,4 @@ namespace hex::plugin::builtin {
});
}
}
}