diff --git a/lib/libimhex/source/subcommands/subcommands.cpp b/lib/libimhex/source/subcommands/subcommands.cpp index 3841ec137..56373b04c 100644 --- a/lib/libimhex/source/subcommands/subcommands.cpp +++ b/lib/libimhex/source/subcommands/subcommands.cpp @@ -13,6 +13,9 @@ namespace hex::subcommands { std::optional findSubCommand(const std::string &arg) { + if (arg == "-" || arg == "--") + return std::nullopt; + for (auto &plugin : PluginManager::getPlugins()) { for (auto &subCommand : plugin.getSubCommands()) { if (fmt::format("--{}", subCommand.commandLong) == arg || fmt::format("-{}", subCommand.commandShort) == arg) { @@ -35,46 +38,62 @@ namespace hex::subcommands { auto argsIter = args.begin(); - // Get subcommand associated with the first argument - std::optional currentSubCommand = findSubCommand(*argsIter); - - if (currentSubCommand) { - argsIter += 1; - // If it is a valid subcommand, remove it from the argument list - } else { - // If no (valid) subcommand was provided, the default one is --open - currentSubCommand = findSubCommand("--open"); - } - - // Arguments of the current subcommand + std::optional currentSubCommand; std::vector currentSubCommandArgs; - // Compute all subcommands to run - while (argsIter != args.end()) { - const std::string &arg = *argsIter; + if (*argsIter == "--") { + // Treat the rest of the args as files + ++argsIter; + std::vector remainingArgs(argsIter, args.end()); + subCommands.emplace_back(*findSubCommand("--open"), remainingArgs); + argsIter = args.end(); // Skip while loop + } else if (!findSubCommand(*argsIter)) { + // First argument not a subcommand, treat all args as files + subCommands.emplace_back(*findSubCommand("--open"), args); + argsIter = args.end(); // Skip while loop + } - if (!currentSubCommandArgs.empty() && arg.starts_with("--") && !(currentSubCommand.has_value() && currentSubCommand->type == SubCommand::Type::SubCommand)) { - // Save command to run - if (currentSubCommand) { + while (argsIter != args.end()) { + // Get subcommand associated with the argument + // Guaranteed to find a match on the first argument, as the other case has been handled above + const auto newSubCommand = findSubCommand(*argsIter); + if (*argsIter == "--") { + // Treat the rest of the args as files + subCommands.emplace_back(*currentSubCommand, currentSubCommandArgs); + + ++argsIter; + std::vector remainingArgs(argsIter, args.end()); + + subCommands.emplace_back(*findSubCommand("--open"), remainingArgs); + + currentSubCommand = std::nullopt; + currentSubCommandArgs.clear(); + break; + } + + // Will always take this `if` statement on the first time through the loop + if (newSubCommand.has_value()) { + if (currentSubCommand.has_value()) { subCommands.emplace_back(*currentSubCommand, currentSubCommandArgs); } - currentSubCommand = std::nullopt; - currentSubCommandArgs = { }; - } else if (currentSubCommand) { - // Add current argument to the current command - currentSubCommandArgs.push_back(arg); - argsIter += 1; - } else { - // Get next subcommand from current argument - currentSubCommand = findSubCommand(arg); - if (!currentSubCommand) { - log::error("No subcommand named '{}' found", arg); - exit(EXIT_FAILURE); + if (newSubCommand->type == SubCommand::Type::SubCommand) { + ++argsIter; + std::vector remainingArgs(argsIter, args.end()); + subCommands.emplace_back(*newSubCommand, remainingArgs); + + currentSubCommand = std::nullopt; + currentSubCommandArgs.clear(); + break; } - argsIter += 1; + currentSubCommand = newSubCommand; + currentSubCommandArgs.clear(); + } else { + currentSubCommandArgs.push_back(*argsIter); } + + ++argsIter; } // Save last command to run @@ -83,7 +102,7 @@ namespace hex::subcommands { } // Run the subcommands - for (auto &[subcommand, subCommandArgs] : subCommands) { + for (const auto &[subcommand, subCommandArgs] : subCommands) { subcommand.callback(subCommandArgs); } diff --git a/plugins/builtin/source/content/command_line_interface.cpp b/plugins/builtin/source/content/command_line_interface.cpp index 60ef12067..45ac4aa85 100644 --- a/plugins/builtin/source/content/command_line_interface.cpp +++ b/plugins/builtin/source/content/command_line_interface.cpp @@ -99,29 +99,22 @@ namespace hex::plugin::builtin { } std::vector 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 &args){ for (auto &arg : args) { - if (arg.starts_with("--")) - break; - RequestOpenFile::post(arg); } }); @@ -586,4 +576,4 @@ namespace hex::plugin::builtin { }); } -} \ No newline at end of file +}