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
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
10 changed files with 95 additions and 28 deletions

View file

@ -2,6 +2,7 @@
## Unversioned ## Unversioned
- Minor: Added an option to only open channels specified in command line with `-c` parameter. You can also use `--help` to display short help message (#1940)
- Minor: Added customizable timeout buttons to the user info popup - Minor: Added customizable timeout buttons to the user info popup
- Minor: Deprecate loading of "v1" window layouts. If you haven't updated Chatterino in more than 2 years, there's a chance you will lose your window layout. - Minor: Deprecate loading of "v1" window layouts. If you haven't updated Chatterino in more than 2 years, there's a chance you will lose your window layout.
- Minor: Disable checking for updates on unsupported platforms (#1874) - Minor: Disable checking for updates on unsupported platforms (#1874)

View file

@ -32,6 +32,7 @@ Mm2PL | https://github.com/mm2pl | | Contributor
gempir | https://github.com/gempir | | Contributor gempir | https://github.com/gempir | | Contributor
mfmarlow | https://github.com/mfmarlow | | Contributor mfmarlow | https://github.com/mfmarlow | | Contributor
dnsge | https://github.com/dnsge | | Contributor dnsge | https://github.com/dnsge | | Contributor
zneix | https://github.com/zneix | | Contributor
# If you are a contributor add yourself above this line # If you are a contributor add yourself above this line
Defman21 | https://github.com/Defman21 | | Documentation Defman21 | https://github.com/Defman21 | | Documentation

View file

@ -86,12 +86,6 @@ namespace {
} }
} // namespace } // namespace
bool shouldRunBrowserExtensionHost(const QStringList &args)
{
return args.size() > 0 && (args[0].startsWith("chrome-extension://") ||
args[0].endsWith(".json"));
}
void runBrowserExtensionHost() void runBrowserExtensionHost()
{ {
initFileMode(); initFileMode();

View file

@ -1,10 +1,7 @@
#pragma once #pragma once
class QStringList;
namespace chatterino { namespace chatterino {
bool shouldRunBrowserExtensionHost(const QStringList &args);
void runBrowserExtensionHost(); void runBrowserExtensionHost();
} // namespace chatterino } // namespace chatterino

View file

@ -9,6 +9,7 @@
#include <csignal> #include <csignal>
#include "Application.hpp" #include "Application.hpp"
#include "common/Args.hpp"
#include "common/Modes.hpp" #include "common/Modes.hpp"
#include "common/NetworkManager.hpp" #include "common/NetworkManager.hpp"
#include "singletons/Paths.hpp" #include "singletons/Paths.hpp"
@ -215,7 +216,10 @@ void runGui(QApplication &a, Paths &paths, Settings &settings)
removeRunningFile(runningPath); removeRunningFile(runningPath);
if (!getArgs().dontSaveSettings)
{
pajlada::Settings::SettingManager::gSave(); pajlada::Settings::SettingManager::gSave();
}
chatterino::NetworkManager::deinit(); chatterino::NetworkManager::deinit();

View file

@ -1,27 +1,87 @@
#include "Args.hpp" #include "Args.hpp"
#include <QApplication>
#include <QCommandLineParser>
#include <QDebug>
#include <QJsonArray>
#include <QJsonDocument>
#include <QJsonObject>
#include <QStringList>
namespace chatterino { 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);
} }
else if (arg == "--version")
// Twitch (default)
if (platform == "t")
{ {
this->printVersion = true; // TODO: try not to parse JSON
QString channelObjectString =
"{\"splits2\": { \"data\": { \"name\": \"" + channelName +
"\", \"type\": \"twitch\" }, \"type\": \"split\" }}";
channelArray.push_back(
QJsonDocument::fromJson(channelObjectString.toUtf8())
.object());
} }
} }
if (channelArray.size() > 0)
{
this->dontSaveSettings = true;
this->channelsToJoin = channelArray;
}
}
this->printVersion = parser.isSet("v");
this->crashRecovery = parser.isSet("crash-recovery");
} }
static Args *instance = nullptr; 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() const Args &getArgs()

View file

@ -1,6 +1,7 @@
#pragma once #pragma once
#include <QStringList> #include <QApplication>
#include <QJsonArray>
namespace chatterino { namespace chatterino {
@ -8,13 +9,16 @@ namespace chatterino {
class Args class Args
{ {
public: public:
Args(const QStringList &args); Args(const QApplication &app);
bool printVersion{}; bool printVersion{};
bool crashRecovery{}; bool crashRecovery{};
bool shouldRunBrowserExtensionHost{};
bool dontSaveSettings{};
QJsonArray channelsToJoin{};
}; };
void initArgs(const QStringList &args); void initArgs(const QApplication &app);
const Args &getArgs(); const Args &getArgs();
} // namespace chatterino } // namespace chatterino

View file

@ -1,4 +1,5 @@
#include <QApplication> #include <QApplication>
#include <QCommandLineParser>
#include <QDebug> #include <QDebug>
#include <QMessageBox> #include <QMessageBox>
#include <QStringList> #include <QStringList>
@ -25,14 +26,10 @@ int main(int argc, char **argv)
QCoreApplication::setApplicationVersion(CHATTERINO_VERSION); QCoreApplication::setApplicationVersion(CHATTERINO_VERSION);
QCoreApplication::setOrganizationDomain("https://www.chatterino.com"); QCoreApplication::setOrganizationDomain("https://www.chatterino.com");
// convert char** to QStringList initArgs(a);
auto args = QStringList();
std::transform(argv + 1, argv + argc, std::back_inserter(args),
[&](auto s) { return s; });
initArgs(args);
// run in gui mode or browser extension host mode // run in gui mode or browser extension host mode
if (shouldRunBrowserExtensionHost(args)) if (getArgs().shouldRunBrowserExtensionHost)
{ {
runBrowserExtensionHost(); runBrowserExtensionHost();
} }

View file

@ -12,6 +12,7 @@
#include <chrono> #include <chrono>
#include "Application.hpp" #include "Application.hpp"
#include "common/Args.hpp"
#include "debug/AssertInGuiThread.hpp" #include "debug/AssertInGuiThread.hpp"
#include "messages/MessageElement.hpp" #include "messages/MessageElement.hpp"
#include "providers/irc/Irc2.hpp" #include "providers/irc/Irc2.hpp"
@ -326,6 +327,10 @@ void WindowManager::initialize(Settings &settings, Paths &paths)
void WindowManager::save() void WindowManager::save()
{ {
if (getArgs().dontSaveSettings)
{
return;
}
qDebug() << "[WindowManager] Saving"; qDebug() << "[WindowManager] Saving";
assertInGuiThread(); assertInGuiThread();
QJsonDocument document; QJsonDocument document;

View file

@ -1,6 +1,7 @@
#include "widgets/dialogs/SettingsDialog.hpp" #include "widgets/dialogs/SettingsDialog.hpp"
#include "Application.hpp" #include "Application.hpp"
#include "common/Args.hpp"
#include "singletons/Resources.hpp" #include "singletons/Resources.hpp"
#include "util/LayoutCreator.hpp" #include "util/LayoutCreator.hpp"
#include "util/Shortcut.hpp" #include "util/Shortcut.hpp"
@ -314,8 +315,11 @@ void SettingsDialog::showEvent(QShowEvent *)
///// Widget creation helpers ///// Widget creation helpers
void SettingsDialog::onOkClicked() void SettingsDialog::onOkClicked()
{
if (!getArgs().dontSaveSettings)
{ {
pajlada::Settings::SettingManager::gSave(); pajlada::Settings::SettingManager::gSave();
}
this->close(); this->close();
} }