YACReader: Port commandline to QCommandLineParser

This commit is contained in:
Felix Kauselmann 2018-02-16 15:31:32 +01:00
parent 9103ee0c0c
commit afab73cbc6

View File

@ -1,6 +1,7 @@
#include <QApplication> #include <QApplication>
#include <QDir> #include <QDir>
#include <QTranslator> #include <QTranslator>
#include <QCommandLineParser>
#include "main_window_viewer.h" #include "main_window_viewer.h"
#include "configuration.h" #include "configuration.h"
@ -12,11 +13,11 @@
using namespace QsLogging; using namespace QsLogging;
#if defined(WIN32) && defined(_DEBUG) #if defined(WIN32) && defined(_DEBUG)
#define _CRTDBG_MAP_ALLOC #define _CRTDBG_MAP_ALLOC
#include <stdlib.h> #include <stdlib.h>
#include <crtdbg.h> #include <crtdbg.h>
#define DEBUG_NEW new( _NORMAL_BLOCK, __FILE__, __LINE__ ) #define DEBUG_NEW new( _NORMAL_BLOCK, __FILE__, __LINE__ )
#define new DEBUG_NEW #define new DEBUG_NEW
#endif #endif
#ifdef Q_OS_MAC #ifdef Q_OS_MAC
@ -53,106 +54,83 @@ class YACReaderApplication: public QApplication
int main(int argc, char * argv[]) int main(int argc, char * argv[])
{ {
#if defined(_MSC_VER) && defined(_DEBUG) #if defined(_MSC_VER) && defined(_DEBUG)
_CrtSetDbgFlag ( _CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF ); _CrtSetDbgFlag ( _CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF );
#endif #endif
#ifdef Q_OS_MAC #ifdef Q_OS_MAC
YACReaderApplication app(argc,argv); YACReaderApplication app(argc,argv);
#else #else
QApplication app(argc, argv); QApplication app(argc, argv);
#endif #endif
#ifdef FORCE_ANGLE #ifdef FORCE_ANGLE
app.setAttribute(Qt::AA_UseOpenGLES); app.setAttribute(Qt::AA_UseOpenGLES);
#endif #endif
app.setApplicationName("YACReader"); app.setApplicationName("YACReader");
app.setOrganizationName("YACReader"); app.setOrganizationName("YACReader");
app.setAttribute(Qt::AA_UseHighDpiPixmaps); app.setAttribute(Qt::AA_UseHighDpiPixmaps);
if (QIcon::hasThemeIcon("YACReader")) { if (QIcon::hasThemeIcon("YACReader")) {
app.setWindowIcon(QIcon::fromTheme("YACReader")); app.setWindowIcon(QIcon::fromTheme("YACReader"));
} }
//simple command line parser
//will be replaced by QCommandLineParser in the future
QStringList optlist;
QStringList arglist;
if (argc > 1) // simple commandline parser
{ QCommandLineParser parser;
//extract options and arguments parser.addHelpOption();
optlist = QCoreApplication::arguments().filter(QRegExp ("^-{1,2}")); //options starting with "-" parser.addVersionOption();
arglist = QCoreApplication::arguments().filter(QRegExp ("^(?!-{1,2})")); //positional arguments parser.addPositionalArgument("[File|Directory]", "File or directory to open.");
//deal with standard options QCommandLineOption comicId("comicId", "", "comicId");
if (!optlist.isEmpty()) QCommandLineOption libraryId("libraryId", "", "libraryId");
{ // hide comicId and libraryId from help
QTextStream parser(stdout); comicId.setFlags(QCommandLineOption::HiddenFromHelp);
if (optlist.contains("--version") || optlist.contains("-v")) libraryId.setFlags(QCommandLineOption::HiddenFromHelp);
{ // process
parser << app.applicationName() << " " << QString(VERSION) << endl << "Copyright 2014 by Luis Angel San Martin Rodriguez" << endl; parser.addOption(comicId);
return 0; parser.addOption(libraryId);
} parser.process(app);
if (optlist.contains("--help") || optlist.contains("-h"))
{
parser << endl << "Usage: YACReader [File|Directory|Option]" << endl << endl;
parser << "Options:" << endl;
parser << " -h, --help\t\tDisplay this text and exit." << endl;
parser << " -v, --version\t\tDisplay version information and exit." << endl << endl;
parser << "Arguments:" << endl;
parser << " file\t\t\tOpen comic file." <<endl;
parser << " directory\t\tOpen comic directory." << endl << endl;
return 0;
}
}
}
QString destLog = YACReader::getSettingsPath()+"/yacreader.log"; QString destLog = YACReader::getSettingsPath()+"/yacreader.log";
QDir().mkpath(YACReader::getSettingsPath()); QDir().mkpath(YACReader::getSettingsPath());
Logger& logger = Logger::instance(); Logger& logger = Logger::instance();
logger.setLoggingLevel(QsLogging::InfoLevel); logger.setLoggingLevel(QsLogging::InfoLevel);
DestinationPtr fileDestination(DestinationFactory::MakeFileDestination( DestinationPtr fileDestination(DestinationFactory::MakeFileDestination(
destLog, EnableLogRotation, MaxSizeBytes(1048576), MaxOldLogCount(2))); destLog, EnableLogRotation, MaxSizeBytes(1048576), MaxOldLogCount(2)));
DestinationPtr debugDestination(DestinationFactory::MakeDebugOutputDestination()); DestinationPtr debugDestination(DestinationFactory::MakeDebugOutputDestination());
logger.addDestination(debugDestination); logger.addDestination(debugDestination);
logger.addDestination(fileDestination); logger.addDestination(fileDestination);
QTranslator translator; QTranslator translator;
QString sufix = QLocale::system().name(); QString sufix = QLocale::system().name();
#if defined Q_OS_UNIX && !defined Q_OS_MAC #if defined Q_OS_UNIX && !defined Q_OS_MAC
translator.load(QString(DATADIR)+"/yacreader/languages/yacreader_"+sufix); translator.load(QString(DATADIR)+"/yacreader/languages/yacreader_"+sufix);
#else #else
translator.load(QCoreApplication::applicationDirPath()+"/languages/yacreader_"+sufix); translator.load(QCoreApplication::applicationDirPath()+"/languages/yacreader_"+sufix);
#endif #endif
app.installTranslator(&translator); app.installTranslator(&translator);
MainWindowViewer * mwv = new MainWindowViewer(); MainWindowViewer * mwv = new MainWindowViewer();
// some arguments need to be parsed after MainWindowViewer creation
QStringList arglist = parser.positionalArguments();
if (parser.isSet(comicId) && parser.isSet(libraryId) && arglist.count() >=1)
{
mwv->open(arglist.at(0), parser.value(comicId).toULongLong(), parser.value(libraryId).toULongLong());
}
else if (arglist.count() >= 1)
{
mwv->openComicFromPath(arglist.at(0));
}
//parser code for comic loading needs to be processed after MainWindowViewer creation
//if we have a valid request, open it - if not, load normally
if (argc > 1)
{
if (!optlist.filter("--comicId=").isEmpty() && !optlist.filter("--libraryId=").isEmpty())
{
if (arglist.count()>1)
{
mwv->open(arglist.at(1), optlist.filter("--comicId=").at(0).split("=").at(1).toULongLong(), optlist.filter("--libraryId=").at(0).split("=").at(1).toULongLong());
}
}
else if ((arglist.count()>1))
{
//open first positional argument, silently ignore all following positional arguments
mwv->openComicFromPath(arglist.at(1));
}
}
#ifdef Q_OS_MAC #ifdef Q_OS_MAC
app.setWindow(mwv); app.setWindow(mwv);
#endif #endif
mwv->show(); mwv->show();
int ret = app.exec(); int ret = app.exec();
delete mwv; delete mwv;
//Configuration::getConfiguration().save(); //Configuration::getConfiguration().save();
YACReader::exitCheck(ret); YACReader::exitCheck(ret);
return ret; return ret;
} }