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 <hjk@qt.io>
This commit is contained in:
Jarek Kobus
2024-05-16 09:55:16 +02:00
parent 513bfcbda1
commit 7e4c7aa779
2 changed files with 29 additions and 0 deletions

View File

@@ -1425,6 +1425,19 @@ CommandLine::CommandLine(const FilePath &exe, const QStringList &args)
addArgs(args);
}
CommandLine::CommandLine(const FilePath &exe, std::initializer_list<ArgRef> args)
: m_executable(exe)
{
for (const ArgRef &arg : args) {
if (const auto ptr = std::get_if<const char *>(&arg.m_arg))
addArg(QString::fromUtf8(*ptr));
else if (const auto ptr = std::get_if<std::reference_wrapper<const QString>>(&arg.m_arg))
addArg(*ptr);
else if (const auto ptr = std::get_if<std::reference_wrapper<const QStringList>>(&arg.m_arg))
addArgs(*ptr);
}
}
CommandLine::CommandLine(const FilePath &exe, const QStringList &args, OsType osType)
: m_executable(exe)
{

View File

@@ -11,6 +11,8 @@
#include <QPair>
#include <QStringList>
#include <variant>
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<const char *,
std::reference_wrapper<const QString>,
std::reference_wrapper<const QStringList>> m_arg;
};
explicit CommandLine(const FilePath &executable);
CommandLine(const FilePath &exe, const QStringList &args);
CommandLine(const FilePath &exe, std::initializer_list<ArgRef> args);
CommandLine(const FilePath &exe, const QStringList &args, OsType osType);
CommandLine(const FilePath &exe, const QString &unparsedArgs, RawType);