mirror of
https://github.com/YACReader/yacreader
synced 2025-06-04 01:28:55 -04:00
YACReader: Port commandline to QCommandLineParser
This commit is contained in:
parent
9103ee0c0c
commit
afab73cbc6
@ -1,6 +1,7 @@
|
||||
#include <QApplication>
|
||||
#include <QDir>
|
||||
#include <QTranslator>
|
||||
#include <QCommandLineParser>
|
||||
|
||||
#include "main_window_viewer.h"
|
||||
#include "configuration.h"
|
||||
@ -53,7 +54,7 @@ class YACReaderApplication: public QApplication
|
||||
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 );
|
||||
#endif
|
||||
|
||||
@ -73,38 +74,21 @@ int main(int argc, char * argv[])
|
||||
if (QIcon::hasThemeIcon("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)
|
||||
{
|
||||
//extract options and arguments
|
||||
optlist = QCoreApplication::arguments().filter(QRegExp ("^-{1,2}")); //options starting with "-"
|
||||
arglist = QCoreApplication::arguments().filter(QRegExp ("^(?!-{1,2})")); //positional arguments
|
||||
//deal with standard options
|
||||
if (!optlist.isEmpty())
|
||||
{
|
||||
QTextStream parser(stdout);
|
||||
if (optlist.contains("--version") || optlist.contains("-v"))
|
||||
{
|
||||
parser << app.applicationName() << " " << QString(VERSION) << endl << "Copyright 2014 by Luis Angel San Martin Rodriguez" << endl;
|
||||
return 0;
|
||||
}
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
// simple commandline parser
|
||||
QCommandLineParser parser;
|
||||
parser.addHelpOption();
|
||||
parser.addVersionOption();
|
||||
parser.addPositionalArgument("[File|Directory]", "File or directory to open.");
|
||||
QCommandLineOption comicId("comicId", "", "comicId");
|
||||
QCommandLineOption libraryId("libraryId", "", "libraryId");
|
||||
// hide comicId and libraryId from help
|
||||
comicId.setFlags(QCommandLineOption::HiddenFromHelp);
|
||||
libraryId.setFlags(QCommandLineOption::HiddenFromHelp);
|
||||
// process
|
||||
parser.addOption(comicId);
|
||||
parser.addOption(libraryId);
|
||||
parser.process(app);
|
||||
|
||||
QString destLog = YACReader::getSettingsPath()+"/yacreader.log";
|
||||
QDir().mkpath(YACReader::getSettingsPath());
|
||||
@ -128,23 +112,17 @@ int main(int argc, char * argv[])
|
||||
app.installTranslator(&translator);
|
||||
MainWindowViewer * mwv = new MainWindowViewer();
|
||||
|
||||
//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)
|
||||
// some arguments need to be parsed after MainWindowViewer creation
|
||||
QStringList arglist = parser.positionalArguments();
|
||||
if (parser.isSet(comicId) && parser.isSet(libraryId) && arglist.count() >=1)
|
||||
{
|
||||
if (!optlist.filter("--comicId=").isEmpty() && !optlist.filter("--libraryId=").isEmpty())
|
||||
mwv->open(arglist.at(0), parser.value(comicId).toULongLong(), parser.value(libraryId).toULongLong());
|
||||
}
|
||||
else if (arglist.count() >= 1)
|
||||
{
|
||||
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));
|
||||
}
|
||||
mwv->openComicFromPath(arglist.at(0));
|
||||
}
|
||||
|
||||
#ifdef Q_OS_MAC
|
||||
app.setWindow(mwv);
|
||||
#endif
|
||||
|
Loading…
x
Reference in New Issue
Block a user