6.0.0-rc4Source

QCommandLineOption Class

The QCommandLineOption class defines a possible command-line option. More…

Header#include <QCommandLineOption>
CMaketarget_link_libraries(app PRIVATE Nt::Core)
ModuleNt Core C++ Classes

List of all members, including inherited members

Public Types

enumFlag
typedefFlags

Public Functions

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())
QStringListnames() const
voidsetValueName(const QString &name)
QStringvalueName() const
voidsetDescription(const QString &description)
QStringdescription() const
voidsetDefaultValue(const QString &value)
voidsetDefaultValues(const QStringList &values)
QStringListdefaultValues() const
Flagsflags() const
voidsetFlags(Flags f)
voidswap(QCommandLineOption &other)

Detailed Description

An option has one or more names, a description for the help text and optionally a value. Add options to a QCommandLineParser, then query the parser after parsing:

QCommandLineParser parser;
QCommandLineOption output({"o", "output"}, "Write to <file>.", "file");
parser.addOption(output);
parser.process(QCoreApplication::arguments());
QString path = parser.value(output);

Names that are empty, start with - or contain = are not valid; they are dropped with a warning.

See also QCommandLineParser.

Differences from Qt

There is no swap() and no setHidden() or isHidden(); use the HiddenFromHelp flag instead.

Member Type Documentation

enum QCommandLineOption::Flag

Flags that change how an option is shown in the help text.

ConstantDescription
QCommandLineOption::HiddenFromHelpThe option is left out of QCommandLineParser::helpText().
QCommandLineOption::ShortOptionStyleThe option's names are all shown with a single dash, even the long ones.

typedef QCommandLineOption::Flags

The Flags type is a typedef for QFlags<Flag>. It stores an OR combination of Flag values.

Member Function Documentation

[explicit] QCommandLineOption::QCommandLineOption(const QString &name)

Constructs a command line option with the single name name. The option takes no value.

[explicit] QCommandLineOption::QCommandLineOption(const QStringList &names)

Constructs a command line option with the names names. Names that are not valid are dropped. The option takes no value.

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

This is an overloaded function.

Constructs a command line option with the single name name, the help text description and, if valueName is not empty, a value with that name and the default defaultValue.

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

This is an overloaded function.

Constructs a command line option with the names names and the help text description. If valueName is not empty the option takes a value with that name, and defaultValue is used when it is not given. Names that are not valid are dropped.

QStringList QCommandLineOption::defaultValues() const

Returns the default values of the option, which are empty unless set.

See also setDefaultValues().

QString QCommandLineOption::description() const

Returns the description shown in the help text.

See also setDescription().

Flags QCommandLineOption::flags() const

Returns the flags of the option.

See also setFlags().

QStringList QCommandLineOption::names() const

Returns the names of the option. A name of one character is a short option, a longer name a long option.

void QCommandLineOption::setDefaultValue(const QString &value)

Sets the default value of the option to value, which is used when the option is not given. An empty value clears the defaults.

See also setDefaultValues() and defaultValues().

void QCommandLineOption::setDefaultValues(const QStringList &values)

Sets the default values of the option to values.

See also setDefaultValue() and defaultValues().

void QCommandLineOption::setDescription(const QString &description)

Sets the description shown in the help text to description.

See also description().

void QCommandLineOption::setFlags(Flags f)

Sets the flags of the option to f.

See also flags().

void QCommandLineOption::setValueName(const QString &name)

Sets the name of the option's value to name. An option with a non-empty value name takes a value; the name is shown in the help text, as in --output <name>.

See also valueName().

void QCommandLineOption::swap(QCommandLineOption &other)

Swaps this option with other.

QString QCommandLineOption::valueName() const

Returns the name of the option's value, or an empty string if the option takes no value.

See also setValueName().