Add a CLI option to list elements recursively (#2345)

This commit is contained in:
vi-n 2018-10-03 19:18:34 +02:00 committed by Janek Bevendorff
parent aa7216b9d9
commit 4ff63c2bf5
2 changed files with 13 additions and 6 deletions

View File

@ -50,6 +50,11 @@ int List::execute(const QStringList& arguments)
QObject::tr("Key file of the database."),
QObject::tr("path"));
parser.addOption(keyFile);
QCommandLineOption recursiveOption(QStringList() << "R"
<< "recursive",
QObject::tr("Recursive mode, list elements recursively"));
parser.addOption(recursiveOption);
parser.process(arguments);
const QStringList args = parser.positionalArguments();
@ -58,22 +63,24 @@ int List::execute(const QStringList& arguments)
return EXIT_FAILURE;
}
bool recursive = parser.isSet(recursiveOption);
Database* db = Database::unlockFromStdin(args.at(0), parser.value(keyFile));
if (db == nullptr) {
return EXIT_FAILURE;
}
if (args.size() == 2) {
return this->listGroup(db, args.at(1));
return this->listGroup(db, recursive, args.at(1));
}
return this->listGroup(db);
return this->listGroup(db, recursive);
}
int List::listGroup(Database* database, QString groupPath)
int List::listGroup(Database* database, bool recursive, QString groupPath)
{
QTextStream outputTextStream(stdout, QIODevice::WriteOnly);
if (groupPath.isEmpty()) {
outputTextStream << database->rootGroup()->print();
outputTextStream << database->rootGroup()->print(recursive);
outputTextStream.flush();
return EXIT_SUCCESS;
}
@ -84,7 +91,7 @@ int List::listGroup(Database* database, QString groupPath)
return EXIT_FAILURE;
}
outputTextStream << group->print();
outputTextStream << group->print(recursive);
outputTextStream.flush();
return EXIT_SUCCESS;
}

View File

@ -26,7 +26,7 @@ public:
List();
~List();
int execute(const QStringList& arguments);
int listGroup(Database* database, QString groupPath = QString(""));
int listGroup(Database* database, bool recursive, QString groupPath = QString(""));
};
#endif // KEEPASSXC_LIST_H