21
#include "AttachmentExport.h"
22
#include "AttachmentImport.h"
23
#include "AttachmentRemove.h"
26
#include "DatabaseCreate.h"
27
#include "DatabaseEdit.h"
28
#include "DatabaseInfo.h"
42
#include "RemoveGroup.h"
47
#include <QCommandLineParser>
49
#include <QRegularExpression>
51
const QCommandLineOption Command::HelpOption = QCommandLineOption(QStringList()
53
<< QStringLiteral("?")
55
<< QStringLiteral("h") << QStringLiteral("help"),
56
QObject::tr("Display this help."));
58
const QCommandLineOption Command::QuietOption =
59
QCommandLineOption(QStringList() << "q"
61
QObject::tr("Silence password prompt and other secondary outputs."));
63
const QCommandLineOption Command::KeyFileOption = QCommandLineOption(QStringList() << "k"
65
QObject::tr("Key file of the database."),
68
const QCommandLineOption Command::NoPasswordOption =
69
QCommandLineOption(QStringList() << "no-password", QObject::tr("Deactivate password key for the database."));
71
const QCommandLineOption Command::YubiKeyOption =
72
QCommandLineOption(QStringList() << "y"
74
QObject::tr("Yubikey slot and optional serial used to access the database (e.g., 1:7370001)."),
75
QObject::tr("slot[:serial]"));
80
QSharedPointer<QCommandLineParser> buildParser(Command* command)
82
auto parser = QSharedPointer<QCommandLineParser>(new QCommandLineParser());
83
parser->setApplicationDescription(command->description);
84
for (const CommandLineArgument& positionalArgument : command->positionalArguments) {
85
parser->addPositionalArgument(
86
positionalArgument.name, positionalArgument.description, positionalArgument.syntax);
88
for (const CommandLineArgument& optionalArgument : command->optionalArguments) {
89
parser->addPositionalArgument(optionalArgument.name, optionalArgument.description, optionalArgument.syntax);
91
for (const QCommandLineOption& option : command->options) {
92
parser->addOption(option);
94
parser->addOption(Command::HelpOption);
101
: currentDatabase(nullptr)
103
options.append(Command::QuietOption);
106
Command::~Command() = default;
108
QString Command::getDescriptionLine()
110
QString response = name;
112
QString spaces = space.repeated(20 - name.length());
113
response = response.append(spaces);
114
response = response.append(description);
115
response = response.append("\n");
119
QString Command::getHelpText()
121
auto help = buildParser(this)->helpText();
123
help.replace(QStringLiteral("[options]"), name + QStringLiteral(" [options]"));
125
auto appname = QFileInfo(QCoreApplication::applicationFilePath()).fileName();
126
auto regex = QRegularExpression(QStringLiteral(" .*%1").arg(QRegularExpression::escape(appname)));
127
help.replace(regex, appname.prepend(" "));
132
QSharedPointer<QCommandLineParser> Command::getCommandLineParser(const QStringList& arguments)
134
auto& err = Utils::STDERR;
135
QSharedPointer<QCommandLineParser> parser = buildParser(this);
137
if (!parser->parse(arguments)) {
138
err << parser->errorText() << "\n\n";
139
err << getHelpText();
142
if (parser->positionalArguments().size() < positionalArguments.size()) {
143
err << QObject::tr("Missing positional argument(s).") << "\n\n";
144
err << getHelpText();
147
if (parser->positionalArguments().size() > (positionalArguments.size() + optionalArguments.size())) {
148
err << QObject::tr("Too many arguments provided.") << "\n\n";
149
err << getHelpText();
152
if (parser->isSet(HelpOption)) {
153
err << getHelpText();
161
QMap<QString, QSharedPointer<Command>> s_commands;
163
void setupCommands(bool interactive)
167
s_commands.insert(QStringLiteral("add"), QSharedPointer<Command>(new Add()));
168
s_commands.insert(QStringLiteral("analyze"), QSharedPointer<Command>(new Analyze()));
169
s_commands.insert(QStringLiteral("attachment-export"), QSharedPointer<Command>(new AttachmentExport()));
170
s_commands.insert(QStringLiteral("attachment-import"), QSharedPointer<Command>(new AttachmentImport()));
171
s_commands.insert(QStringLiteral("attachment-rm"), QSharedPointer<Command>(new AttachmentRemove()));
172
s_commands.insert(QStringLiteral("clip"), QSharedPointer<Command>(new Clip()));
173
s_commands.insert(QStringLiteral("close"), QSharedPointer<Command>(new Close()));
174
s_commands.insert(QStringLiteral("db-create"), QSharedPointer<Command>(new DatabaseCreate()));
175
s_commands.insert(QStringLiteral("db-edit"), QSharedPointer<Command>(new DatabaseEdit()));
176
s_commands.insert(QStringLiteral("db-info"), QSharedPointer<Command>(new DatabaseInfo()));
177
s_commands.insert(QStringLiteral("diceware"), QSharedPointer<Command>(new Diceware()));
178
s_commands.insert(QStringLiteral("edit"), QSharedPointer<Command>(new Edit()));
179
s_commands.insert(QStringLiteral("estimate"), QSharedPointer<Command>(new Estimate()));
180
s_commands.insert(QStringLiteral("generate"), QSharedPointer<Command>(new Generate()));
181
s_commands.insert(QStringLiteral("help"), QSharedPointer<Command>(new Help()));
182
s_commands.insert(QStringLiteral("ls"), QSharedPointer<Command>(new List()));
183
s_commands.insert(QStringLiteral("merge"), QSharedPointer<Command>(new Merge()));
184
s_commands.insert(QStringLiteral("mkdir"), QSharedPointer<Command>(new AddGroup()));
185
s_commands.insert(QStringLiteral("mv"), QSharedPointer<Command>(new Move()));
186
s_commands.insert(QStringLiteral("open"), QSharedPointer<Command>(new Open()));
187
s_commands.insert(QStringLiteral("rm"), QSharedPointer<Command>(new Remove()));
188
s_commands.insert(QStringLiteral("rmdir"), QSharedPointer<Command>(new RemoveGroup()));
189
s_commands.insert(QStringLiteral("search"), QSharedPointer<Command>(new Search()));
190
s_commands.insert(QStringLiteral("show"), QSharedPointer<Command>(new Show()));
193
s_commands.insert(QStringLiteral("exit"), QSharedPointer<Command>(new Exit("exit")));
194
s_commands.insert(QStringLiteral("quit"), QSharedPointer<Command>(new Exit("quit")));
196
s_commands.insert(QStringLiteral("export"), QSharedPointer<Command>(new Export()));
197
s_commands.insert(QStringLiteral("import"), QSharedPointer<Command>(new Import()));
201
QList<QSharedPointer<Command>> getCommands()
203
return s_commands.values();
206
QSharedPointer<Command> getCommand(const QString& commandName)
208
return s_commands.value(commandName);