From 7e4c7aa77961dbdde132a76848ff5a6680a0c1a8 Mon Sep 17 00:00:00 2001 From: Jarek Kobus Date: Thu, 16 May 2024 09:55:16 +0200 Subject: [PATCH] CommandLine: Add a c'tor accepting a list of various types Make it possible to pass a list of a mixture of QStrings and QStringLists for command line's "args" argument. Change-Id: Iafe3249f074b9568537e72a80e46ec4ed143014a Reviewed-by: hjk --- src/libs/utils/commandline.cpp | 13 +++++++++++++ src/libs/utils/commandline.h | 16 ++++++++++++++++ 2 files changed, 29 insertions(+) diff --git a/src/libs/utils/commandline.cpp b/src/libs/utils/commandline.cpp index 8e6bc60277e..b4fa60481ed 100644 --- a/src/libs/utils/commandline.cpp +++ b/src/libs/utils/commandline.cpp @@ -1425,6 +1425,19 @@ CommandLine::CommandLine(const FilePath &exe, const QStringList &args) addArgs(args); } +CommandLine::CommandLine(const FilePath &exe, std::initializer_list args) + : m_executable(exe) +{ + for (const ArgRef &arg : args) { + if (const auto ptr = std::get_if(&arg.m_arg)) + addArg(QString::fromUtf8(*ptr)); + else if (const auto ptr = std::get_if>(&arg.m_arg)) + addArg(*ptr); + else if (const auto ptr = std::get_if>(&arg.m_arg)) + addArgs(*ptr); + } +} + CommandLine::CommandLine(const FilePath &exe, const QStringList &args, OsType osType) : m_executable(exe) { diff --git a/src/libs/utils/commandline.h b/src/libs/utils/commandline.h index 52ff8c5496f..3718f80bca3 100644 --- a/src/libs/utils/commandline.h +++ b/src/libs/utils/commandline.h @@ -11,6 +11,8 @@ #include #include +#include + namespace Utils { class AbstractMacroExpander; @@ -120,8 +122,22 @@ public: CommandLine(); ~CommandLine(); + struct ArgRef + { + ArgRef(const char *arg) : m_arg(arg) {} + ArgRef(const QString &arg) : m_arg(arg) {} + ArgRef(const QStringList &args) : m_arg(args) {} + + private: + friend class CommandLine; + const std::variant, + std::reference_wrapper> m_arg; + }; + explicit CommandLine(const FilePath &executable); CommandLine(const FilePath &exe, const QStringList &args); + CommandLine(const FilePath &exe, std::initializer_list args); CommandLine(const FilePath &exe, const QStringList &args, OsType osType); CommandLine(const FilePath &exe, const QString &unparsedArgs, RawType);