From 5e68cd2fa2736d7e4be30c5e660fc4061975b49a Mon Sep 17 00:00:00 2001 From: louib Date: Sun, 1 Aug 2021 13:38:20 -0400 Subject: [PATCH] Abort CLI open on error --- src/cli/Command.cpp | 2 ++ src/cli/keepassxc-cli.cpp | 11 +++++++---- 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/src/cli/Command.cpp b/src/cli/Command.cpp index ceb80c11..f0309192 100644 --- a/src/cli/Command.cpp +++ b/src/cli/Command.cpp @@ -138,10 +138,12 @@ QSharedPointer Command::getCommandLineParser(const QStringLi return {}; } if (parser->positionalArguments().size() < positionalArguments.size()) { + err << QObject::tr("Missing positional argument(s).") << "\n\n"; err << getHelpText(); return {}; } if (parser->positionalArguments().size() > (positionalArguments.size() + optionalArguments.size())) { + err << QObject::tr("Too many arguments provided.") << "\n\n"; err << getHelpText(); return {}; } diff --git a/src/cli/keepassxc-cli.cpp b/src/cli/keepassxc-cli.cpp index 301e8b25..120c7064 100644 --- a/src/cli/keepassxc-cli.cpp +++ b/src/cli/keepassxc-cli.cpp @@ -109,7 +109,7 @@ private: }; #endif -void enterInteractiveMode(const QStringList& arguments) +int enterInteractiveMode(const QStringList& arguments) { auto& err = Utils::STDERR; // Replace command list with interactive version @@ -118,7 +118,9 @@ void enterInteractiveMode(const QStringList& arguments) Open openCmd; QStringList openArgs(arguments); openArgs.removeFirst(); - openCmd.execute(openArgs); + if (openCmd.execute(openArgs) != EXIT_SUCCESS) { + return EXIT_FAILURE; + }; QScopedPointer reader; #if defined(USE_READLINE) @@ -165,6 +167,8 @@ void enterInteractiveMode(const QStringList& arguments) if (currentDatabase) { currentDatabase->releaseData(); } + + return EXIT_SUCCESS; } int main(int argc, char** argv) @@ -224,8 +228,7 @@ int main(int argc, char** argv) QString commandName = parser.positionalArguments().at(0); if (commandName == "open") { - enterInteractiveMode(arguments); - return EXIT_SUCCESS; + return enterInteractiveMode(arguments); } auto command = Commands::getCommand(commandName);