Argument parsing rework (#1940)

* Experimental argument handling

* Restored browser extension launch functionality

Also moved check from BrowerExtension.cpp to Args.cpp as it is more relevant there and doesn't require passing arguments to a function in another file

* Fixed formatting

* Simplified Args.cpp code, added changelog entry

* Hid crash-recovery from help

* Dont save settings if launched with --channels

* Changed parsing method to t:channel

* Code cleanup

* Changed plaform delimeter to :, platform defaults to Twitch

Co-authored-by: fourtf <tf.four@gmail.com>
This commit is contained in:
Paweł
2020-09-26 16:03:51 +02:00
committed by GitHub
parent 2f3accf3cb
commit d314566ab6
10 changed files with 95 additions and 28 deletions
+68 -8
View File
@@ -1,27 +1,87 @@
#include "Args.hpp"
#include <QApplication>
#include <QCommandLineParser>
#include <QDebug>
#include <QJsonArray>
#include <QJsonDocument>
#include <QJsonObject>
#include <QStringList>
namespace chatterino {
Args::Args(const QStringList &args)
Args::Args(const QApplication &app)
{
for (auto &&arg : args)
QCommandLineParser parser;
parser.setApplicationDescription("Chatterino 2 Client for Twitch Chat");
parser.addHelpOption();
// Used internally by app to restart after unexpected crashes
QCommandLineOption crashRecoveryOption("crash-recovery");
crashRecoveryOption.setHidden(true);
parser.addOptions({
{{"v", "version"}, "Displays version information."},
crashRecoveryOption,
});
parser.addOption(QCommandLineOption(
{"c", "channels"},
"Joins only supplied channels on startup. Use letters with colons to "
"specify platform. Only twitch channels are supported at the moment.\n"
"If platform isn't specified, default is Twitch.",
"t:channel1;t:channel2;..."));
parser.process(app);
const QStringList args = parser.positionalArguments();
this->shouldRunBrowserExtensionHost =
(args.size() > 0 && (args[0].startsWith("chrome-extension://") ||
args[0].endsWith(".json")));
if (parser.isSet("c"))
{
if (arg == "--crash-recovery")
QJsonArray channelArray;
QStringList channelArgList = parser.value("c").split(";");
for (QString channelArg : channelArgList)
{
this->crashRecovery = true;
// Twitch is default platform
QString platform = "t";
QString channelName = channelArg;
const QRegExp regExp("(.):(.*)");
if (regExp.indexIn(channelArg) != -1)
{
platform = regExp.cap(1);
channelName = regExp.cap(2);
}
// Twitch (default)
if (platform == "t")
{
// TODO: try not to parse JSON
QString channelObjectString =
"{\"splits2\": { \"data\": { \"name\": \"" + channelName +
"\", \"type\": \"twitch\" }, \"type\": \"split\" }}";
channelArray.push_back(
QJsonDocument::fromJson(channelObjectString.toUtf8())
.object());
}
}
else if (arg == "--version")
if (channelArray.size() > 0)
{
this->printVersion = true;
this->dontSaveSettings = true;
this->channelsToJoin = channelArray;
}
}
this->printVersion = parser.isSet("v");
this->crashRecovery = parser.isSet("crash-recovery");
}
static Args *instance = nullptr;
void initArgs(const QStringList &args)
void initArgs(const QApplication &app)
{
instance = new Args(args);
instance = new Args(app);
}
const Args &getArgs()