将多个解析值传递给 addPositionalArgument 函数

Pass multiple parsing values to addPositionalArgument function

本文关键字:addPositionalArgument 函数 值传      更新时间:2023-10-16

我正在寻找如何解析命令行参数。我发现这个:

// A boolean option with multiple names (-f, --force)
QCommandLineOption forceOption(QStringList() << "f" << "force",
        QCoreApplication::translate("main", "Overwrite existing files."));
parser.addOption(forceOption);

这工作正常。但是如何为字符串值添加两个值呢?例如,foo --source ...应该与foo -s ...相同。

我试过了:

parser.addPositionalArgument(
   QStringList() << "s" << "source",
   QCoreApplication::translate("main", "...")
);

但这会引发一个错误:

error: no matching function for call to 
'QCommandLineParser::addPositionalArgument(QStringList&, QString)'
parser.addPositionalArgument(QStringList() << "t" << "title",
QCoreApplication::translate("main", "...."));

这可能是addPositionalArgument期望一个字符串而不是字符串列表。

但是如何为两个值指定别名呢?

你没有为此使用位置参数。位置参数是需要按特定顺序出现的参数,并且可以具有任何值。它们不是通过 -s--source 之类的东西引入的参数。

正如您已经发现的那样,您可以使用 QCommandLineOption 中的QStringList为两者别名。如果您希望参数跟随,只需在构造函数中指定以下内容:

QCommandLineOption sourceOption(
    QStringList() << "s" << "source",
    "Specify the source", // better translate that string
    "source", // the name of the following argument
    "something" // the default value of the following argument, not required
);
parser.addOption(sourceOption);
相关文章:
  • 没有找到相关文章