Use QCommandLineParser instead of by-hand parsing.

This will be more flexible for future CLI options
This commit is contained in:
Gavin Bisesi 2015-08-22 09:39:56 -04:00 committed by Fabio Bas
parent 36470c5061
commit 089acfd84a
2 changed files with 33 additions and 9 deletions

View file

@ -24,6 +24,8 @@
#include <iostream>
#include <QMetaType>
#include <QDateTime>
#include <QCommandLineParser>
#include "passwordhasher.h"
#include "servatrice.h"
#include "server_logger.h"
@ -34,6 +36,7 @@
#include "version_string.h"
#include <google/protobuf/stubs/common.h>
RNG_Abstract *rng;
ServerLogger *logger;
QThread *loggerThread;
@ -111,15 +114,36 @@ int main(int argc, char *argv[])
QCoreApplication app(argc, argv);
app.setOrganizationName("Cockatrice");
app.setApplicationName("Servatrice");
QStringList args = app.arguments();
bool testRandom = args.contains("--test-random");
bool testHashFunction = args.contains("--test-hash");
bool logToConsole = args.contains("--log-to-console");
app.setApplicationVersion(VERSION_STRING);
bool testRandom = false;
bool testHashFunction = false;
bool logToConsole = false;
QString configPath;
int hasConfigPath=args.indexOf("--config");
if(hasConfigPath > -1 && args.count() > hasConfigPath + 1)
configPath = args.at(hasConfigPath + 1);
QCommandLineParser parser;
parser.addHelpOption();
parser.addVersionOption();
QCommandLineOption testRandomOpt("test-random", "Test PRNG (chi^2)");
parser.addOption(testRandomOpt);
QCommandLineOption testHashFunctionOpt("test-hash", "Test password hash function");
parser.addOption(testHashFunctionOpt);
QCommandLineOption logToConsoleOpt("log-to-console", "Write server logs to console");
parser.addOption(logToConsoleOpt);
QCommandLineOption configPathOpt("config", "Read server configuration from <file>", "file", "");
parser.addOption(configPathOpt);
parser.process(app);
testRandom = parser.isSet(testRandomOpt);
testHashFunction = parser.isSet(testHashFunctionOpt);
logToConsole = parser.isSet(logToConsoleOpt);
configPath = parser.value(configPathOpt);
qRegisterMetaType<QList<int> >("QList<int>");

View file

@ -32,6 +32,6 @@ QString SettingsCache::guessConfigurationPath(QString & specificPath)
return guessFileName;
#endif
guessFileName = QStandardPaths::writableLocation(QStandardPaths::DataLocation) + "/" + fileName;
guessFileName = QStandardPaths::writableLocation(QStandardPaths::DataLocation) + "/" + fileName;
return guessFileName;
}