mirror of
https://github.com/WerWolv/ImHex.git
synced 2026-03-28 15:57:03 -05:00
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:
@@ -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 {
|
||||
});
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user