mirror of
https://github.com/keepassxreboot/keepassxc.git
synced 2024-12-28 08:49:42 -05:00
Fix CLI help and version options. (#650)
* Correct handling of --help and --version * Moving arguments building up. * Only manipulating argv is the command is valid. * Not a failure when --help * Not using showVersion()
This commit is contained in:
parent
8d70167acf
commit
344235b1e1
@ -20,6 +20,7 @@
|
|||||||
#include <QCommandLineParser>
|
#include <QCommandLineParser>
|
||||||
#include <QCoreApplication>
|
#include <QCoreApplication>
|
||||||
#include <QStringList>
|
#include <QStringList>
|
||||||
|
#include <QTextStream>
|
||||||
|
|
||||||
#include <cli/Clip.h>
|
#include <cli/Clip.h>
|
||||||
#include <cli/EntropyMeter.h>
|
#include <cli/EntropyMeter.h>
|
||||||
@ -47,6 +48,10 @@ int main(int argc, char** argv)
|
|||||||
return EXIT_FAILURE;
|
return EXIT_FAILURE;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
QStringList arguments;
|
||||||
|
for (int i = 0; i < argc; ++i) {
|
||||||
|
arguments << QString(argv[i]);
|
||||||
|
}
|
||||||
QCommandLineParser parser;
|
QCommandLineParser parser;
|
||||||
|
|
||||||
QString description("KeePassXC command line interface.");
|
QString description("KeePassXC command line interface.");
|
||||||
@ -63,50 +68,66 @@ int main(int argc, char** argv)
|
|||||||
|
|
||||||
parser.addHelpOption();
|
parser.addHelpOption();
|
||||||
parser.addVersionOption();
|
parser.addVersionOption();
|
||||||
// TODO : use process once the setOptionsAfterPositionalArgumentsMode (Qt 5.6)
|
// TODO : use the setOptionsAfterPositionalArgumentsMode (Qt 5.6) function
|
||||||
// is available. Until then, options passed to sub-commands won't be
|
// when available. Until then, options passed to sub-commands won't be
|
||||||
// recognized by this parser.
|
// recognized by this parser.
|
||||||
// parser.process(app);
|
parser.parse(arguments);
|
||||||
|
|
||||||
if (argc < 2) {
|
if (parser.positionalArguments().size() < 1) {
|
||||||
QCoreApplication app(argc, argv);
|
QCoreApplication app(argc, argv);
|
||||||
app.setApplicationVersion(KEEPASSX_VERSION);
|
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();
|
||||||
|
return EXIT_SUCCESS;
|
||||||
|
}
|
||||||
parser.showHelp();
|
parser.showHelp();
|
||||||
return EXIT_FAILURE;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
QString commandName = argv[1];
|
QString commandName = parser.positionalArguments().at(0);
|
||||||
|
|
||||||
// Removing the first cli argument before dispatching.
|
|
||||||
++argv;
|
|
||||||
--argc;
|
|
||||||
|
|
||||||
int exitCode = EXIT_FAILURE;
|
int exitCode = EXIT_FAILURE;
|
||||||
|
|
||||||
if (commandName == "clip") {
|
if (commandName == "clip") {
|
||||||
|
// Removing the first cli argument before dispatching.
|
||||||
|
++argv;
|
||||||
|
--argc;
|
||||||
argv[0] = const_cast<char*>("keepassxc-cli clip");
|
argv[0] = const_cast<char*>("keepassxc-cli clip");
|
||||||
exitCode = Clip::execute(argc, argv);
|
exitCode = Clip::execute(argc, argv);
|
||||||
} else if (commandName == "entropy-meter") {
|
} else if (commandName == "entropy-meter") {
|
||||||
|
++argv;
|
||||||
|
--argc;
|
||||||
argv[0] = const_cast<char*>("keepassxc-cli entropy-meter");
|
argv[0] = const_cast<char*>("keepassxc-cli entropy-meter");
|
||||||
exitCode = EntropyMeter::execute(argc, argv);
|
exitCode = EntropyMeter::execute(argc, argv);
|
||||||
} else if (commandName == "extract") {
|
} else if (commandName == "extract") {
|
||||||
|
++argv;
|
||||||
|
--argc;
|
||||||
argv[0] = const_cast<char*>("keepassxc-cli extract");
|
argv[0] = const_cast<char*>("keepassxc-cli extract");
|
||||||
exitCode = Extract::execute(argc, argv);
|
exitCode = Extract::execute(argc, argv);
|
||||||
} else if (commandName == "list") {
|
} else if (commandName == "list") {
|
||||||
|
++argv;
|
||||||
|
--argc;
|
||||||
argv[0] = const_cast<char*>("keepassxc-cli list");
|
argv[0] = const_cast<char*>("keepassxc-cli list");
|
||||||
exitCode = List::execute(argc, argv);
|
exitCode = List::execute(argc, argv);
|
||||||
} else if (commandName == "merge") {
|
} else if (commandName == "merge") {
|
||||||
|
++argv;
|
||||||
|
--argc;
|
||||||
argv[0] = const_cast<char*>("keepassxc-cli merge");
|
argv[0] = const_cast<char*>("keepassxc-cli merge");
|
||||||
exitCode = Merge::execute(argc, argv);
|
exitCode = Merge::execute(argc, argv);
|
||||||
} else if (commandName == "show") {
|
} else if (commandName == "show") {
|
||||||
|
++argv;
|
||||||
|
--argc;
|
||||||
argv[0] = const_cast<char*>("keepassxc-cli show");
|
argv[0] = const_cast<char*>("keepassxc-cli show");
|
||||||
exitCode = Show::execute(argc, argv);
|
exitCode = Show::execute(argc, argv);
|
||||||
} else {
|
} else {
|
||||||
qCritical("Invalid command %s.", qPrintable(commandName));
|
qCritical("Invalid command %s.", qPrintable(commandName));
|
||||||
QCoreApplication app(argc, argv);
|
QCoreApplication app(argc, argv);
|
||||||
app.setApplicationVersion(KEEPASSX_VERSION);
|
app.setApplicationVersion(KEEPASSX_VERSION);
|
||||||
parser.showHelp();
|
// showHelp exits the application immediately, so we need to set the
|
||||||
exitCode = EXIT_FAILURE;
|
// exit code here.
|
||||||
|
parser.showHelp(EXIT_FAILURE);
|
||||||
}
|
}
|
||||||
|
|
||||||
#if defined(WITH_ASAN) && defined(WITH_LSAN)
|
#if defined(WITH_ASAN) && defined(WITH_LSAN)
|
||||||
|
Loading…
Reference in New Issue
Block a user