QCommandLineOption Class
QCommandLineOption 类定义了一个可能的命令行选项。更多内容...
| 头文件: | #include <QCommandLineOption> |
| CMake: | find_package(Qt6 REQUIRED COMPONENTS Core) target_link_libraries(mytarget PRIVATE Qt6::Core) |
| qmake: | QT += core |
- 所有成员的列表,包括继承的成员
- QCommandLineOption 属于“隐式共享类”。
公共类型
公共函数
| QCommandLineOption(const QString &name) | |
| QCommandLineOption(const QStringList &names) | |
| QCommandLineOption(const QString &name, const QString &description, const QString &valueName = QString(), const QString &defaultValue = QString()) | |
| QCommandLineOption(const QStringList &names, const QString &description, const QString &valueName = QString(), const QString &defaultValue = QString()) | |
| QCommandLineOption(const QCommandLineOption &other) | |
| ~QCommandLineOption() | |
| QStringList | defaultValues() const |
| QString | description() const |
| QCommandLineOption::Flags | flags() const |
| QStringList | names() const |
| void | setDefaultValue(const QString &defaultValue) |
| void | setDefaultValues(const QStringList &defaultValues) |
| void | setDescription(const QString &description) |
| void | setFlags(QCommandLineOption::Flags flags) |
| void | setValueName(const QString &valueName) |
| void | swap(QCommandLineOption &other) |
| QString | valueName() const |
| QCommandLineOption & | operator=(QCommandLineOption &&other) |
| QCommandLineOption & | operator=(const QCommandLineOption &other) |
详细说明
该类用于描述命令行上的选项。它允许通过多种别名以不同方式定义同一个选项。它还用于描述选项的使用方式——选项可以是标志(例如-v ),也可以接受一个值(例如-o file )。
示例:
QCommandLineOption verboseOption("verbose", "Verbose mode. Prints out more information.");
QCommandLineOption outputOption(QStringList() << "o" << "output", "Write generated data into <file>.", "file");另请参阅 QCommandLineParser 。
成员类型文档
enum QCommandLineOption::Flag
flags QCommandLineOption::Flags
| 常数 | 值 | 描述 |
|---|---|---|
QCommandLineOption::HiddenFromHelp | 0x1 | 在用户可见的帮助输出中隐藏此选项。默认情况下,所有选项均可见。为特定选项设置此标志会将其设为内部选项,即不会在帮助输出中列出。 |
QCommandLineOption::ShortOptionStyle | 0x2 | 该选项将始终被视为短选项,无论QCommandLineParser::setSingleDashWordOptionMode 设置为何值。这使得诸如-DDEFINE=VALUE 或-I/include/path 之类的标志,即使在解析器处于QCommandLineParser::ParseAsLongOptions 模式下,也能被解释为短标志。 |
QCommandLineOption::IgnoreOptionsAfter | 0x4 | [自 6.9 起] 此选项之后将不再解析任何选项。这在需要向二级应用程序发送额外命令行参数时非常有用。如果为此选项提供了值,该值将被忽略。 |
Flags 类型是QFlags<Flag> 的 typedef。它存储 Flag 值的按“或”运算组合。
另请参阅 QCommandLineOption::setFlags() 和QCommandLineOption::flags()。
成员函数文档
[explicit] QCommandLineOption::QCommandLineOption(const QString &name)
构建一个名称为name 的命令行选项对象。
名称可以是短名称,也可以是长名称。如果名称长度为一个字符,则被视为短名称。选项名称不能为空,不能以连字符或斜杠开头,不能包含= ,且不能重复。
另请参阅 setDescription()、setValueName() 和setDefaultValues()。
[explicit] QCommandLineOption::QCommandLineOption(const QStringList &names)
构建一个名称为names 的命令行选项对象。
此重载允许为选项设置多个名称,例如o 和output 。
名称可以是短名称,也可以是长名称。列表中长度为一个字符的名称即为短名称。选项名称不能为空,不能以连字符或斜杠开头,不能包含= ,且不能重复。
另请参阅 setDescription()、setValueName() 和setDefaultValues()。
QCommandLineOption::QCommandLineOption(const QString &name, const QString &description, const QString &valueName = QString(), const QString &defaultValue = QString())
根据给定的参数构建一个命令行选项对象。
选项名称设置为name 。名称可以是短名称,也可以是长名称。如果名称长度为一个字符,则被视为短名称。选项名称不能为空,不能以连字符或斜杠开头,不能包含= ,且不能重复。
描述字段被设置为description 。通常会在描述末尾添加一个“.”。
此外,如果选项需要指定值,则需设置valueName 。该选项的默认值设为defaultValue 。
在 Qt 5.4 之前的版本中,该构造函数为explicit 。在 Qt 5.4 及后续版本中,情况已发生变化,该构造函数可用于统一初始化:
QCommandLineParser parser;
parser.addOption({"verbose", "Verbose mode. Prints out more information."});另请参阅 setDescription()、setValueName() 和setDefaultValues()。
QCommandLineOption::QCommandLineOption(const QStringList &names, const QString &description, const QString &valueName = QString(), const QString &defaultValue = QString())
根据给定的参数构建一个命令行选项对象。
此重载允许为选项设置多个名称,例如o 和output 。
选项的名称被设置为names 。名称可以是短名称,也可以是长名称。列表中长度为一个字符的名称即为短名称。选项名称不能为空,不能以连字符或斜杠开头,不能包含= ,且不能重复。
描述字段的值应设置为description 。通常会在描述末尾添加一个“.”。
此外,如果选项需要指定值,则需设置valueName 。该选项的默认值设置为defaultValue 。
在 Qt 5.4 之前的版本中,该构造函数为explicit 。在 Qt 5.4 及后续版本中,情况已发生变化,该构造函数可用于统一初始化:
QCommandLineParser parser;
parser.addOption({{"o", "output"}, "Write generated data into <file>.", "file"});另请参阅 setDescription()、setValueName() 和setDefaultValues()。
QCommandLineOption::QCommandLineOption(const QCommandLineOption &other)
创建一个 QCommandLineOption 对象,该对象是 QCommandLineOption 对象other 的副本。
另请参阅 operator=()。
[noexcept] QCommandLineOption::~QCommandLineOption()
销毁该命令行选项对象。
QStringList QCommandLineOption::defaultValues() const
返回为该选项设置的默认值。
另请参阅 setDefaultValues()。
QString QCommandLineOption::description() const
返回为此选项设置的描述。
另请参阅 setDescription()。
QCommandLineOption::Flags QCommandLineOption::flags() const
返回一组影响此命令行选项的标志。
另请参阅 setFlags() 和QCommandLineOption::Flags 。
QStringList QCommandLineOption::names() const
返回为该选项设置的名称。
void QCommandLineOption::setDefaultValue(const QString &defaultValue)
将此选项的默认值设置为defaultValue 。
如果应用程序的用户未在命令行中指定该选项,则使用默认值。
如果defaultValue 为空,则该选项没有默认值。
另请参阅 defaultValues() 和setDefaultValues()。
void QCommandLineOption::setDefaultValues(const QStringList &defaultValues)
将此选项使用的默认值列表设置为defaultValues 。
如果应用程序的用户未在命令行中指定该选项,则使用默认值。
另请参阅 defaultValues() 和setDefaultValue()。
void QCommandLineOption::setDescription(const QString &description)
将此选项的描述设置为description 。
通常会在描述末尾添加一个“.”。
该描述由QCommandLineParser::showHelp() 函数使用。
另请参阅 description()。
void QCommandLineOption::setFlags(QCommandLineOption::Flags flags)
将影响此命令行选项的标志集设置为flags 。
另请参阅 flags() 和QCommandLineOption::Flags 。
void QCommandLineOption::setValueName(const QString &valueName)
将预期值的名称(用于文档)设置为valueName 。
未分配值的选项具有布尔类型的行为:用户要么指定了 –option,要么未指定。
已分配值的选项需要为预期值设置名称,以便在帮助输出中记录该选项的文档。一个名称分别为o 和output 、值名为file 的选项,将显示为-o, --output <file> 。
若预期该选项仅出现一次,请调用QCommandLineParser::value();若预期该选项出现多次,请调用QCommandLineParser::values()。
另请参阅 valueName()。
[noexcept] void QCommandLineOption::swap(QCommandLineOption &other)
将此选项与other 互换。此操作速度极快,且绝不会失败。
QString QCommandLineOption::valueName() const
返回预期值的名称。
如果为空,则该选项不接受任何值。
另请参阅 setValueName()。
[noexcept] QCommandLineOption &QCommandLineOption::operator=(QCommandLineOption &&other)
将other 通过Move操作赋值给此QCommandLineOption 实例。
QCommandLineOption &QCommandLineOption::operator=(const QCommandLineOption &other)
复制other 对象,并将副本赋值给此QCommandLineOption 对象。
© 2026 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.