From b82c5699ee0300b8547aa694967ff0f590352ed2 Mon Sep 17 00:00:00 2001 From: Sergey Fedorov Date: Wed, 12 Aug 2026 19:20:14 +0000 Subject: [PATCH] Qt4: replace QCommandLineParser with a hand-rolled fallback QCommandLineParser doesn't exist before Qt 5.2. Version-gate it: Qt5.2+ keeps using it unchanged, Qt4 falls back to parseServerOption(), a small hand-rolled parser covering the same CLI surface this app actually uses (-h/--help, -v/--version, -s/--server ), built on QCoreApplication::arguments() which has existed since Qt4.0. --- src/main.cpp | 41 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) diff --git a/src/main.cpp b/src/main.cpp index 50c37b1..109e60d 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -35,7 +35,9 @@ extern "C" { #if !defined(_WIN32) && !defined(PROJ_GNUTLS_DEBUG) #include #endif +#if QT_VERSION >= QT_VERSION_CHECK(5, 2, 0) #include +#endif #include #include @@ -46,6 +48,7 @@ extern "C" { #include #include +#include static void log_callback(int level, const char* str) { @@ -120,6 +123,40 @@ int pin_callback(void* userdata, int attempt, const char* token_url, return 0; } +#if QT_VERSION < QT_VERSION_CHECK(5, 2, 0) +/* QCommandLineParser doesn't exist before Qt 5.2; hand-roll the same + * tiny CLI surface (-h/--help, -v/--version, -s/--server ) for + * Qt4 instead. */ +static QString parseServerOption(const QCoreApplication& app) +{ + const QStringList args = app.arguments(); + for (int i = 1; i < args.size(); ++i) { + const QString& arg = args.at(i); + if (arg == QLatin1String("-h") || arg == QLatin1String("--help")) { + std::printf("%s\n\n", + qPrintable(QObject::tr("OpenConnect is a VPN client, that utilizes TLS and DTLS " + "for secure session establishment, and is compatible " + "with many VPN protocols."))); + std::printf("Usage: %s [options]\n\n", qPrintable(args.at(0))); + std::printf("Options:\n"); + std::printf(" -h, --help Displays help on commandline options.\n"); + std::printf(" -v, --version Displays version information.\n"); + std::printf(" -s, --server %s\n", + qPrintable(QObject::tr("auto-connect to existing profile "))); + ::exit(0); + } else if (arg == QLatin1String("-v") || arg == QLatin1String("--version")) { + std::printf("%s %s\n", qPrintable(app.applicationName()), qPrintable(app.applicationVersion())); + ::exit(0); + } else if (arg == QLatin1String("-s") || arg == QLatin1String("--server")) { + if (i + 1 < args.size()) { + return args.at(i + 1); + } + } + } + return QString(); +} +#endif + int main(int argc, char* argv[]) { bool haveTray = false; @@ -176,6 +213,7 @@ int main(int argc, char* argv[]) #endif openconnect_init_ssl(); +#if QT_VERSION >= QT_VERSION_CHECK(5, 2, 0) QCommandLineParser parser; parser.setApplicationDescription( QObject::tr("OpenConnect is a VPN client, that utilizes TLS and DTLS " @@ -192,6 +230,9 @@ int main(int argc, char* argv[]) parser.process(app); const QString profileName{ parser.value(QLatin1String("server")) }; +#else + const QString profileName{ parseServerOption(app) }; +#endif MainWindow mainWindow(nullptr, haveTray, profileName); app.setActivationWindow(&mainWindow); #ifdef PROJ_PKCS11 -- 2.43.0