QCommandLineParser Class
QCommandLineParser 类提供了一种处理命令行选项的方法。更多
Header: | #include <QCommandLineParser> |
CMake.QCommandLineParser | find_package(Qt6 REQUIRED COMPONENTS Core) target_link_libraries(mytarget PRIVATE Qt6::Core) |
qmake: | QT += core |
公共类型
(since 6.9) enum class | MessageType { Information, Error } |
enum | OptionsAfterPositionalArgumentsMode { ParseAsOptions, ParseAsPositionalArguments } |
enum | SingleDashWordOptionMode { ParseAsCompactedShortOptions, ParseAsLongOptions } |
公共函数
QCommandLineParser() | |
~QCommandLineParser() | |
QCommandLineOption | addHelpOption() |
bool | addOption(const QCommandLineOption &option) |
bool | addOptions(const QList<QCommandLineOption> &options) |
void | addPositionalArgument(const QString &name, const QString &description, const QString &syntax = QString()) |
QCommandLineOption | addVersionOption() |
QString | applicationDescription() const |
void | clearPositionalArguments() |
QString | errorText() const |
QString | helpText() const |
bool | isSet(const QString &name) const |
bool | isSet(const QCommandLineOption &option) const |
QStringList | optionNames() const |
bool | parse(const QStringList &arguments) |
QStringList | positionalArguments() const |
void | process(const QStringList &arguments) |
void | process(const QCoreApplication &app) |
void | setApplicationDescription(const QString &description) |
void | setOptionsAfterPositionalArgumentsMode(QCommandLineParser::OptionsAfterPositionalArgumentsMode parsingMode) |
void | setSingleDashWordOptionMode(QCommandLineParser::SingleDashWordOptionMode singleDashWordOptionMode) |
void | showHelp(int exitCode = 0) |
void | showVersion() |
QStringList | unknownOptionNames() const |
QString | value(const QString &optionName) const |
QString | value(const QCommandLineOption &option) const |
QStringList | values(const QString &optionName) const |
QStringList | values(const QCommandLineOption &option) const |
静态公共成员
(since 6.9) void | showMessageAndExit(QCommandLineParser::MessageType type, const QString &message, int exitCode = 0) |
详细说明
QCoreApplication QCommandLineParser 以简单的字符串列表形式提供命令行参数。QCommandLineParser 提供了定义选项集、解析命令行参数、存储已实际使用的选项以及选项值的功能。
任何不是选项的参数(即不是以-
开头的参数)都会被存储为 "位置参数"。
解析器会处理短名称、长名称、同一选项的多个名称以及选项值。
命令行上的选项会被识别为以一个或两个-
字符开头,然后是选项名称。选项-
(单破折号)是一种特殊情况,通常表示标准输入,不作为选项处理。解析器会将--
(双破折号)之后的所有内容视为位置参数。
短选项是单字母。在命令行中通过-v
可以指定v
选项。在默认解析模式下,短选项可以写成紧凑形式,例如-abc
等同于-a -b -c
。解析模式可以改为ParseAsLongOptions ,在这种情况下,-abc
将被解析为长选项abc
。
长选项的长度超过一个字母,不能压缩在一起。长选项verbose
将作为--verbose
或-verbose
传递。
向选项传递值可以使用赋值操作符 (-v=value
,--verbose=value
) 或空格 (-v value
,--verbose value
) 。即使值以-
开头,也可以这样做。
解析器不支持可选值--如果一个选项被设置为需要一个值,则必须有一个值。如果该选项放在最后且没有值,则该选项将被视为未指定。
解析器不自动支持通过使用--disable-option
或--no-option
格式来否定或禁用长选项。不过,可以明确地处理这种情况,即创建一个以no-option
作为名称之一的选项,并明确地处理该选项。
例如
int main(int argc, char *argv[]) { QCoreApplication app(argc, argv); QCoreApplication::setApplicationName("my-copy-program"); QCoreApplication::setApplicationVersion("1.0"); QCommandLineParser parser; parser.setApplicationDescription("Test helper"); parser.addHelpOption(); parser.addVersionOption(); parser.addPositionalArgument("source", QCoreApplication::translate("main", "Source file to copy.")); parser.addPositionalArgument("destination", QCoreApplication::translate("main", "Destination directory.")); // A boolean option with a single name (-p) QCommandLineOption showProgressOption("p", QCoreApplication::translate("main", "Show progress during copy")); parser.addOption(showProgressOption); // A boolean option with multiple names (-f, --force) QCommandLineOption forceOption(QStringList() << "f" << "force", QCoreApplication::translate("main", "Overwrite existing files.")); parser.addOption(forceOption); // An option with a value QCommandLineOption targetDirectoryOption(QStringList() << "t" << "target-directory", QCoreApplication::translate("main", "Copy all source files into <directory>."), QCoreApplication::translate("main", "directory")); parser.addOption(targetDirectoryOption); // Process the actual command line arguments given by the user parser.process(app); const QStringList args = parser.positionalArguments(); // source is args.at(0), destination is args.at(1) bool showProgress = parser.isSet(showProgressOption); bool force = parser.isSet(forceOption); QString targetDir = parser.value(targetDirectoryOption); // ... }
使用addOptions() 可以使上例中的三个addOption() 调用更加简洁:
parser.addOptions({ // A boolean option with a single name (-p) {"p", QCoreApplication::translate("main", "Show progress during copy")}, // A boolean option with multiple names (-f, --force) {{"f", "force"}, QCoreApplication::translate("main", "Overwrite existing files.")}, // An option with a value {{"t", "target-directory"}, QCoreApplication::translate("main", "Copy all source files into <directory>."), QCoreApplication::translate("main", "directory")}, });
已知限制:对QCoreApplication 和子类内部 Qt 选项的解析发生在 QCommandLineParser 存在之前,因此无法将其考虑在内。这意味着任何看起来像 Qt 内置选项的选项值都会被QCoreApplication 视为 Qt 内置选项。例如:--profile -reverse
会导致QGuiApplication 看到 -reverse 选项的设置,并在 QCommandLineParser 定义profile
选项和解析命令行之前将其从QCoreApplication::arguments() 中移除。
如何在复杂应用程序中使用 QCommandLineParser
在实际应用中,需要对位置参数和选项值进行额外的错误检查。例如,应检查数字范围。
因此,最好引入一个函数来进行命令行解析,该函数接收一个结构或类,接收选项值后返回一个代表结果的对象。QtNetwork 模块中的 dnslookup 示例就说明了这一点:
struct DnsQuery { DnsQuery() : type(QDnsLookup::A) {} QDnsLookup::Type type; QHostAddress nameServer; QString name; }; struct CommandLineParseResult { enum class Status { Ok, Error, VersionRequested, HelpRequested }; Status statusCode = Status::Ok; std::optional<QString> errorString = std::nullopt; }; CommandLineParseResult parseCommandLine(QCommandLineParser &parser, DnsQuery *query) { using Status = CommandLineParseResult::Status; parser.setSingleDashWordOptionMode(QCommandLineParser::ParseAsLongOptions); const QCommandLineOption nameServerOption("n", "The name server to use.", "nameserver"); parser.addOption(nameServerOption); const QCommandLineOption typeOption("t", "The lookup type.", "type"); parser.addOption(typeOption); parser.addPositionalArgument("name", "The name to look up."); const QCommandLineOption helpOption = parser.addHelpOption(); const QCommandLineOption versionOption = parser.addVersionOption(); if (!parser.parse(QCoreApplication::arguments())) return { Status::Error, parser.errorText() }; if (parser.isSet(versionOption)) return { Status::VersionRequested }; if (parser.isSet(helpOption)) return { Status::HelpRequested }; if (parser.isSet(nameServerOption)) { const QString nameserver = parser.value(nameServerOption); query->nameServer = QHostAddress(nameserver); if (query->nameServer.isNull() || query->nameServer.protocol() == QAbstractSocket::UnknownNetworkLayerProtocol) { return { Status::Error, u"Bad nameserver address: %1"_s.arg(nameserver) }; } } if (parser.isSet(typeOption)) { const QString typeParameter = parser.value(typeOption); if (std::optional<QDnsLookup::Type> type = typeFromParameter(typeParameter)) query->type = *type; else return { Status::Error, u"Bad record type: %1"_s.arg(typeParameter) }; } const QStringList positionalArguments = parser.positionalArguments(); if (positionalArguments.isEmpty()) return { Status::Error, u"Argument 'name' missing."_s }; if (positionalArguments.size() > 1) return { Status::Error, u"Several 'name' arguments specified."_s }; query->name = positionalArguments.first(); return { Status::Ok }; }
在主函数中,如果通过了帮助选项,则应将帮助信息打印到标准输出中,应用程序应返回退出代码 0。
如果检测到错误,则应将错误信息打印到标准错误输出中,应用程序应返回 0 以外的退出代码。
QCoreApplication::setApplicationVersion(QT_VERSION_STR); QCoreApplication::setApplicationName(QCoreApplication::translate("QDnsLookupExample", "DNS Lookup Example")); QCommandLineParser parser; parser.setApplicationDescription(QCoreApplication::translate("QDnsLookupExample", "An example demonstrating the " "class QDnsLookup.")); DnsQuery query; using Status = CommandLineParseResult::Status; CommandLineParseResult parseResult = parseCommandLine(parser, &query); switch (parseResult.statusCode) { case Status::Ok: break; case Status::Error: std::fputs(qPrintable(parseResult.errorString.value_or(u"Unknown error occurred"_s)), stderr); std::fputs("\n\n", stderr); std::fputs(qPrintable(parser.helpText()), stderr); return 1; case Status::VersionRequested: parser.showVersion(); Q_UNREACHABLE_RETURN(0); case Status::HelpRequested: parser.showHelp(); Q_UNREACHABLE_RETURN(0); }
这里需要考虑的一个特殊情况是 Windows 和移动平台上的图形用户界面应用程序。这些应用程序可能不会使用标准输出或错误通道,因为输出要么被丢弃,要么无法访问。
在 Windows 平台上,如果无法获得控制台窗口,QCommandLineParser 会使用消息框来显示使用信息和错误。可以通过设置QT_COMMAND_LINE_PARSER_NO_GUI_MESSAGE_BOXES
环境变量来省略这些消息框。
对于其他平台,建议使用QMessageBox 来显示帮助文本和错误信息。为保留帮助文本的格式,应使用带有<pre>
元素的富文本:
switch (parseResult.statusCode) { case Status::Ok: break; case Status::Error: { QString errorMessage = parseResult.errorString.value_or(u"Unknown error occurred"_qs); QMessageBox::warning(0, QGuiApplication::applicationDisplayName(), "<html><head/><body><h2>" + errorMessage + "</h2><pre>" + parser.helpText() + "</pre></body></html>"); return 1; } case Status::VersionRequested: QMessageBox::information(0, QGuiApplication::applicationDisplayName(), QGuiApplication::applicationDisplayName() + ' ' + QCoreApplication::applicationVersion()); return 0; case Status::HelpRequested: QMessageBox::warning(0, QGuiApplication::applicationDisplayName(), "<html><head/><body><pre>" + parser.helpText() + "</pre></body></html>"); return 0; }
不过,这不适用于 dnslookup 示例,因为它是一个控制台应用程序。
另请参阅 QCommandLineOption 和QCoreApplication 。
成员类型文档
[since 6.9]
enum class QCommandLineParser::MessageType
枚举用于指定信息的类型以及向用户显示信息的方式。
常数 | 值 | 说明 |
---|---|---|
QCommandLineParser::MessageType::Information | 0 | 用于显示信息消息。信息将打印到stdout 。 |
QCommandLineParser::MessageType::Error | 1 | 用于显示错误信息。该消息将被打印到stderr 。 |
该枚举在 Qt 6.9 中引入。
另请参阅 showMessageAndExit().
enum QCommandLineParser::OptionsAfterPositionalArgumentsMode
该枚举描述了解析器解释出现在位置参数之后的选项的方式。
常量 | 值 | 说明 |
---|---|---|
QCommandLineParser::ParseAsOptions | 0 | application argument --opt -t 被解释为设置 和 选项,就像 所做的那样。这是默认解析模式。要指定 和 为位置参数,用户可以使用 ,如 。opt t application --opt -t argument --opt -t -- application argument -- --opt -t |
QCommandLineParser::ParseAsPositionalArguments | 1 | application argument --opt 则被解释为有两个位置参数,即 和 。该模式适用于旨在启动其他可执行文件(如包装器、调试工具等)或支持内部命令(后面跟有命令选项)的可执行文件。 是命令的名称,在它后面出现的所有选项可由另一个命令行分析器收集和分析,可能在另一个可执行文件中。argument --opt argument |
另请参阅 setOptionsAfterPositionalArgumentsMode() 。
enum QCommandLineParser::SingleDashWordOptionMode
该枚举描述了解析器对使用单破折号后跟多个字母的命令行选项的解释方式,如-abc
。
常量 | 值 | 说明 |
---|---|---|
QCommandLineParser::ParseAsCompactedShortOptions | 0 | -abc 如果所有选项都不取值,则被解释为 ,即在命令行上压缩的三个短选项。如果 有一个值,则解释为 ,即短选项 后接一个值 。这通常用于类似编译器的工具,以处理 或 等选项。 这是默认的解析模式。建议新应用程序使用这种模式。-a -b -c a -a bc a bc -DDEFINE=VALUE -I/include/path |
QCommandLineParser::ParseAsLongOptions | 1 | -abc 被解释为 ,即名为 的长选项。Qt 自己的工具(uic、rcc......)一直都是这样解析参数的。该模式应用于保持与以这种方式解析参数的应用程序的兼容性。如果 选项设置了 标志,则是一个例外,在这种情况下,它仍被解释为 。--abc abc a QCommandLineOption::ShortOptionStyle -a bc |
另请参阅 setSingleDashWordOptionMode() 。
成员函数文档
QCommandLineParser::QCommandLineParser()
构造一个命令行分析器对象。
[noexcept]
QCommandLineParser::~QCommandLineParser()
销毁命令行分析器对象。
QCommandLineOption QCommandLineParser::addHelpOption()
为命令行分析程序添加帮助选项。
为该命令行指定的选项由-h
或--help
描述。在 Windows 系统中,还支持另一种-?
。--help-all
选项对其进行了扩展,在输出中包含了该命令未定义的通用 Qt XML 选项。
QCommandLineParser 会自动处理这些选项。
切记使用setApplicationDescription() 设置应用程序说明,使用该选项时将显示应用程序说明。
示例
int main(int argc, char *argv[]) { QCoreApplication app(argc, argv); QCoreApplication::setApplicationName("my-copy-program"); QCoreApplication::setApplicationVersion("1.0"); QCommandLineParser parser; parser.setApplicationDescription("Test helper"); parser.addHelpOption(); parser.addVersionOption(); parser.addPositionalArgument("source", QCoreApplication::translate("main", "Source file to copy.")); parser.addPositionalArgument("destination", QCoreApplication::translate("main", "Destination directory.")); // A boolean option with a single name (-p) QCommandLineOption showProgressOption("p", QCoreApplication::translate("main", "Show progress during copy")); parser.addOption(showProgressOption); // A boolean option with multiple names (-f, --force) QCommandLineOption forceOption(QStringList() << "f" << "force", QCoreApplication::translate("main", "Overwrite existing files.")); parser.addOption(forceOption); // An option with a value QCommandLineOption targetDirectoryOption(QStringList() << "t" << "target-directory", QCoreApplication::translate("main", "Copy all source files into <directory>."), QCoreApplication::translate("main", "directory")); parser.addOption(targetDirectoryOption); // Process the actual command line arguments given by the user parser.process(app); const QStringList args = parser.positionalArguments(); // source is args.at(0), destination is args.at(1) bool showProgress = parser.isSet(showProgressOption); bool force = parser.isSet(forceOption); QString targetDir = parser.value(targetDirectoryOption); // ... }
返回可用于调用isSet() 的选项实例。
bool QCommandLineParser::addOption(const QCommandLineOption &option)
添加option 选项,以便在解析时查找。
如果添加选项成功,则返回true
;否则返回false
。
如果选项没有附加名称,或者选项名称与之前添加的选项名称冲突,则添加选项失败。
bool QCommandLineParser::addOptions(const QList<QCommandLineOption> &options)
添加解析时要查找的选项。选项由参数options 指定。
如果添加所有选项成功,则返回true
;否则返回false
。
有关此函数可能失败的情况,请参阅addOption() 文档。
void QCommandLineParser::addPositionalArgument(const QString &name, const QString &description, const QString &syntax = QString())
定义应用程序的附加参数,以方便帮助文本。
参数name 和description 将出现在帮助的Arguments:
部分。如果指定了syntax ,它将被附加到 "使用说明 "行,否则name 将被附加。
示例
// Usage: image-editor file // // Arguments: // file The file to open. parser.addPositionalArgument("file", QCoreApplication::translate("main", "The file to open.")); // Usage: web-browser [urls...] // // Arguments: // urls URLs to open, optionally. parser.addPositionalArgument("urls", QCoreApplication::translate("main", "URLs to open, optionally."), "[urls...]"); // Usage: cp source destination // // Arguments: // source Source file to copy. // destination Destination directory. parser.addPositionalArgument("source", QCoreApplication::translate("main", "Source file to copy.")); parser.addPositionalArgument("destination", QCoreApplication::translate("main", "Destination directory."));
另请参阅 addHelpOption() 和helpText()。
QCommandLineOption QCommandLineParser::addVersionOption()
添加-v
/--version
选项,显示应用程序的版本字符串。
该选项由QCommandLineParser 自动处理。
您可以使用QCoreApplication::setApplicationVersion() 设置实际版本字符串。
返回可用于调用isSet() 的选项实例。
QString QCommandLineParser::applicationDescription() const
返回在setApplicationDescription() 中设置的应用程序说明。
另请参阅 setApplicationDescription()。
void QCommandLineParser::clearPositionalArguments()
清除帮助文本中的附加参数定义。
只有支持具有不同选项的多个命令的工具才需要这样做。一旦确定了实际命令,就可以定义该命令的选项,并相应调整该命令的帮助文本。
例如
QCoreApplication app(argc, argv); QCommandLineParser parser; parser.addPositionalArgument("command", "The command to execute."); // Call parse() to find out the positional arguments. parser.parse(QCoreApplication::arguments()); const QStringList args = parser.positionalArguments(); const QString command = args.isEmpty() ? QString() : args.first(); if (command == "resize") { parser.clearPositionalArguments(); parser.addPositionalArgument("resize", "Resize the object to a new size.", "resize [resize_options]"); parser.addOption(QCommandLineOption("size", "New size.", "new_size")); parser.process(app); // ... } /* This code results in context-dependent help: $ tool --help Usage: tool command Arguments: command The command to execute. $ tool resize --help Usage: tool resize [resize_options] Options: --size <size> New size. Arguments: resize Resize the object to a new size. */
QString QCommandLineParser::errorText() const
为用户返回翻译后的错误文本。只有当parse() 返回false
时,才应调用此功能。
QString QCommandLineParser::helpText() const
返回包含完整帮助信息的字符串。
另请参阅 showHelp().
bool QCommandLineParser::isSet(const QString &name) const
检查name 选项是否已传递给应用程序。
如果设置了name ,则返回true
,否则返回 false。
提供的名称可以是使用addOption() 添加的任何选项的长或短名称。所有选项名称都被视为等价。如果名称无法识别或选项不存在,则返回 false。
示例
bool verbose = parser.isSet("verbose");
bool QCommandLineParser::isSet(const QCommandLineOption &option) const
这是一个重载函数。
检查option 是否已传递给应用程序。
如果设置了option ,则返回true
,否则返回 false。
这是检查无值选项的推荐方法。
示例
QCoreApplication app(argc, argv); QCommandLineParser parser; QCommandLineOption verboseOption("verbose"); parser.addOption(verboseOption); parser.process(app); bool verbose = parser.isSet(verboseOption);
QStringList QCommandLineParser::optionNames() const
返回找到的选项名称列表。
返回解析器找到的所有已识别选项名称的列表,按找到的顺序排列。对于格式为 {-option=value} 的长选项,value 部分将被删除。
该列表中的名称不包括前面的破折号字符。如果解析器不止一次遇到名称,则名称可能会在列表中出现多次。
列表中的任何条目都可以与value() 或values() 一起使用,以获取任何相关的选项值。
bool QCommandLineParser::parse(const QStringList &arguments)
解析命令行arguments 。
大多数程序不需要调用它,只需调用process() 即可。
parse()比较低级,只进行解析。如果 parse() 返回false
,应用程序必须使用errorText() 处理错误。例如,在图形化程序中显示图形化错误信息就很有用。
调用 parse() 而不是process() 也有助于暂时忽略未知选项,因为在调用process() 之前,稍后会提供更多选项定义(取决于其中一个参数)。
不要忘记,arguments 必须以可执行文件的名称开头(但会被忽略)。
如果出现解析错误(未知选项或缺少值),则返回false
;否则返回true
。
另请参见 process().
QStringList QCommandLineParser::positionalArguments() const
返回位置参数列表。
这些参数是所有未被识别为选项的参数。
void QCommandLineParser::process(const QStringList &arguments)
处理命令行arguments 。
除了解析选项(如parse()) 之外,该函数还处理内置选项并处理错误。
如果调用了addVersionOption ,则内置选项为--version
;如果调用了addHelpOption ,则内置选项为--help
/--help-all
。
调用这些选项之一或发生错误(例如传递了未知选项)时,当前进程将使用 exit() 函数停止运行。
另请参见 QCoreApplication::arguments() 和parse()。
void QCommandLineParser::process(const QCoreApplication &app)
这是一个重载函数。
命令行从QCoreApplication 实例app 获取。
void QCommandLineParser::setApplicationDescription(const QString &description)
设置helpText() 显示的应用程序description 。
另请参阅 applicationDescription().
void QCommandLineParser::setOptionsAfterPositionalArgumentsMode(QCommandLineParser::OptionsAfterPositionalArgumentsMode parsingMode)
将解析模式设为parsingMode 。必须在process() 或parse() 之前调用。
void QCommandLineParser::setSingleDashWordOptionMode(QCommandLineParser::SingleDashWordOptionMode singleDashWordOptionMode)
将解析模式设为singleDashWordOptionMode 。必须在process() 或parse() 之前调用。
void QCommandLineParser::showHelp(int exitCode = 0)
显示帮助信息并退出应用程序。这是由 -help 选项自动触发的,但也可用于在用户未正确调用程序时显示帮助。退出代码设置为exitCode 。如果用户要求查看帮助,则应将其设置为 0,如果出现错误,则应将其设置为任何其他值。
另请参阅 helpText() 和showMessageAndExit()。
[static, since 6.9]
void QCommandLineParser::showMessageAndExit(QCommandLineParser::MessageType type, const QString &message, int exitCode = 0)
显示message ,并以给定的exitCode 退出应用程序。
message 通常会根据给定的type 直接打印到stdout
或stderr
,或者在必要时显示在 Windows 下的消息框中,并根据给定的type 显示信息图标或错误图标(如果不需要消息框,请设置QT_COMMAND_LINE_PARSER_NO_GUI_MESSAGE_BOXES
环境变量)。
与showHelp 、showVersion 和内置选项(调用addVersionOption 时显示--version
,调用addHelpOption 时显示--help
/--help-all
)使用的信息显示方式相同。
该函数在 Qt 6.9 中引入。
另请参阅 addVersionOption()、showHelp()、showVersion() 和QCommandLineParser::MessageType 。
void QCommandLineParser::showVersion()
显示QCoreApplication::applicationVersion() 中的版本信息,并退出应用程序。这将由 -version 选项自动触发,但也可以在不使用process() 时显示版本信息。退出代码被设置为 EXIT_SUCCESS (0)。
另请参见 addVersionOption()。
QStringList QCommandLineParser::unknownOptionNames() const
返回未知选项名称列表。
该列表将包括无法识别的长名和短名选项。对于{-option=value}形式的长选项,value部分将被去掉,只添加长名称。
该列表中的名称不包括前面的破折号字符。如果解析器不止一次遇到名称,则名称可能会在此列表中出现多次。
另请参阅 optionNames()。
QString QCommandLineParser::value(const QString &optionName) const
返回为给定选项名称optionName 找到的选项值,如果找不到则返回空字符串。
所提供的名称可以是使用addOption() 添加的任何选项的长或短名称。所有选项名称都被视为等价。如果无法识别该名称或该选项不存在,则返回空字符串。
对于解析器找到的选项,将返回该选项的最后一个值。如果命令行中没有指定该选项,则返回默认值。
如果选项没有取值,将打印警告并返回空字符串。
另请参见 values()、QCommandLineOption::setDefaultValue() 和QCommandLineOption::setDefaultValues()。
QString QCommandLineParser::value(const QCommandLineOption &option) const
这是一个重载函数。
返回为给定option 找到的选项值,如果找不到则返回空字符串。
对于解析器找到的选项,将返回该选项的最后一个值。如果命令行未指定选项,则返回默认值。
如果选项没有值,则返回空字符串。
另请参见 values()、QCommandLineOption::setDefaultValue() 和QCommandLineOption::setDefaultValues()。
QStringList QCommandLineParser::values(const QString &optionName) const
Returns a list of option values found for the given option nameoptionName, or an empty list if not found.
所提供的名称可以是使用addOption() 添加的任何选项的长或短名称。所有选项名称都被视为等价。如果名称不被识别或不存在该选项,则返回空列表。
对于解析器找到的选项,解析器每次遇到该选项时,列表都会包含一个条目。如果命令行中没有指定选项,则返回默认值。
如果选项没有取值,则返回空列表。
另请参见 value()、QCommandLineOption::setDefaultValue() 和QCommandLineOption::setDefaultValues()。
QStringList QCommandLineParser::values(const QCommandLineOption &option) const
这是一个重载函数。
返回为给定option 找到的选项值列表,如果未找到,则返回空列表。
对于解析器找到的选项,解析器每次遇到该选项时,列表都会包含一个条目。如果命令行中没有指定选项,则返回默认值。
如果选项没有取值,则返回空列表。
另请参见 value()、QCommandLineOption::setDefaultValue() 和QCommandLineOption::setDefaultValues()。
© 2025 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.