8#include <private/qcoreapplication_p.h>
13#if defined(Q_OS_WIN) && !defined(QT_BOOTSTRAPPED)
44 QStringList::const_iterator *argumentIterator,
45 QStringList::const_iterator argsEnd);
337 qWarning() <<
"QCommandLineParser: already having an option named" <<
name;
419 tr(
"Displays help, including generic Qt options."));
457 arg.description = description;
519#if defined(Q_OS_WIN) && !defined(QT_BOOTSTRAPPED)
522static inline bool displayMessageBox()
524 if (GetConsoleWindow()
527 STARTUPINFO startupInfo;
528 startupInfo.cb =
sizeof(STARTUPINFO);
529 GetStartupInfo(&startupInfo);
530 return !(startupInfo.dwFlags & STARTF_USESTDHANDLES);
536#if defined(Q_OS_WIN) && !defined(QT_BOOTSTRAPPED)
537 if (displayMessageBox()) {
538 const UINT
flags = MB_OK | MB_TOPMOST | MB_SETFOREGROUND
545 MessageBoxW(0,
reinterpret_cast<const wchar_t *
>(
message.utf16()),
572 ::exit(EXIT_FAILURE);
600 qWarning(
"QCommandLineParser: call process() or parse() before %s",
method);
632 QStringList::const_iterator *argumentIterator, QStringList::const_iterator argsEnd)
641 if (assignPos == -1) {
642 ++(*argumentIterator);
643 if (*argumentIterator == argsEnd) {
652 if (assignPos != -1) {
653 errorText = QCommandLineParser::tr(
"Unexpected value after '%1'.").
arg(
argument.left(assignPos));
680 bool forcePositional =
false;
688 qWarning(
"QCommandLineParser: argument list cannot be empty, it should contain at least the executable name");
692 QStringList::const_iterator argumentIterator =
args.
begin();
695 for (; argumentIterator !=
args.
end() ; ++argumentIterator) {
698 if (forcePositional) {
700 }
else if (
argument.startsWith(
"--"_L1)) {
710 forcePositional =
true;
712 }
else if (
argument.startsWith(dashChar)) {
721 bool valueFound =
false;
775 forcePositional =
true;
777 if (argumentIterator ==
args.
end())
804 if (
aliases.contains(optionName))
833 if (!valueList.isEmpty())
834 return valueList.last();
866 if (
option.valueName().isEmpty()) {
867 qWarning(
"QCommandLineParser: option not expecting values: \"%ls\"",
1004 ::exit(EXIT_SUCCESS);
1042 const auto nl = u
'\n';
1043 const auto indentation =
" "_L1;
1047 auto nextNameSection = [&]() {
1049 nameIndex += section.
size();
1056 const int max = 79 - (indentation.size() + optionNameMaxWidth + 1);
1068 if (
x > max && lastBreakable != -1) {
1070 breakAt = lastBreakable;
1071 nextLineStart = lastBreakable + 1;
1072 }
else if ((
x > max - 1 && lastBreakable == -1) ||
i ==
len - 1) {
1075 nextLineStart = breakAt;
1076 }
else if (
c == nl) {
1079 nextLineStart =
i + 1;
1082 if (breakAt != -1) {
1083 const qsizetype numChars = breakAt - lineStart;
1085 text += indentation + nextNameSection().
leftJustified(optionNameMaxWidth) + u
' ';
1089 lineStart = nextLineStart;
1090 if (lineStart <
len && description.
at(lineStart).isSpace())
1096 while (nameIndex <
names.size()) {
1097 text += indentation + nextNameSection() + nl;
1112 if (includeQtOptions &&
qApp)
1113 qApp->d_func()->addQtOptions(&options);
1114 if (!options.isEmpty())
1115 usage += u
' ' + QCommandLineParser::tr(
"[options]");
1118 text += QCommandLineParser::tr(
"Usage: %1").
arg(
usage) + nl;
1122 if (!options.isEmpty())
1123 text += QCommandLineParser::tr(
"Options:") + nl;
1125 optionNameList.reserve(options.size());
1133 const int numDashes = optionName.
size() == 1 ? 1 : 2;
1137 optionNamesString.
chop(2);
1138 const auto valueName =
option.valueName();
1139 if (!valueName.isEmpty())
1140 optionNamesString +=
" <"_L1 + valueName + u
'>';
1141 optionNameList.append(optionNamesString);
1142 longestOptionNameString =
qMax(longestOptionNameString, optionNamesString.size());
1144 ++longestOptionNameString;
1145 const int optionNameMaxWidth =
qMin(50,
int(longestOptionNameString));
1146 auto optionNameIterator = optionNameList.cbegin();
1151 ++optionNameIterator;
1154 if (!options.isEmpty())
1156 text += QCommandLineParser::tr(
"Arguments:") + nl;
The QCommandLineOption class defines a possible command-line option. \inmodule QtCore.
QString valueName() const
Returns the name of the expected value.
QStringList names() const
Returns the names set for this option.
QStringList aliases(const QString &name) const
bool parseOptionValue(const QString &optionName, const QString &argument, QStringList::const_iterator *argumentIterator, QStringList::const_iterator argsEnd)
Parse the value for a given option, if it was defined to expect one.
bool registerFoundOption(const QString &optionName)
NameHash_t nameHash
Hash mapping option names to their offsets in commandLineOptionList and optionArgumentList.
bool builtinVersionOption
Whether addVersionOption was called.
QHash< qsizetype, QStringList > optionValuesHash
Option values found (only for options with a value)
QStringList optionNames
Names of options found on the command line.
QString helpText(bool includeQtOptions) const
QCommandLineParserPrivate()
QString description
Application description.
bool needsParsing
True if parse() needs to be called.
void checkParsed(const char *method)
QCommandLineParser::SingleDashWordOptionMode singleDashWordOptionMode
The parsing mode for "-abc".
bool parse(const QStringList &args)
bool builtinHelpOption
Whether addHelpOption was called.
QStringList unknownOptionNames
Names of options which were unknown.
QCommandLineParser::OptionsAfterPositionalArgumentsMode optionsAfterPositionalArgumentsMode
How to parse "arg -option".
QStringList positionalArgumentList
Arguments which did not belong to any option.
QList< PositionalArgumentDefinition > positionalArgumentDefinitions
Q_NORETURN void showHelp(int exitCode, bool includeQtOptions)
QList< QCommandLineOption > commandLineOptionList
The command line options used for parsing.
QString errorText
Error text set when parse() returns false.
The QCommandLineParser class provides a means for handling the command line options.
QString value(const QString &name) const
Returns the option value found for the given option name optionName, or an empty string if not found.
bool parse(const QStringList &arguments)
Parses the command line arguments.
void addPositionalArgument(const QString &name, const QString &description, const QString &syntax=QString())
Defines an additional argument to the application, for the benefit of the help text.
QString applicationDescription() const
Returns the application description set in setApplicationDescription().
QStringList positionalArguments() const
Returns a list of positional arguments.
QStringList unknownOptionNames() const
Returns a list of unknown option names.
void setSingleDashWordOptionMode(SingleDashWordOptionMode parsingMode)
Sets the parsing mode to singleDashWordOptionMode.
Q_NORETURN void showVersion()
Displays the version information from QCoreApplication::applicationVersion(), and exits the applicati...
QStringList values(const QString &name) const
Returns a list of option values found for the given option name optionName, or an empty list if not f...
void setApplicationDescription(const QString &description)
Sets the application description shown by helpText().
bool addOptions(const QList< QCommandLineOption > &options)
bool addOption(const QCommandLineOption &commandLineOption)
Adds the option option to look for while parsing.
Q_NORETURN void showHelp(int exitCode=0)
Displays the help information, and exits the application.
QString errorText() const
Returns a translated error text for the user.
SingleDashWordOptionMode
This enum describes the way the parser interprets command-line options that use a single dash followe...
@ ParseAsCompactedShortOptions
bool isSet(const QString &name) const
Checks whether the option name was passed to the application.
~QCommandLineParser()
Destroys the command line parser object.
QCommandLineParser()
Constructs a command line parser object.
QString helpText() const
Returns a string containing the complete help information.
QStringList optionNames() const
Returns a list of option names that were found.
void process(const QStringList &arguments)
Processes the command line arguments.
void clearPositionalArguments()
Clears the definitions of additional arguments from the help text.
OptionsAfterPositionalArgumentsMode
This enum describes the way the parser interprets options that occur after positional arguments.
@ ParseAsPositionalArguments
void setOptionsAfterPositionalArgumentsMode(OptionsAfterPositionalArgumentsMode mode)
Sets the parsing mode to parsingMode.
QCommandLineOption addVersionOption()
Adds the {-v} / {–version} option, which displays the version string of the application.
QCommandLineOption addHelpOption()
Adds help options to the command-line parser.
static QCoreApplication * instance() noexcept
Returns a pointer to the application's QCoreApplication (or QGuiApplication/QApplication) instance.
QString applicationVersion
the version of this application
static QStringList arguments()
QString applicationName
the name of this application
const_iterator constFind(const Key &key) const noexcept
const_iterator constEnd() const noexcept
Returns a const \l{STL-style iterators}{STL-style iterator} pointing to the imaginary item after the ...
bool contains(const Key &key) const noexcept
Returns true if the hash contains an item with the key; otherwise returns false.
T value(const Key &key) const noexcept
friend class const_iterator
const_iterator cend() const noexcept
qsizetype mapped_type
Typedef for T.
void clear() noexcept(std::is_nothrow_destructible< Node >::value)
Removes all items from the hash and frees up all memory used by it.
iterator insert(const Key &key, const T &value)
Inserts a new item with the key and a value of value.
constexpr void chop(qsizetype n)
qsizetype size() const noexcept
bool isEmpty() const noexcept
const_reference at(qsizetype i) const noexcept
void append(parameter_type t)
\macro QT_RESTRICTED_CAST_FROM_ASCII
const ushort * utf16() const
Returns the QString as a '\0\'-terminated array of unsigned shorts.
QString mid(qsizetype position, qsizetype n=-1) const &
bool isEmpty() const noexcept
Returns true if the string has no characters; otherwise returns false.
void clear()
Clears the contents of the string and makes it null.
qsizetype size() const noexcept
Returns the number of characters in this string.
QString arg(qlonglong a, int fieldwidth=0, int base=10, QChar fillChar=u' ') const
QString leftJustified(qsizetype width, QChar fill=u' ', bool trunc=false) const
Returns a string of size width that contains this string padded by the fill character.
const QChar at(qsizetype i) const
Returns the character at the given index position in the string.
QSet< QString >::iterator it
QList< QVariant > arguments
Combined button and popup list for selecting options.
QHash< QString, qsizetype > NameHash_t
static QString wrapText(const QString &names, int optionNameMaxWidth, const QString &description)
static void showParserMessage(const QString &message, MessageType type)
void Q_CORE_EXPORT qt_call_post_routines()
QList< QString > QStringList
Constructs a string list that contains the given string, str.
void Q_CORE_EXPORT qt_call_post_routines()
DBusConnection const char DBusError * error
DBusConnection const char DBusError DBusBusType DBusError return DBusConnection DBusHandleMessageFunction void DBusFreeFunction return DBusConnection return DBusConnection return const char DBusError return DBusConnection DBusMessage dbus_uint32_t return DBusConnection dbus_bool_t DBusConnection DBusAddWatchFunction DBusRemoveWatchFunction DBusWatchToggledFunction void DBusFreeFunction return DBusConnection DBusDispatchStatusFunction void DBusFreeFunction DBusTimeout return DBusTimeout return DBusWatch return DBusWatch unsigned int return DBusError const DBusError return const DBusMessage return DBusMessage return DBusMessage return DBusMessage return DBusMessage return DBusMessage return DBusMessageIter int const void return DBusMessageIter DBusMessageIter return DBusMessageIter void DBusMessageIter void int return DBusMessage DBusMessageIter return DBusMessageIter return DBusMessageIter DBusMessageIter const char const char const char const char * method
EGLOutputLayerEXT EGLint EGLAttrib value
[5]
constexpr const T & qMin(const T &a, const T &b)
constexpr const T & qMax(const T &a, const T &b)
GLenum GLsizei GLsizei GLint * values
[15]
GLint GLint GLint GLint GLint x
[0]
GLuint GLsizei const GLchar * message
GLenum GLuint GLintptr offset
GLsizeiptr const void GLenum usage
static QStringList aliases(const QQmlJSScope::ConstPtr &scope)
#define qPrintable(string)
#define qUtf16Printable(string)
#define QStringLiteral(str)
Q_CORE_EXPORT bool qEnvironmentVariableIsSet(const char *varName) noexcept
#define Q_DECLARE_TYPEINFO(TYPE, FLAGS)
QApplication app(argc, argv)
[0]
Documentation for positional arguments.
\inmodule QtCore \reentrant