简体   繁体   English

传递包含空格和引号的参数字符串

[英]Passing argument string which contains spaces and quotes

Using QProcess::startDetached , I need to pass a dynamic argument list which is coming from another process to the starting process. 使用QProcess::startDetached ,我需要将一个动态参数列表传递给起始进程,该列表来自另一个进程。

const QString & prog, const QStringList & args, const QString & workingDirectory ...)

Note that arguments that contain spaces are not passed to the process as separate arguments. 请注意,包含空格的参数不会作为单独的参数传递给进程。

... ...

Windows: Arguments that contain spaces are wrapped in quotes. Windows:包含空格的参数用引号括起来。 The started process will run as a regular standalone process. 启动的进程将作为常规独立进程运行。

I have a string which contains below text, It comes from an external program without any control on it: 我有一个包含下面文本的字符串,它来自一个没有任何控制权的外部程序:

-c "resume" -c "print 'Hi!'" -c "print 'Hello World'"

I need to pass above string to QProcess::startDetached so that the starting program catches it as same as above string. 我需要将上面的字符串传递给QProcess::startDetached以便启动程序捕获它与上面的字符串相同。

Do I have to parse the string and build a string-list? 我是否必须解析字符串并构建字符串列表? Or anyone has a better solution? 或者任何人有更好的解决方案?

You don't have to use a QStringList at all for the arguments, as there is this overloaded function: - 您根本不需要使用QStringList作为参数,因为存在这个重载函数: -

bool QProcess::startDetached(const QString & program)

Which, as the documentation states: - 其中,正如文件所述: -

Starts the program program in a new process. 以新流程启动程序程序。 program is a single string of text containing both the program name and its arguments . program是包含程序名称及其参数的单个文本字符串 The arguments are separated by one or more spaces. 参数由一个或多个空格分隔。

The program string can also contain quotes, to ensure that arguments containing spaces are correctly supplied to the new process. 程序字符串还可以包含引号,以确保包含空格的参数正确提供给新进程。

You may need to replace " with \\", but you can do that from a QString 您可能需要替换“with”,但您可以从QString中执行此操作

You can use parseCombinedArgString (from Qt's source code) to parse: 您可以使用parseCombinedArgString (来自Qt的源代码)来解析:

QStringList parseCombinedArgString(const QString &program)
{
    QStringList args;
    QString tmp;
    int quoteCount = 0;
    bool inQuote = false;
    // handle quoting. tokens can be surrounded by double quotes
    // "hello world". three consecutive double quotes represent
    // the quote character itself.
    for (int i = 0; i < program.size(); ++i)
    {
        if (program.at(i) == QLatin1Char('"'))
        {
            ++quoteCount;
            if (quoteCount == 3)
            {
                // third consecutive quote
                quoteCount = 0;
                tmp += program.at(i);
            }
            continue;
        }
        if (quoteCount)
        {
            if (quoteCount == 1)
                inQuote = !inQuote;
            quoteCount = 0;
        }
        if (!inQuote && program.at(i).isSpace())
        {
            if (!tmp.isEmpty())
            {
                args += tmp;
                tmp.clear();
            }
        }
        else
        {
            tmp += program.at(i);
        }
    }
    if (!tmp.isEmpty())
        args += tmp;
    return args;
}

是的,您必须“解析”字符串,将其拆分到正确的位置,然后将每个子字符串输入到传递给函数的QStringList对象中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM