From ce050716c2e4f1dd56fbd3fa3290bfe258ea1af6 Mon Sep 17 00:00:00 2001 From: Aqua-sama Date: Sat, 27 Jan 2018 13:44:22 +0100 Subject: Passing unknown command line parameters to the Configuration - no longer compiling CookiesForm that we don't use --- src/configuration.cpp | 25 ++++++++++++++----------- src/configuration.h | 3 ++- src/forms/aboutdialog.cpp | 9 ++++----- src/forms/aboutdialog.h | 2 +- src/main.cpp | 38 ++++++++++++++++++++++++++++++++++++-- 5 files changed, 57 insertions(+), 20 deletions(-) (limited to 'src') diff --git a/src/configuration.cpp b/src/configuration.cpp index 4e72d18..c114155 100644 --- a/src/configuration.cpp +++ b/src/configuration.cpp @@ -12,7 +12,7 @@ namespace po = boost::program_options; -Configuration::Configuration(const QStringList &options) +Configuration::Configuration() { m_homePath = QStandardPaths::writableLocation(QStandardPaths::HomeLocation).toStdString(); @@ -81,20 +81,13 @@ Configuration::Configuration(const QStringList &options) // store the defaults into the vm { - int argc = options.length(); - const char* argv[argc]; - for(int i = 0; i < argc; ++i) { - argv[i] = qUtf8Printable(options.at(i)); - } - - po::store(po::parse_command_line(argc, argv, desc), vm); + const char* argv[0]; + po::store(po::parse_command_line(0, argv, desc), vm); } } -Configuration::~Configuration() -{ -} +Configuration::~Configuration() = default; bool Configuration::read(const QString &path) { @@ -103,3 +96,13 @@ bool Configuration::read(const QString &path) return true; } +bool Configuration::parse(int argc, const char **argv) +{ + try { + po::store(po::parse_command_line(argc, argv, desc), vm); + } catch (const po::error &e) { + return false; + } + + return true; +} diff --git a/src/configuration.h b/src/configuration.h index 27988b9..c9223eb 100644 --- a/src/configuration.h +++ b/src/configuration.h @@ -19,10 +19,11 @@ class Configuration { public: - explicit Configuration(const QStringList &options = QStringList()); + explicit Configuration(); ~Configuration(); bool read(const QString &path); + bool parse(int argc, const char **argv); template std::optional value(const char *path) const diff --git a/src/forms/aboutdialog.cpp b/src/forms/aboutdialog.cpp index 89cb23e..5b788bd 100644 --- a/src/forms/aboutdialog.cpp +++ b/src/forms/aboutdialog.cpp @@ -11,10 +11,6 @@ #include "version.h" #include -// The extra level of indirection will allow the preprocessor to expand the macros before they are converted to strings. -#define STRINGIFY(x) #x -#define STR(x) STRINGIFY(x) - // compiler // clang also defines __GNUC__, so we need to check for clang first #if defined(__clang__) @@ -32,7 +28,10 @@ AboutDialog::AboutDialog(QWidget *parent) setAttribute(Qt::WA_DeleteOnClose, true); ui->setupUi(this); - ui->icon->setPixmap(qApp->windowIcon().pixmap(72, 72)); + // clang-tidy: don't use static_cast to downcast from a base to a derived class, + // use dynamic_cast instead + //ui->icon->setPixmap(qApp->windowIcon().pixmap(72, 72)); + ui->icon->setPixmap(dynamic_cast(QApplication::instance())->windowIcon().pixmap(72, 72)); auto *aboutLabel = new QLabel(this); aboutLabel->setWordWrap(true); diff --git a/src/forms/aboutdialog.h b/src/forms/aboutdialog.h index 1a1b4eb..3175939 100644 --- a/src/forms/aboutdialog.h +++ b/src/forms/aboutdialog.h @@ -22,7 +22,7 @@ class AboutDialog : public QDialog public: explicit AboutDialog(QWidget *parent = nullptr); - ~AboutDialog(); + ~AboutDialog() override; private: Ui::AboutDialog *ui; diff --git a/src/main.cpp b/src/main.cpp index 70f22be..973ac58 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -10,7 +10,6 @@ #include "src/mainwindow/mainwindow.h" #include "version.h" #include -#include #include #include @@ -66,7 +65,9 @@ int main(int argc, char **argv) parser.addPositionalArgument("URL", "URL(s) to open"); - parser.process(instance); + // use parse instead of process + // process calls exit() on unknown options + parser.parse(instance.arguments()); #ifdef QT_DEBUG qDebug("config=%s", qUtf8Printable(parser.value(configOption))); @@ -83,6 +84,7 @@ int main(int argc, char **argv) qUtf8Printable(parser.value(defaultConfigOption)), config->read(parser.value(defaultConfigOption)) ? "ok" : "failed"); } + // then load in the user configuration, which will overwrite it if(parser.isSet(configOption)) { qDebug("Reading configuration [%s]: %s", @@ -90,6 +92,38 @@ int main(int argc, char **argv) config->read(parser.value(configOption)) ? "ok" : "failed"); } + // parse command-line overrides + // we assume the users knows what they're doing, so we only pass the unknown options to program_options + // passing any unknown options though will cause it to fail, so we need to filter out the regular options + // unfortunately, QCommandLineParser will only give us the unknown option + // names, so we need to build a list, add them as options, reparse, and then + // we get their values + if(!parser.unknownOptionNames().isEmpty()) { + int _argc = parser.unknownOptionNames().length() + 1; + const char* _argv[_argc]; + + // program_options requires 0 to be the program name, otherwise it seems to fail + _argv[0] = qUtf8Printable(instance.arguments().at(0)); + + // create a list of unknown QCommandLineOption's + // parser.addOptions() takes a list, so this is a QList + QList opts; + for (const QString &opt : parser.unknownOptionNames()) { + QCommandLineOption o(opt, "dummy desc", "dummy value"); + opts.append(o); + } + + // add list and reparse to set the new options + parser.addOptions(opts); + parser.parse(instance.arguments()); + + for(int i = 1; i < _argc; ++i) { + _argv[i] = qUtf8Printable(QString("--%1=%2").arg(opts[i-1].names().at(0), parser.value(opts[i-1]))); + } + + qDebug("Parsing command-line overrides: %s", config->parse(_argc, _argv) ? "ok" : "failed"); + } + // check for other instances // if we socket hasn't been disabled (socket is not none) if(parser.value(socketOption) != "none") { -- cgit v1.2.1