CLI Command cleanup

This PR cleans up the `Command` classes in the CLI, introducing a
`DatabaseCommand` class for the commands operating on a database,
and a `getCommandLineParser` command to centralize the arguments
parsing and validation.

The opening of the database based on the CLI arguments and options
is now centralized in `DatabaseCommand.execute`, making it easy to
add new database opening features (like YubiKey support for the CLI).

Also a couple of bugs fixed:
  * `Create` was still using `stdout` for some error messages.
  * `Diceware` and `Generate` were not validating that the word count was an integer.
  * `Diceware` was also using `stdout` for some error messages.
This commit is contained in:
louib 2019-06-01 17:53:40 -04:00 committed by Jonathan White
parent 3cf171cbf5
commit 04360ed552
31 changed files with 591 additions and 637 deletions

View file

@ -1,5 +1,5 @@
/*
* Copyright (C) 2017 KeePassXC Team <team@keepassxc.org>
* Copyright (C) 2019 KeePassXC Team <team@keepassxc.org>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
@ -35,6 +35,8 @@
#include "Merge.h"
#include "Remove.h"
#include "Show.h"
#include "TextStream.h"
#include "Utils.h"
const QCommandLineOption Command::QuietOption =
QCommandLineOption(QStringList() << "q"
@ -51,13 +53,17 @@ const QCommandLineOption Command::NoPasswordOption =
QMap<QString, Command*> commands;
Command::Command()
{
options.append(Command::QuietOption);
}
Command::~Command()
{
}
QString Command::getDescriptionLine()
{
QString response = name;
QString space(" ");
QString spaces = space.repeated(15 - name.length());
@ -67,6 +73,36 @@ QString Command::getDescriptionLine()
return response;
}
QSharedPointer<QCommandLineParser> Command::getCommandLineParser(const QStringList& arguments)
{
TextStream errorTextStream(Utils::STDERR, QIODevice::WriteOnly);
QSharedPointer<QCommandLineParser> parser = QSharedPointer<QCommandLineParser>(new QCommandLineParser());
parser->setApplicationDescription(description);
for (CommandLineArgument positionalArgument : positionalArguments) {
parser->addPositionalArgument(
positionalArgument.name, positionalArgument.description, positionalArgument.syntax);
}
for (CommandLineArgument optionalArgument : optionalArguments) {
parser->addPositionalArgument(optionalArgument.name, optionalArgument.description, optionalArgument.syntax);
}
for (QCommandLineOption option : options) {
parser->addOption(option);
}
parser->addHelpOption();
parser->process(arguments);
if (parser->positionalArguments().size() < positionalArguments.size()) {
errorTextStream << parser->helpText().replace("[options]", name.append(" [options]"));
return QSharedPointer<QCommandLineParser>(nullptr);
}
if (parser->positionalArguments().size() > (positionalArguments.size() + optionalArguments.size())) {
errorTextStream << parser->helpText().replace("[options]", name.append(" [options]"));
return QSharedPointer<QCommandLineParser>(nullptr);
}
return parser;
}
void populateCommands()
{
if (commands.isEmpty()) {