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));
681 bool forcePositional =
false;
689 qWarning(
"QCommandLineParser: argument list cannot be empty, it should contain at least the executable name");
693 QStringList::const_iterator argumentIterator =
args.
begin();
696 for (; argumentIterator !=
args.
end() ; ++argumentIterator) {
699 if (forcePositional) {
701 }
else if (
argument.startsWith(doubleDashString)) {
711 forcePositional =
true;
713 }
else if (
argument.startsWith(dashChar)) {
722 bool valueFound =
false;
776 forcePositional =
true;
778 if (argumentIterator ==
args.
end())
805 if (aliases.contains(optionName))
834 if (!valueList.isEmpty())
835 return valueList.last();
999 ::exit(EXIT_SUCCESS);
1037 const auto nl = u
'\n';
1038 const auto indentation =
" "_L1;
1042 auto nextNameSection = [&]() {
1044 nameIndex += section.
size();
1051 const int max = 79 - (indentation.size() + optionNameMaxWidth + 1);
1063 if (
x > max && lastBreakable != -1) {
1065 breakAt = lastBreakable;
1066 nextLineStart = lastBreakable + 1;
1067 }
else if ((
x > max - 1 && lastBreakable == -1) ||
i ==
len - 1) {
1070 nextLineStart = breakAt;
1071 }
else if (
c == nl) {
1074 nextLineStart =
i + 1;
1077 if (breakAt != -1) {
1078 const qsizetype numChars = breakAt - lineStart;
1080 text += indentation + nextNameSection().
leftJustified(optionNameMaxWidth) + u
' ';
1084 lineStart = nextLineStart;
1085 if (lineStart <
len && description.
at(lineStart).
isSpace())
1091 while (nameIndex <
names.size()) {
1092 text += indentation + nextNameSection() + nl;
1106 if (includeQtOptions &&
qApp)
1107 qApp->d_func()->addQtOptions(&options);
1109 usage += u
' ' + QCommandLineParser::tr(
"[options]");
1112 text += QCommandLineParser::tr(
"Usage: %1").
arg(
usage) + nl;
1117 text += QCommandLineParser::tr(
"Options:") + nl;
1119 optionNameList.reserve(options.
size());
1127 const int numDashes = optionName.
size() == 1 ? 1 : 2;
1131 optionNamesString.
chop(2);
1132 const auto valueName =
option.valueName();
1133 if (!valueName.isEmpty())
1134 optionNamesString +=
" <"_L1 + valueName + u
'>';
1135 optionNameList.append(optionNamesString);
1136 longestOptionNameString =
qMax(longestOptionNameString, optionNamesString.
size());
1138 ++longestOptionNameString;
1139 const int optionNameMaxWidth =
qMin(50,
int(longestOptionNameString));
1140 auto optionNameIterator = optionNameList.cbegin();
1145 ++optionNameIterator;
1150 text += QCommandLineParser::tr(
"Arguments:") + nl;
constexpr bool isSpace() const noexcept
Returns true if the character is a separator character (Separator_* categories or certain code points...
The QCommandLineOption class defines a possible command-line option. \inmodule QtCore.
QString valueName() const
Returns the name of the expected value.
QStringList defaultValues() const
Returns the default values set for this option.
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 ...
qsizetype mapped_type
Typedef for T.
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
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.
qsizetype size() const noexcept
bool isEmpty() const noexcept
const_reference at(qsizetype i) const noexcept
void append(parameter_type t)
QVariant property(const char *name) const
Returns the value of the object's name property.
\macro QT_RESTRICTED_CAST_FROM_ASCII
void chop(qsizetype n)
Removes n characters from the end of the string.
const ushort * utf16() const
Returns the QString as a '\0\'-terminated array of unsigned shorts.
void clear()
Clears the contents of the string and makes it null.
qsizetype size() const
Returns the number of characters in this string.
QString arg(qlonglong a, int fieldwidth=0, int base=10, QChar fillChar=u' ') const
QString mid(qsizetype position, qsizetype n=-1) const
Returns a string that contains n characters of this string, starting at the specified position index.
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.
bool isEmpty() const
Returns true if the string has no characters; otherwise returns false.
QString toString() const
Returns the variant as a QString if the variant has a userType() including, but not limited to:
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()
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
#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