diff options
Diffstat (limited to 'src/cli/keepassxc-cli.cpp')
-rw-r--r-- | src/cli/keepassxc-cli.cpp | 74 |
1 files changed, 18 insertions, 56 deletions
diff --git a/src/cli/keepassxc-cli.cpp b/src/cli/keepassxc-cli.cpp index 18bb224ec..1462f92b9 100644 --- a/src/cli/keepassxc-cli.cpp +++ b/src/cli/keepassxc-cli.cpp @@ -22,12 +22,7 @@ #include <QStringList> #include <QTextStream> -#include <cli/Clip.h> -#include <cli/EntropyMeter.h> -#include <cli/Extract.h> -#include <cli/List.h> -#include <cli/Merge.h> -#include <cli/Show.h> +#include <cli/Command.h> #include "config-keepassx.h" #include "core/Tools.h" @@ -48,6 +43,10 @@ int main(int argc, char** argv) return EXIT_FAILURE; } + QCoreApplication app(argc, argv); + app.setApplicationVersion(KEEPASSX_VERSION); + + QTextStream out(stdout); QStringList arguments; for (int i = 0; i < argc; ++i) { arguments << QString(argv[i]); @@ -55,16 +54,13 @@ int main(int argc, char** argv) QCommandLineParser parser; QString description("KeePassXC command line interface."); - description = description.append(QString("\n\nAvailable commands:")); - description = description.append(QString("\n clip\t\tCopy a password to the clipboard.")); - description = description.append(QString("\n extract\tExtract and print the content of a database.")); - description = description.append(QString("\n entropy-meter\tCalculate password entropy.")); - description = description.append(QString("\n list\t\tList database entries.")); - description = description.append(QString("\n merge\t\tMerge two databases.")); - description = description.append(QString("\n show\t\tShow a password.")); - parser.setApplicationDescription(QCoreApplication::translate("main", qPrintable(description))); + description = description.append(QObject::tr("\n\nAvailable commands:\n")); + for (Command* command : Command::getCommands()) { + description = description.append(command->getDescriptionLine()); + } + parser.setApplicationDescription(description); - parser.addPositionalArgument("command", QCoreApplication::translate("main", "Name of the command to execute.")); + parser.addPositionalArgument("command", QObject::tr("Name of the command to execute.")); parser.addHelpOption(); parser.addVersionOption(); @@ -74,62 +70,28 @@ int main(int argc, char** argv) parser.parse(arguments); if (parser.positionalArguments().size() < 1) { - QCoreApplication app(argc, argv); - app.setApplicationVersion(KEEPASSX_VERSION); if (parser.isSet("version")) { // Switch to parser.showVersion() when available (QT 5.4). - QTextStream out(stdout); - out << KEEPASSX_VERSION << "\n"; - out.flush(); + out << KEEPASSX_VERSION << endl; return EXIT_SUCCESS; } parser.showHelp(); } QString commandName = parser.positionalArguments().at(0); + Command* command = Command::getCommand(commandName); - int exitCode = EXIT_FAILURE; - - if (commandName == "clip") { - // Removing the first cli argument before dispatching. - ++argv; - --argc; - argv[0] = const_cast<char*>("keepassxc-cli clip"); - exitCode = Clip::execute(argc, argv); - } else if (commandName == "entropy-meter") { - ++argv; - --argc; - argv[0] = const_cast<char*>("keepassxc-cli entropy-meter"); - exitCode = EntropyMeter::execute(argc, argv); - } else if (commandName == "extract") { - ++argv; - --argc; - argv[0] = const_cast<char*>("keepassxc-cli extract"); - exitCode = Extract::execute(argc, argv); - } else if (commandName == "list") { - ++argv; - --argc; - argv[0] = const_cast<char*>("keepassxc-cli list"); - exitCode = List::execute(argc, argv); - } else if (commandName == "merge") { - ++argv; - --argc; - argv[0] = const_cast<char*>("keepassxc-cli merge"); - exitCode = Merge::execute(argc, argv); - } else if (commandName == "show") { - ++argv; - --argc; - argv[0] = const_cast<char*>("keepassxc-cli show"); - exitCode = Show::execute(argc, argv); - } else { + if (command == nullptr) { qCritical("Invalid command %s.", qPrintable(commandName)); - QCoreApplication app(argc, argv); - app.setApplicationVersion(KEEPASSX_VERSION); // showHelp exits the application immediately, so we need to set the // exit code here. parser.showHelp(EXIT_FAILURE); } + // Removing the first argument (keepassxc). + arguments.removeFirst(); + int exitCode = command->execute(arguments); + #if defined(WITH_ASAN) && defined(WITH_LSAN) // do leak check here to prevent massive tail of end-of-process leak errors from third-party libraries __lsan_do_leak_check(); |