Commit df3ae429 authored by Linus Jahn's avatar Linus Jahn Committed by GitHub

Add command line parser for help and version infos (#44)

parent 5d736f17
......@@ -7,6 +7,7 @@ project(kaidan)
set(APPLICATION_ID "harbour.kaidan")
set(APPLICATION_NAME "kaidan")
set(APPLICATION_DISPLAY_NAME "Kaidan")
set(APPLICATION_DESCRIPTION "Cross platform XMPP client")
# Version
set(VERSION_MAJOR 0)
......@@ -66,6 +67,7 @@ target_compile_definitions(${PROJECT_NAME} PRIVATE
APPLICATION_ID="${APPLICATION_ID}"
APPLICATION_NAME="${APPLICATION_NAME}"
APPLICATION_DISPLAY_NAME="${APPLICATION_DISPLAY_NAME}"
APPLICATION_DESCRIPTION="${APPLICATION_DESCRIPTION}"
VERSION_STRING="${VERSION_STRING}"
)
// Qt
#include <QCommandLineParser>
#include <QCommandLineOption>
#include <QDebug>
#include <QGuiApplication>
#include <QQmlApplicationEngine>
#include <QQuickView>
......@@ -11,16 +14,72 @@
#include "RosterController.h"
#include "RosterItem.h"
enum CommandLineParseResult
{
CommandLineOk,
CommandLineError,
CommandLineVersionRequested,
CommandLineHelpRequested
};
CommandLineParseResult parseCommandLine(QCommandLineParser &parser, QString *errorMessage)
{
// add all possible arguments
QCommandLineOption helpOption = parser.addHelpOption();
QCommandLineOption versionOption = parser.addVersionOption();
// parse arguments
if (!parser.parse(QGuiApplication::arguments()))
{
*errorMessage = parser.errorText();
return CommandLineError;
}
// check for special cases
if (parser.isSet(versionOption))
return CommandLineVersionRequested;
if (parser.isSet(helpOption))
return CommandLineHelpRequested;
// if nothing special happened, return OK
return CommandLineOk;
}
int main(int argc, char *argv[])
{
qmlRegisterType<RosterController>(APPLICATION_ID, 1, 0, "RosterController");
qmlRegisterType<RosterItem>(APPLICATION_ID, 1, 0, "RosterItem");
// create a qt app
QGuiApplication app(argc, argv);
QGuiApplication::setApplicationName(APPLICATION_NAME);
QGuiApplication::setApplicationDisplayName(APPLICATION_DISPLAY_NAME);
QGuiApplication::setApplicationVersion(VERSION_STRING);
// create parser and add a description
QCommandLineParser parser;
parser.setApplicationDescription(QString(APPLICATION_DISPLAY_NAME) +
" - " + QString(APPLICATION_DESCRIPTION));
// parse the arguments
QString commandLineErrorMessage;
switch (parseCommandLine(parser, &commandLineErrorMessage))
{
case CommandLineError:
qDebug() << commandLineErrorMessage;
return 1;
case CommandLineVersionRequested:
parser.showVersion();
return 0;
case CommandLineHelpRequested:
parser.showHelp();
return 0;
case CommandLineOk:
qDebug() << "Successfully parsed command line input.";
break;
}
QtEventLoop eventLoop;
BoostNetworkFactories networkFactories(&eventLoop);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment