Qt:QCommandLineOption类

1059 篇文章 289 订阅
QCommandLineOption是Qt库中的一个类,用于处理命令行选项。它可以定义具有多个别名的选项,设置描述、值名称和默认值。该类支持创建需要或不需要值的选项,并在用户未指定时使用默认值。QCommandLineOption与QCommandLineParser配合使用,方便地解析和显示命令行帮助信息。
摘要由CSDN通过智能技术生成

QCommandLineOption类定义了可能的命令行选项

头文件:

#include <QCommandLineOption>

cmake:

find_package(Qt6 COMPONENTS Core REQUIRED)
target_link_libraries(mytarget PRIVATE Qt6::Core)

qmake:

QT += core

引入:

Qt 5.2

详细说明

该类用于描述命令行上的选项。它允许用不同的方式定义同一个选项,并可能使用多个别名。它也用于描述如何使用选项-它可以是一个标志(例如-v)或一个值(例如-o file)。

例子:

QCommandLineOption verboseOption("verbose", "Verbose mode. Prints out more information.");
QCommandLineOption outputOption(QStringList() << "o" << "output", "Write generated data into <file>.", "file");

公开类型

成员函数

QCommandLineOption

QCommandLineOption::QCommandLineOption(const QCommandLineOption &other)

构造一个QCommandLineOption对象,该对象是QCommandLineOption对象other的副本。

参见操作符 operator=().。

QCommandLineOption::QCommandLineOption(const QStringList &names, const QString &description, const QString &valueName = QString(), const QString &defaultValue = QString())

使用给定的参数构造一个命令行选项对象。

此重载允许为选项设置多个名称,例如o和output。

选项的名称设置为names。名字可以短也可以长。列表中长度为一个字符的任何名称都是短名称。选项名称不能为空,不能以破折号或斜杠开头,不能包含=并且不能重复。

描述设置为description。通常在描述的最后加上一个“.”。

此外,如果该选项需要一个值,则需要设置valueName。该选项的默认值设置为defaultValue。

在5.4之前的Qt版本中,这个构造函数是explicit的。它不再是并且可以用于C ++ 11样式的统一初始化:

QCommandLineParser parser;
parser.addOption({{"o", "output"}, "Write generated data into <file>.", "file"});

另请参见setDescription(),setValueName()和setDefaultValues()。

QCommandLineOption::QCommandLineOption(const QString &name, const QString &description, const QString &valueName = QString(), const QString &defaultValue = QString())

使用给定的参数构造一个命令行选项对象。

此重载允许为选项设置多个名称,例如o和output。

选项的名称设置为names。名字可以短也可以长。列表中长度为一个字符的任何名称都是短名称。选项名称不能为空,不能以破折号或斜杠开头,不能包含=并且不能重复。

描述设置为description。通常在描述的最后加上一个“.”。

此外,如果该选项需要一个值,则需要设置valueName。该选项的默认值设置为defaultValue。

在5.4之前的Qt版本中,这个构造函数是explicit的。它不再是并且可以用于C ++ 11样式的统一初始化:

QCommandLineParser parser;
parser.addOption({"verbose", "Verbose mode. Prints out more information."});

另请参见setDescription(),setValueName()和setDefaultValues()。

QCommandLineOption::QCommandLineOption(const QStringList &names)

构造一个具有名称的命令行选项对象。

这个重载允许为选项设置多个名称,例如o和output。

名字可以短也可以长。列表中长度为一个字符的任何名称都是短名称。选项名称不能为空,不能以破折号或斜杠开头,不能包含=并且不能重复。

请参阅setDescription(), setValueName(),和setDefaultValues()。

QCommandLineOption::QCommandLineOption(const QString &name)

构造名称为name的命令行选项对象。

名称可以短也可以长。如果名称长度为一个字符,则认为它是一个短名称。选项名称不能为空,不能以破折号或斜杠开头,不能包含=并且不能重复。

请参阅setDescription(), setValueName(),和setDefaultValues()。

operator=

[since 5.2]
QCommandLineOption &QCommandLineOption::operator=(QCommandLineOption &&other)

移动其他对象到此QCommandLineOption实例。

此功能是在Qt 5.2中引入的。

QCommandLineOption &QCommandLineOption::operator=(const QCommandLineOption &other)

制作另一个对象的副本,并将其分配给此QCommandLineOption对象。

~QCommandLineOption

QCommandLineOption::~QCommandLineOption()

销毁命令行选项对象。

defaultValues

QStringList QCommandLineOption::defaultValues() const

返回为此选项设置的默认值。

参见setDefaultValues()。

description

QString QCommandLineOption::description() const

返回此选项的描述集。

参见setDescription()。

flags

[since 5.8]QCommandLineOption::Flags QCommandLineOption::flags() const

返回一组影响此命令行选项的标志。

这个函数是在Qt 5.8中引入的。

参见setFlags()和QCommandLineOption::Flags。

names

QStringList QCommandLineOption::names() const

返回为此选项设置的名称。

setDefaultValue

void QCommandLineOption::setDefaultValue(const QString &defaultValue)

将此选项的默认值设置为defaultValue。

如果应用程序的用户没有在命令行上指定该选项,则使用默认值。

如果defaultValue为空,则该选项没有缺省值。

请参阅defaultValues()和setDefaultValues()。

void QCommandLineOption::setDefaultValues(const QStringList &defaultValues)

将此选项使用的默认值列表设置为defaultValues。

如果应用程序的用户没有在命令行上指定该选项,则使用默认值。

请参阅defaultValues()和setDefaultValue()。

setDescription

void QCommandLineOption::setDescription(const QString &description)

将用于此选项的描述设置为description。

习惯上,在描述的最后加上一个“.”。

该描述由QCommandLineParser::showHelp()使用。

参见描述()。

setFlags

[since 5.8]void QCommandLineOption::setFlags(QCommandLineOption::Flags flags)

将影响此命令行选项的标志设置为flags。

这个函数是在Qt 5.8中引入的。

参见flags()和QCommandLineOption:: flags。

setValueName

void QCommandLineOption::setValueName(const QString &valueName)

将文档的期望值的名称设置为valueName。

没有分配值的选项具有类似布尔的行为:用户指定–option或不指定。

分配了值的选项需要为期望值设置名称,以便在帮助输出中记录该选项。名称为o和output且值名称为的选项file将显示为-o, --output 。

如果您希望该选项仅出现一次,则调用QCommandLineParser :: value();如果您希望该选项多次出现,则调用QCommandLineParser :: values()。

另请参见valueName()。

swap

void QCommandLineOption::swap(QCommandLineOption &other)

使用此选项交换其他选项。此操作非常快,并且永远不会失败。

valueName

QString QCommandLineOption::valueName() const

返回期望值的名称。

如果为空,则该选项不接受值。

参见setValueName()。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值