qcommandlineparser.cpp

Absolute File Name:/home/qt/qt5_coco/qt5/qtbase/src/corelib/tools/qcommandlineparser.cpp
Source codeSwitch to Preprocessed file
LineSourceCount
1/****************************************************************************-
2**-
3** Copyright (C) 2013 Laszlo Papp <lpapp@kde.org>-
4** Copyright (C) 2013 David Faure <faure@kde.org>-
5** Contact: http://www.qt.io/licensing/-
6**-
7** This file is part of the QtCore module of the Qt Toolkit.-
8**-
9** $QT_BEGIN_LICENSE:LGPL21$-
10** Commercial License Usage-
11** Licensees holding valid commercial Qt licenses may use this file in-
12** accordance with the commercial license agreement provided with the-
13** Software or, alternatively, in accordance with the terms contained in-
14** a written agreement between you and The Qt Company. For licensing terms-
15** and conditions see http://www.qt.io/terms-conditions. For further-
16** information use the contact form at http://www.qt.io/contact-us.-
17**-
18** GNU Lesser General Public License Usage-
19** Alternatively, this file may be used under the terms of the GNU Lesser-
20** General Public License version 2.1 or version 3 as published by the Free-
21** Software Foundation and appearing in the file LICENSE.LGPLv21 and-
22** LICENSE.LGPLv3 included in the packaging of this file. Please review the-
23** following information to ensure the GNU Lesser General Public License-
24** requirements will be met: https://www.gnu.org/licenses/lgpl.html and-
25** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.-
26**-
27** As a special exception, The Qt Company gives you certain additional-
28** rights. These rights are described in The Qt Company LGPL Exception-
29** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.-
30**-
31** $QT_END_LICENSE$-
32**-
33****************************************************************************/-
34-
35#include "qcommandlineparser.h"-
36-
37#include <qcoreapplication.h>-
38#include <qhash.h>-
39#include <qvector.h>-
40#include <qdebug.h>-
41#if defined(Q_OS_WIN) && !defined(QT_BOOTSTRAPPED) && !defined(Q_OS_WINCE) && !defined(Q_OS_WINRT)-
42# include <qt_windows.h>-
43#endif-
44#include <stdio.h>-
45#include <stdlib.h>-
46-
47QT_BEGIN_NAMESPACE-
48-
49extern void Q_CORE_EXPORT qt_call_post_routines();-
50-
51typedef QHash<QString, int> NameHash_t;-
52-
53class QCommandLineParserPrivate-
54{-
55public:-
56 inline QCommandLineParserPrivate()-
57 : singleDashWordOptionMode(QCommandLineParser::ParseAsCompactedShortOptions),-
58 optionsAfterPositionalArgumentsMode(QCommandLineParser::ParseAsOptions),-
59 builtinVersionOption(false),-
60 builtinHelpOption(false),-
61 needsParsing(true)-
62 { }
executed 41 times by 1 test: end of block
Executed by:
  • tst_QCommandLineParser
41
63-
64 bool parse(const QStringList &args);-
65 void checkParsed(const char *method);-
66 QStringList aliases(const QString &name) const;-
67 QString helpText() const;-
68 bool registerFoundOption(const QString &optionName);-
69 bool parseOptionValue(const QString &optionName, const QString &argument,-
70 QStringList::const_iterator *argumentIterator,-
71 QStringList::const_iterator argsEnd);-
72-
73 //! Error text set when parse() returns false-
74 QString errorText;-
75-
76 //! The command line options used for parsing-
77 QList<QCommandLineOption> commandLineOptionList;-
78-
79 //! Hash mapping option names to their offsets in commandLineOptionList and optionArgumentList.-
80 NameHash_t nameHash;-
81-
82 //! Option values found (only for options with a value)-
83 QHash<int, QStringList> optionValuesHash;-
84-
85 //! Names of options found on the command line.-
86 QStringList optionNames;-
87-
88 //! Arguments which did not belong to any option.-
89 QStringList positionalArgumentList;-
90-
91 //! Names of options which were unknown.-
92 QStringList unknownOptionNames;-
93-
94 //! Application description-
95 QString description;-
96-
97 //! Documentation for positional arguments-
98 struct PositionalArgumentDefinition-
99 {-
100 QString name;-
101 QString description;-
102 QString syntax;-
103 };-
104 QVector<PositionalArgumentDefinition> positionalArgumentDefinitions;-
105-
106 //! The parsing mode for "-abc"-
107 QCommandLineParser::SingleDashWordOptionMode singleDashWordOptionMode;-
108-
109 //! How to parse "arg -option"-
110 QCommandLineParser::OptionsAfterPositionalArgumentsMode optionsAfterPositionalArgumentsMode;-
111-
112 //! Whether addVersionOption was called-
113 bool builtinVersionOption;-
114-
115 //! Whether addHelpOption was called-
116 bool builtinHelpOption;-
117-
118 //! True if parse() needs to be called-
119 bool needsParsing;-
120};-
121Q_DECLARE_TYPEINFO(QCommandLineParserPrivate::PositionalArgumentDefinition, Q_MOVABLE_TYPE);-
122-
123QStringList QCommandLineParserPrivate::aliases(const QString &optionName) const-
124{-
125 const NameHash_t::const_iterator it = nameHash.constFind(optionName);-
126 if (it == nameHash.cend()) {
it == nameHash.cend()Description
TRUEevaluated 2 times by 1 test
Evaluated by:
  • tst_QCommandLineParser
FALSEevaluated 27 times by 1 test
Evaluated by:
  • tst_QCommandLineParser
2-27
127 qWarning("QCommandLineParser: option not defined: \"%s\"", qPrintable(optionName));-
128 return QStringList();
executed 2 times by 1 test: return QStringList();
Executed by:
  • tst_QCommandLineParser
2
129 }-
130 return commandLineOptionList.at(*it).names();
executed 27 times by 1 test: return commandLineOptionList.at(*it).names();
Executed by:
  • tst_QCommandLineParser
27
131}-
132-
133/*!-
134 \since 5.2-
135 \class QCommandLineParser-
136 \inmodule QtCore-
137 \ingroup tools-
138-
139 \brief The QCommandLineParser class provides a means for handling the-
140 command line options.-
141-
142 QCoreApplication provides the command-line arguments as a simple list of strings.-
143 QCommandLineParser provides the ability to define a set of options, parse the-
144 command-line arguments, and store which options have actually been used, as-
145 well as option values.-
146-
147 Any argument that isn't an option (i.e. doesn't start with a \c{-}) is stored-
148 as a "positional argument".-
149-
150 The parser handles short names, long names, more than one name for the same-
151 option, and option values.-
152-
153 Options on the command line are recognized as starting with a single or-
154 double \c{-} character(s).-
155 The option \c{-} (single dash alone) is a special case, often meaning standard-
156 input, and not treated as an option. The parser will treat everything after the-
157 option \c{--} (double dash) as positional arguments.-
158-
159 Short options are single letters. The option \c{v} would be specified by-
160 passing \c{-v} on the command line. In the default parsing mode, short options-
161 can be written in a compact form, for instance \c{-abc} is equivalent to \c{-a -b -c}.-
162 The parsing mode for can be set to ParseAsLongOptions, in which case \c{-abc}-
163 will be parsed as the long option \c{abc}.-
164-
165 Long options are more than one letter long and cannot be compacted together.-
166 The long option \c{verbose} would be passed as \c{--verbose} or \c{-verbose}.-
167-
168 Passing values to options can be done using the assignment operator: \c{-v=value}-
169 \c{--verbose=value}, or a space: \c{-v value} \c{--verbose value}, i.e. the next-
170 argument is used as value (even if it starts with a \c{-}).-
171-
172 The parser does not support optional values - if an option is set to-
173 require a value, one must be present. If such an option is placed last-
174 and has no value, the option will be treated as if it had not been-
175 specified.-
176-
177 The parser does not automatically support negating or disabling long options-
178 by using the format \c{--disable-option} or \c{--no-option}. However, it is-
179 possible to handle this case explicitly by making an option with \c{no-option}-
180 as one of its names, and handling the option explicitly.-
181-
182 Example:-
183 \snippet code/src_corelib_tools_qcommandlineparser_main.cpp 0-
184-
185 If your compiler supports the C++11 standard, the three addOption() calls in-
186 the above example can be simplified:-
187 \snippet code/src_corelib_tools_qcommandlineparser_main.cpp cxx11-
188-
189 Known limitation: the parsing of Qt options inside QCoreApplication and subclasses-
190 happens before QCommandLineParser exists, so it can't take it into account. This-
191 means any option value that looks like a builtin Qt option, will be treated by-
192 QCoreApplication as a builtin Qt option. Example: \c{--profile -reverse} will-
193 lead to QGuiApplication seeing the -reverse option set, and removing it from-
194 QCoreApplication::arguments() before QCommandLineParser defines the \c{profile}-
195 option and parses the command line.-
196-
197 \section2 How to Use QCommandLineParser in Complex Applications-
198-
199 In practice, additional error checking needs to be performed on the positional-
200 arguments and option values. For example, ranges of numbers should be checked.-
201-
202 It is then advisable to introduce a function to do the command line parsing-
203 which takes a struct or class receiving the option values returning an-
204 enumeration representing the result. The dnslookup example of the QtNetwork-
205 module illustrates this:-
206-
207 \snippet dnslookup.h 0-
208-
209 \snippet dnslookup.cpp 0-
210-
211 In the main function, help should be printed to the standard output if the help option-
212 was passed and the application should return the exit code 0.-
213-
214 If an error was detected, the error message should be printed to the standard-
215 error output and the application should return an exit code other than 0.-
216-
217 \snippet dnslookup.cpp 1-
218-
219 A special case to consider here are GUI applications on Windows and mobile-
220 platforms. These applications may not use the standard output or error channels-
221 since the output is either discarded or not accessible.-
222-
223 On Windows, QCommandLineParser uses message boxes to display usage information-
224 and errors if no console window can be obtained.-
225-
226 For other platforms, it is recommended to display help texts and error messages-
227 using a QMessageBox. To preserve the formatting of the help text, rich text-
228 with \c <pre> elements should be used:-
229-
230 \code-
231-
232 switch (parseCommandLine(parser, &query, &errorMessage)) {-
233 case CommandLineOk:-
234 break;-
235 case CommandLineError:-
236 QMessageBox::warning(0, QGuiApplication::applicationDisplayName(),-
237 "<html><head/><body><h2>" + errorMessage + "</h2><pre>"-
238 + parser.helpText() + "</pre></body></html>");-
239 return 1;-
240 case CommandLineVersionRequested:-
241 QMessageBox::information(0, QGuiApplication::applicationDisplayName(),-
242 QGuiApplication::applicationDisplayName() + ' '-
243 + QCoreApplication::applicationVersion());-
244 return 0;-
245 case CommandLineHelpRequested:-
246 QMessageBox::warning(0, QGuiApplication::applicationDisplayName(),-
247 "<html><head/><body><pre>"-
248 + parser.helpText() + "</pre></body></html>");-
249 return 0;-
250 }-
251 \endcode-
252-
253 However, this does not apply to the dnslookup example, because it is a-
254 console application.-
255-
256 \sa QCommandLineOption, QCoreApplication-
257*/-
258-
259/*!-
260 Constructs a command line parser object.-
261*/-
262QCommandLineParser::QCommandLineParser()-
263 : d(new QCommandLineParserPrivate)-
264{-
265}
executed 41 times by 1 test: end of block
Executed by:
  • tst_QCommandLineParser
41
266-
267/*!-
268 Destroys the command line parser object.-
269*/-
270QCommandLineParser::~QCommandLineParser()-
271{-
272 delete d;-
273}
executed 41 times by 1 test: end of block
Executed by:
  • tst_QCommandLineParser
41
274-
275/*!-
276 \enum QCommandLineParser::SingleDashWordOptionMode-
277-
278 This enum describes the way the parser interprets command-line-
279 options that use a single dash followed by multiple letters, as as \c{-abc}.-
280-
281 \value ParseAsCompactedShortOptions \c{-abc} is interpreted as \c{-a -b -c},-
282 i.e. as three short options that have been compacted on the command-line,-
283 if none of the options take a value. If \c{a} takes a value, then it-
284 is interpreted as \c{-a bc}, i.e. the short option \c{a} followed by the value \c{bc}.-
285 This is typically used in tools that behave like compilers, in order-
286 to handle options such as \c{-DDEFINE=VALUE} or \c{-I/include/path}.-
287 This is the default parsing mode. New applications are recommended to-
288 use this mode.-
289-
290 \value ParseAsLongOptions \c{-abc} is interpreted as \c{--abc},-
291 i.e. as the long option named \c{abc}. This is how Qt's own tools-
292 (uic, rcc...) have always been parsing arguments. This mode should be-
293 used for preserving compatibility in applications that were parsing-
294 arguments in such a way.-
295-
296 \sa setSingleDashWordOptionMode()-
297*/-
298-
299/*!-
300 Sets the parsing mode to \a singleDashWordOptionMode.-
301 This must be called before process() or parse().-
302*/-
303void QCommandLineParser::setSingleDashWordOptionMode(QCommandLineParser::SingleDashWordOptionMode singleDashWordOptionMode)-
304{-
305 d->singleDashWordOptionMode = singleDashWordOptionMode;-
306}
executed 28 times by 1 test: end of block
Executed by:
  • tst_QCommandLineParser
28
307-
308/*!-
309 \enum QCommandLineParser::OptionsAfterPositionalArgumentsMode-
310-
311 This enum describes the way the parser interprets options that-
312 occur after positional arguments.-
313-
314 \value ParseAsOptions \c{application argument --opt -t} is interpreted as setting-
315 the options \c{opt} and \c{t}, just like \c{application --opt -t argument} would do.-
316 This is the default parsing mode. In order to specify that \c{--opt} and \c{-t}-
317 are positional arguments instead, the user can use \c{--}, as in-
318 \c{application argument -- --opt -t}.-
319-
320 \value ParseAsPositionalArguments \c{application argument --opt} is interpreted as-
321 having two positional arguments, \c{argument} and \c{--opt}.-
322 This mode is useful for executables that aim to launch other executables-
323 (e.g. wrappers, debugging tools, etc.) or that support internal commands-
324 followed by options for the command. \c{argument} is the name of the command,-
325 and all options occurring after it can be collected and parsed by another-
326 command line parser, possibly in another executable.-
327-
328 \sa setOptionsAfterPositionalArgumentsMode()-
329-
330 \since 5.6-
331*/-
332-
333/*!-
334 Sets the parsing mode to \a parsingMode.-
335 This must be called before process() or parse().-
336 \since 5.6-
337*/-
338void QCommandLineParser::setOptionsAfterPositionalArgumentsMode(QCommandLineParser::OptionsAfterPositionalArgumentsMode parsingMode)-
339{-
340 d->optionsAfterPositionalArgumentsMode = parsingMode;-
341}
executed 4 times by 1 test: end of block
Executed by:
  • tst_QCommandLineParser
4
342-
343/*!-
344 Adds the option \a option to look for while parsing.-
345-
346 Returns \c true if adding the option was successful; otherwise returns \c false.-
347-
348 Adding the option fails if there is no name attached to the option, or-
349 the option has a name that clashes with an option name added before.-
350 */-
351bool QCommandLineParser::addOption(const QCommandLineOption &option)-
352{-
353 QStringList optionNames = option.names();-
354-
355 if (!optionNames.isEmpty()) {
!optionNames.isEmpty()Description
TRUEevaluated 75 times by 1 test
Evaluated by:
  • tst_QCommandLineParser
FALSEevaluated 1 time by 1 test
Evaluated by:
  • tst_QCommandLineParser
1-75
356 foreach (const QString &name, optionNames) {-
357 if (d->nameHash.contains(name))
d->nameHash.contains(name)Description
TRUEnever evaluated
FALSEevaluated 105 times by 1 test
Evaluated by:
  • tst_QCommandLineParser
0-105
358 return false;
never executed: return false;
0
359 }
executed 105 times by 1 test: end of block
Executed by:
  • tst_QCommandLineParser
105
360-
361 d->commandLineOptionList.append(option);-
362-
363 const int offset = d->commandLineOptionList.size() - 1;-
364 foreach (const QString &name, optionNames)-
365 d->nameHash.insert(name, offset);
executed 105 times by 1 test: d->nameHash.insert(name, offset);
Executed by:
  • tst_QCommandLineParser
105
366-
367 return true;
executed 75 times by 1 test: return true;
Executed by:
  • tst_QCommandLineParser
75
368 }-
369-
370 return false;
executed 1 time by 1 test: return false;
Executed by:
  • tst_QCommandLineParser
1
371}-
372-
373/*!-
374 \since 5.4-
375-
376 Adds the options to look for while parsing. The options are specified by-
377 the parameter \a options.-
378-
379 Returns \c true if adding all of the options was successful; otherwise-
380 returns \c false.-
381-
382 See the documentation for addOption() for when this function may fail.-
383*/-
384bool QCommandLineParser::addOptions(const QList<QCommandLineOption> &options)-
385{-
386 // should be optimized (but it's no worse than what was possible before)-
387 bool result = true;-
388 for (QList<QCommandLineOption>::const_iterator it = options.begin(), end = options.end(); it != end; ++it)
it != endDescription
TRUEevaluated 3 times by 1 test
Evaluated by:
  • tst_QCommandLineParser
FALSEevaluated 1 time by 1 test
Evaluated by:
  • tst_QCommandLineParser
1-3
389 result &= addOption(*it);
executed 3 times by 1 test: result &= addOption(*it);
Executed by:
  • tst_QCommandLineParser
3
390 return result;
executed 1 time by 1 test: return result;
Executed by:
  • tst_QCommandLineParser
1
391}-
392-
393/*!-
394 Adds the \c{-v} / \c{--version} option, which displays the version string of the application.-
395-
396 This option is handled automatically by QCommandLineParser.-
397-
398 You can set the actual version string by using QCoreApplication::setApplicationVersion().-
399-
400 Returns the option instance, which can be used to call isSet().-
401*/-
402QCommandLineOption QCommandLineParser::addVersionOption()-
403{-
404 QCommandLineOption opt(QStringList() << QStringLiteral("v") << QStringLiteral("version"), tr("Displays version information."));
never executed: return qstring_literal_temp;
never executed: return qstring_literal_temp;
0
405 addOption(opt);-
406 d->builtinVersionOption = true;-
407 return opt;
never executed: return opt;
0
408}-
409-
410/*!-
411 Adds the help option (\c{-h}, \c{--help} and \c{-?} on Windows)-
412 This option is handled automatically by QCommandLineParser.-
413-
414 Remember to use setApplicationDescription to set the application description,-
415 which will be displayed when this option is used.-
416-
417 Example:-
418 \snippet code/src_corelib_tools_qcommandlineparser_main.cpp 0-
419-
420 Returns the option instance, which can be used to call isSet().-
421*/-
422QCommandLineOption QCommandLineParser::addHelpOption()-
423{-
424 QCommandLineOption opt(QStringList()-
425#ifdef Q_OS_WIN-
426 << QStringLiteral("?")-
427#endif-
428 << QStringLiteral("h")
never executed: return qstring_literal_temp;
0
429 << QStringLiteral("help"), tr("Displays this help."));
never executed: return qstring_literal_temp;
0
430 addOption(opt);-
431 d->builtinHelpOption = true;-
432 return opt;
never executed: return opt;
0
433}-
434-
435/*!-
436 Sets the application \a description shown by helpText().-
437*/-
438void QCommandLineParser::setApplicationDescription(const QString &description)-
439{-
440 d->description = description;-
441}
never executed: end of block
0
442-
443/*!-
444 Returns the application description set in setApplicationDescription().-
445*/-
446QString QCommandLineParser::applicationDescription() const-
447{-
448 return d->description;
never executed: return d->description;
0
449}-
450-
451/*!-
452 Defines an additional argument to the application, for the benefit of the help text.-
453-
454 The argument \a name and \a description will appear under the \c{Arguments:} section-
455 of the help. If \a syntax is specified, it will be appended to the Usage line, otherwise-
456 the \a name will be appended.-
457-
458 Example:-
459 \snippet code/src_corelib_tools_qcommandlineparser.cpp 2-
460-
461 \sa addHelpOption(), helpText()-
462*/-
463void QCommandLineParser::addPositionalArgument(const QString &name, const QString &description, const QString &syntax)-
464{-
465 QCommandLineParserPrivate::PositionalArgumentDefinition arg;-
466 arg.name = name;-
467 arg.description = description;-
468 arg.syntax = syntax.isEmpty() ? name : syntax;
syntax.isEmpty()Description
TRUEnever evaluated
FALSEnever evaluated
0
469 d->positionalArgumentDefinitions.append(arg);-
470}
never executed: end of block
0
471-
472/*!-
473 Clears the definitions of additional arguments from the help text.-
474-
475 This is only needed for the special case of tools which support multiple commands-
476 with different options. Once the actual command has been identified, the options-
477 for this command can be defined, and the help text for the command can be adjusted-
478 accordingly.-
479-
480 Example:-
481 \snippet code/src_corelib_tools_qcommandlineparser.cpp 3-
482*/-
483void QCommandLineParser::clearPositionalArguments()-
484{-
485 d->positionalArgumentDefinitions.clear();-
486}
never executed: end of block
0
487-
488/*!-
489 Parses the command line \a arguments.-
490-
491 Most programs don't need to call this, a simple call to process() is enough.-
492-
493 parse() is more low-level, and only does the parsing. The application will have to-
494 take care of the error handling, using errorText() if parse() returns \c false.-
495 This can be useful for instance to show a graphical error message in graphical programs.-
496-
497 Calling parse() instead of process() can also be useful in order to ignore unknown-
498 options temporarily, because more option definitions will be provided later on-
499 (depending on one of the arguments), before calling process().-
500-
501 Don't forget that \a arguments must start with the name of the executable (ignored, though).-
502-
503 Returns \c false in case of a parse error (unknown option or missing value); returns \c true otherwise.-
504-
505 \sa process()-
506*/-
507bool QCommandLineParser::parse(const QStringList &arguments)-
508{-
509 return d->parse(arguments);
executed 50 times by 1 test: return d->parse(arguments);
Executed by:
  • tst_QCommandLineParser
50
510}-
511-
512/*!-
513 Returns a translated error text for the user.-
514 This should only be called when parse() returns \c false.-
515*/-
516QString QCommandLineParser::errorText() const-
517{-
518 if (!d->errorText.isEmpty())
!d->errorText.isEmpty()Description
TRUEevaluated 3 times by 1 test
Evaluated by:
  • tst_QCommandLineParser
FALSEevaluated 3 times by 1 test
Evaluated by:
  • tst_QCommandLineParser
3
519 return d->errorText;
executed 3 times by 1 test: return d->errorText;
Executed by:
  • tst_QCommandLineParser
3
520 if (d->unknownOptionNames.count() == 1)
d->unknownOpti...s.count() == 1Description
TRUEevaluated 2 times by 1 test
Evaluated by:
  • tst_QCommandLineParser
FALSEevaluated 1 time by 1 test
Evaluated by:
  • tst_QCommandLineParser
1-2
521 return tr("Unknown option '%1'.").arg(d->unknownOptionNames.first());
executed 2 times by 1 test: return tr("Unknown option '%1'.").arg(d->unknownOptionNames.first());
Executed by:
  • tst_QCommandLineParser
2
522 if (d->unknownOptionNames.count() > 1)
d->unknownOpti...es.count() > 1Description
TRUEevaluated 1 time by 1 test
Evaluated by:
  • tst_QCommandLineParser
FALSEnever evaluated
0-1
523 return tr("Unknown options: %1.").arg(d->unknownOptionNames.join(QStringLiteral(", ")));
executed 1 time by 1 test: return tr("Unknown options: %1.").arg(d->unknownOptionNames.join(([]() -> QString { enum { Size = sizeof(u"" ", ")/2 - 1 }; static const QStaticStringData<Size> qstring_literal = { { { { -1 } }, Size, 0, 0, sizeof(QStringData) }, u"" ", " }; QStringDataPtr holder = { qstring_literal.data_ptr() }; const QString qstring_literal_temp(holder); return qstring_literal_temp; }())));
Executed by:
  • tst_QCommandLineParser
executed 1 time by 1 test: return qstring_literal_temp;
Executed by:
  • tst_QCommandLineParser
1
524 return QString();
never executed: return QString();
0
525}-
526-
527enum MessageType { UsageMessage, ErrorMessage };-
528-
529#if defined(Q_OS_WIN) && !defined(QT_BOOTSTRAPPED) && !defined(Q_OS_WINCE) && !defined(Q_OS_WINRT)-
530// Return whether to use a message box. Use handles if a console can be obtained-
531// or we are run with redirected handles (for example, by QProcess).-
532static inline bool displayMessageBox()-
533{-
534 if (GetConsoleWindow())-
535 return false;-
536 STARTUPINFO startupInfo;-
537 startupInfo.cb = sizeof(STARTUPINFO);-
538 GetStartupInfo(&startupInfo);-
539 return !(startupInfo.dwFlags & STARTF_USESTDHANDLES);-
540}-
541#endif // Q_OS_WIN && !QT_BOOTSTRAPPED && !Q_OS_WIN && !Q_OS_WINRT-
542-
543static void showParserMessage(const QString &message, MessageType type)-
544{-
545#if defined(Q_OS_WINRT)-
546 if (type == UsageMessage)-
547 qInfo(qPrintable(message));-
548 else-
549 qCritical(qPrintable(message));-
550 return;-
551#elif defined(Q_OS_WIN) && !defined(QT_BOOTSTRAPPED) && !defined(Q_OS_WINCE)-
552 if (displayMessageBox()) {-
553 const UINT flags = MB_OK | MB_TOPMOST | MB_SETFOREGROUND-
554 | (type == UsageMessage ? MB_ICONINFORMATION : MB_ICONERROR);-
555 QString title;-
556 if (QCoreApplication::instance())-
557 title = QCoreApplication::instance()->property("applicationDisplayName").toString();-
558 if (title.isEmpty())-
559 title = QCoreApplication::applicationName();-
560 MessageBoxW(0, reinterpret_cast<const wchar_t *>(message.utf16()),-
561 reinterpret_cast<const wchar_t *>(title.utf16()), flags);-
562 return;-
563 }-
564#endif // Q_OS_WIN && !QT_BOOTSTRAPPED && !Q_OS_WINCE-
565 fputs(qPrintable(message), type == UsageMessage ? stdout : stderr);-
566}
never executed: end of block
0
567-
568/*!-
569 Processes the command line \a arguments.-
570-
571 In addition to parsing the options (like parse()), this function also handles the builtin-
572 options and handles errors.-
573-
574 The builtin options are \c{--version} if addVersionOption was called and \c{--help} if addHelpOption was called.-
575-
576 When invoking one of these options, or when an error happens (for instance an unknown option was-
577 passed), the current process will then stop, using the exit() function.-
578-
579 \sa QCoreApplication::arguments(), parse()-
580 */-
581void QCommandLineParser::process(const QStringList &arguments)-
582{-
583 if (!d->parse(arguments)) {
!d->parse(arguments)Description
TRUEnever evaluated
FALSEnever evaluated
0
584 showParserMessage(errorText() + QLatin1Char('\n'), ErrorMessage);-
585 qt_call_post_routines();-
586 ::exit(EXIT_FAILURE);
never executed: ::exit(1);
0
587 }-
588-
589 if (d->builtinVersionOption && isSet(QStringLiteral("version")))
never executed: return qstring_literal_temp;
d->builtinVersionOptionDescription
TRUEnever evaluated
FALSEnever evaluated
isSet(([]() ->...al_temp; }()))Description
TRUEnever evaluated
FALSEnever evaluated
0
590 showVersion();
never executed: showVersion();
0
591-
592 if (d->builtinHelpOption && isSet(QStringLiteral("help")))
never executed: return qstring_literal_temp;
d->builtinHelpOptionDescription
TRUEnever evaluated
FALSEnever evaluated
isSet(([]() ->...al_temp; }()))Description
TRUEnever evaluated
FALSEnever evaluated
0
593 showHelp(EXIT_SUCCESS);
never executed: showHelp(0);
0
594}
never executed: end of block
0
595-
596/*!-
597 \overload-
598-
599 The command line is obtained from the QCoreApplication instance \a app.-
600 */-
601void QCommandLineParser::process(const QCoreApplication &app)-
602{-
603 // QCoreApplication::arguments() is static, but the app instance must exist so we require it as parameter-
604 Q_UNUSED(app);-
605 process(QCoreApplication::arguments());-
606}
never executed: end of block
0
607-
608void QCommandLineParserPrivate::checkParsed(const char *method)-
609{-
610 if (needsParsing)
needsParsingDescription
TRUEevaluated 2 times by 1 test
Evaluated by:
  • tst_QCommandLineParser
FALSEevaluated 260 times by 1 test
Evaluated by:
  • tst_QCommandLineParser
2-260
611 qWarning("QCommandLineParser: call process() or parse() before %s", method);
executed 2 times by 1 test: QMessageLogger(__FILE__, 611, __PRETTY_FUNCTION__).warning("QCommandLineParser: call process() or parse() before %s", method);
Executed by:
  • tst_QCommandLineParser
2
612}
executed 262 times by 1 test: end of block
Executed by:
  • tst_QCommandLineParser
262
613-
614/*!-
615 \internal-
616 Looks up the option \a optionName (found on the command line) and register it as found.-
617 Returns \c true on success.-
618 */-
619bool QCommandLineParserPrivate::registerFoundOption(const QString &optionName)-
620{-
621 if (nameHash.contains(optionName)) {
nameHash.contains(optionName)Description
TRUEevaluated 47 times by 1 test
Evaluated by:
  • tst_QCommandLineParser
FALSEevaluated 5 times by 1 test
Evaluated by:
  • tst_QCommandLineParser
5-47
622 optionNames.append(optionName);-
623 return true;
executed 47 times by 1 test: return true;
Executed by:
  • tst_QCommandLineParser
47
624 } else {-
625 unknownOptionNames.append(optionName);-
626 return false;
executed 5 times by 1 test: return false;
Executed by:
  • tst_QCommandLineParser
5
627 }-
628}-
629-
630/*!-
631 \internal-
632 \brief Parse the value for a given option, if it was defined to expect one.-
633-
634 The value is taken from the next argument, or after the equal sign in \a argument.-
635-
636 \param optionName the short option name-
637 \param argument the argument from the command line currently parsed. Only used for -k=value parsing.-
638 \param argumentIterator iterator to the currently parsed argument. Incremented if the next argument contains the value.-
639 \param argsEnd args.end(), to check if ++argumentIterator goes out of bounds-
640 Returns \c true on success.-
641 */-
642bool QCommandLineParserPrivate::parseOptionValue(const QString &optionName, const QString &argument,-
643 QStringList::const_iterator *argumentIterator, QStringList::const_iterator argsEnd)-
644{-
645 const QLatin1Char assignChar('=');-
646 const NameHash_t::const_iterator nameHashIt = nameHash.constFind(optionName);-
647 if (nameHashIt != nameHash.constEnd()) {
nameHashIt != ...ash.constEnd()Description
TRUEevaluated 38 times by 1 test
Evaluated by:
  • tst_QCommandLineParser
FALSEevaluated 1 time by 1 test
Evaluated by:
  • tst_QCommandLineParser
1-38
648 const int assignPos = argument.indexOf(assignChar);-
649 const NameHash_t::mapped_type optionOffset = *nameHashIt;-
650 const bool withValue = !commandLineOptionList.at(optionOffset).valueName().isEmpty();-
651 if (withValue) {
withValueDescription
TRUEevaluated 24 times by 1 test
Evaluated by:
  • tst_QCommandLineParser
FALSEevaluated 14 times by 1 test
Evaluated by:
  • tst_QCommandLineParser
14-24
652 if (assignPos == -1) {
assignPos == -1Description
TRUEevaluated 20 times by 1 test
Evaluated by:
  • tst_QCommandLineParser
FALSEevaluated 4 times by 1 test
Evaluated by:
  • tst_QCommandLineParser
4-20
653 ++(*argumentIterator);-
654 if (*argumentIterator == argsEnd) {
*argumentIterator == argsEndDescription
TRUEevaluated 1 time by 1 test
Evaluated by:
  • tst_QCommandLineParser
FALSEevaluated 19 times by 1 test
Evaluated by:
  • tst_QCommandLineParser
1-19
655 errorText = QCommandLineParser::tr("Missing value after '%1'.").arg(argument);-
656 return false;
executed 1 time by 1 test: return false;
Executed by:
  • tst_QCommandLineParser
1
657 }-
658 optionValuesHash[optionOffset].append(*(*argumentIterator));-
659 } else {
executed 19 times by 1 test: end of block
Executed by:
  • tst_QCommandLineParser
19
660 optionValuesHash[optionOffset].append(argument.mid(assignPos + 1));-
661 }
executed 4 times by 1 test: end of block
Executed by:
  • tst_QCommandLineParser
4
662 } else {-
663 if (assignPos != -1) {
assignPos != -1Description
TRUEevaluated 2 times by 1 test
Evaluated by:
  • tst_QCommandLineParser
FALSEevaluated 12 times by 1 test
Evaluated by:
  • tst_QCommandLineParser
2-12
664 errorText = QCommandLineParser::tr("Unexpected value after '%1'.").arg(argument.left(assignPos));-
665 return false;
executed 2 times by 1 test: return false;
Executed by:
  • tst_QCommandLineParser
2
666 }-
667 }
executed 12 times by 1 test: end of block
Executed by:
  • tst_QCommandLineParser
12
668 }-
669 return true;
executed 36 times by 1 test: return true;
Executed by:
  • tst_QCommandLineParser
36
670}-
671-
672/*!-
673 \internal-
674-
675 Parse the list of arguments \a args, and fills in-
676 optionNames, optionValuesHash, unknownOptionNames, positionalArguments, and errorText.-
677-
678 Any results from a previous parse operation are removed.-
679-
680 The parser will not look for further options once it encounters the option-
681 \c{--}; this does not include when \c{--} follows an option that requires a value.-
682 */-
683bool QCommandLineParserPrivate::parse(const QStringList &args)-
684{-
685 needsParsing = false;-
686 bool error = false;-
687-
688 const QString doubleDashString(QStringLiteral("--"));
executed 50 times by 1 test: return qstring_literal_temp;
Executed by:
  • tst_QCommandLineParser
50
689 const QLatin1Char dashChar('-');-
690 const QLatin1Char assignChar('=');-
691-
692 bool forcePositional = false;-
693 errorText.clear();-
694 positionalArgumentList.clear();-
695 optionNames.clear();-
696 unknownOptionNames.clear();-
697 optionValuesHash.clear();-
698-
699 if (args.isEmpty()) {
args.isEmpty()Description
TRUEevaluated 1 time by 1 test
Evaluated by:
  • tst_QCommandLineParser
FALSEevaluated 49 times by 1 test
Evaluated by:
  • tst_QCommandLineParser
1-49
700 qWarning("QCommandLineParser: argument list cannot be empty, it should contain at least the executable name");-
701 return false;
executed 1 time by 1 test: return false;
Executed by:
  • tst_QCommandLineParser
1
702 }-
703-
704 QStringList::const_iterator argumentIterator = args.begin();-
705 ++argumentIterator; // skip executable name-
706-
707 for (; argumentIterator != args.end() ; ++argumentIterator) {
argumentIterator != args.end()Description
TRUEevaluated 63 times by 1 test
Evaluated by:
  • tst_QCommandLineParser
FALSEevaluated 48 times by 1 test
Evaluated by:
  • tst_QCommandLineParser
48-63
708 QString argument = *argumentIterator;-
709-
710 if (forcePositional) {
forcePositionalDescription
TRUEevaluated 9 times by 1 test
Evaluated by:
  • tst_QCommandLineParser
FALSEevaluated 54 times by 1 test
Evaluated by:
  • tst_QCommandLineParser
9-54
711 positionalArgumentList.append(argument);-
712 } else if (argument.startsWith(doubleDashString)) {
executed 9 times by 1 test: end of block
Executed by:
  • tst_QCommandLineParser
argument.start...bleDashString)Description
TRUEevaluated 24 times by 1 test
Evaluated by:
  • tst_QCommandLineParser
FALSEevaluated 30 times by 1 test
Evaluated by:
  • tst_QCommandLineParser
9-30
713 if (argument.length() > 2) {
argument.length() > 2Description
TRUEevaluated 22 times by 1 test
Evaluated by:
  • tst_QCommandLineParser
FALSEevaluated 2 times by 1 test
Evaluated by:
  • tst_QCommandLineParser
2-22
714 QString optionName = argument.mid(2).section(assignChar, 0, 0);-
715 if (registerFoundOption(optionName)) {
registerFoundO...on(optionName)Description
TRUEevaluated 20 times by 1 test
Evaluated by:
  • tst_QCommandLineParser
FALSEevaluated 2 times by 1 test
Evaluated by:
  • tst_QCommandLineParser
2-20
716 if (!parseOptionValue(optionName, argument, &argumentIterator, args.end()))
!parseOptionVa...r, args.end())Description
TRUEevaluated 1 time by 1 test
Evaluated by:
  • tst_QCommandLineParser
FALSEevaluated 19 times by 1 test
Evaluated by:
  • tst_QCommandLineParser
1-19
717 error = true;
executed 1 time by 1 test: error = true;
Executed by:
  • tst_QCommandLineParser
1
718 } else {
executed 20 times by 1 test: end of block
Executed by:
  • tst_QCommandLineParser
20
719 error = true;-
720 }
executed 2 times by 1 test: end of block
Executed by:
  • tst_QCommandLineParser
2
721 } else {-
722 forcePositional = true;-
723 }
executed 2 times by 1 test: end of block
Executed by:
  • tst_QCommandLineParser
2
724 } else if (argument.startsWith(dashChar)) {
argument.startsWith(dashChar)Description
TRUEevaluated 23 times by 1 test
Evaluated by:
  • tst_QCommandLineParser
FALSEevaluated 7 times by 1 test
Evaluated by:
  • tst_QCommandLineParser
7-23
725 if (argument.size() == 1) { // single dash ("stdin")
argument.size() == 1Description
TRUEevaluated 2 times by 1 test
Evaluated by:
  • tst_QCommandLineParser
FALSEevaluated 21 times by 1 test
Evaluated by:
  • tst_QCommandLineParser
2-21
726 positionalArgumentList.append(argument);-
727 continue;
executed 2 times by 1 test: continue;
Executed by:
  • tst_QCommandLineParser
2
728 }-
729 switch (singleDashWordOptionMode) {-
730 case QCommandLineParser::ParseAsCompactedShortOptions:
executed 16 times by 1 test: case QCommandLineParser::ParseAsCompactedShortOptions:
Executed by:
  • tst_QCommandLineParser
16
731 {-
732 QString optionName;-
733 bool valueFound = false;-
734 for (int pos = 1 ; pos < argument.size(); ++pos) {
pos < argument.size()Description
TRUEevaluated 25 times by 1 test
Evaluated by:
  • tst_QCommandLineParser
FALSEevaluated 9 times by 1 test
Evaluated by:
  • tst_QCommandLineParser
9-25
735 optionName = argument.mid(pos, 1);-
736 if (!registerFoundOption(optionName)) {
!registerFound...on(optionName)Description
TRUEevaluated 3 times by 1 test
Evaluated by:
  • tst_QCommandLineParser
FALSEevaluated 22 times by 1 test
Evaluated by:
  • tst_QCommandLineParser
3-22
737 error = true;-
738 } else {
executed 3 times by 1 test: end of block
Executed by:
  • tst_QCommandLineParser
3
739 const NameHash_t::const_iterator nameHashIt = nameHash.constFind(optionName);-
740 Q_ASSERT(nameHashIt != nameHash.constEnd()); // checked by registerFoundOption-
741 const NameHash_t::mapped_type optionOffset = *nameHashIt;-
742 const bool withValue = !commandLineOptionList.at(optionOffset).valueName().isEmpty();-
743 if (withValue) {
withValueDescription
TRUEevaluated 6 times by 1 test
Evaluated by:
  • tst_QCommandLineParser
FALSEevaluated 16 times by 1 test
Evaluated by:
  • tst_QCommandLineParser
6-16
744 if (pos + 1 < argument.size()) {
pos + 1 < argument.size()Description
TRUEevaluated 2 times by 1 test
Evaluated by:
  • tst_QCommandLineParser
FALSEevaluated 4 times by 1 test
Evaluated by:
  • tst_QCommandLineParser
2-4
745 if (argument.at(pos + 1) == assignChar)
argument.at(po... == assignCharDescription
TRUEevaluated 1 time by 1 test
Evaluated by:
  • tst_QCommandLineParser
FALSEevaluated 1 time by 1 test
Evaluated by:
  • tst_QCommandLineParser
1
746 ++pos;
executed 1 time by 1 test: ++pos;
Executed by:
  • tst_QCommandLineParser
1
747 optionValuesHash[optionOffset].append(argument.mid(pos + 1));-
748 valueFound = true;-
749 }
executed 2 times by 1 test: end of block
Executed by:
  • tst_QCommandLineParser
2
750 break;
executed 6 times by 1 test: break;
Executed by:
  • tst_QCommandLineParser
6
751 }-
752 if (pos + 1 < argument.size() && argument.at(pos + 1) == assignChar)
pos + 1 < argument.size()Description
TRUEevaluated 8 times by 1 test
Evaluated by:
  • tst_QCommandLineParser
FALSEevaluated 8 times by 1 test
Evaluated by:
  • tst_QCommandLineParser
argument.at(po... == assignCharDescription
TRUEevaluated 1 time by 1 test
Evaluated by:
  • tst_QCommandLineParser
FALSEevaluated 7 times by 1 test
Evaluated by:
  • tst_QCommandLineParser
1-8
753 break;
executed 1 time by 1 test: break;
Executed by:
  • tst_QCommandLineParser
1
754 }
executed 15 times by 1 test: end of block
Executed by:
  • tst_QCommandLineParser
15
755 }-
756 if (!valueFound && !parseOptionValue(optionName, argument, &argumentIterator, args.end()))
!valueFoundDescription
TRUEevaluated 14 times by 1 test
Evaluated by:
  • tst_QCommandLineParser
FALSEevaluated 2 times by 1 test
Evaluated by:
  • tst_QCommandLineParser
!parseOptionVa...r, args.end())Description
TRUEevaluated 1 time by 1 test
Evaluated by:
  • tst_QCommandLineParser
FALSEevaluated 13 times by 1 test
Evaluated by:
  • tst_QCommandLineParser
1-14
757 error = true;
executed 1 time by 1 test: error = true;
Executed by:
  • tst_QCommandLineParser
1
758 break;
executed 16 times by 1 test: break;
Executed by:
  • tst_QCommandLineParser
16
759 }-
760 case QCommandLineParser::ParseAsLongOptions:
executed 5 times by 1 test: case QCommandLineParser::ParseAsLongOptions:
Executed by:
  • tst_QCommandLineParser
5
761 {-
762 const QString optionName = argument.mid(1).section(assignChar, 0, 0);-
763 if (registerFoundOption(optionName)) {
registerFoundO...on(optionName)Description
TRUEevaluated 5 times by 1 test
Evaluated by:
  • tst_QCommandLineParser
FALSEnever evaluated
0-5
764 if (!parseOptionValue(optionName, argument, &argumentIterator, args.end()))
!parseOptionVa...r, args.end())Description
TRUEevaluated 1 time by 1 test
Evaluated by:
  • tst_QCommandLineParser
FALSEevaluated 4 times by 1 test
Evaluated by:
  • tst_QCommandLineParser
1-4
765 error = true;
executed 1 time by 1 test: error = true;
Executed by:
  • tst_QCommandLineParser
1
766 } else {
executed 5 times by 1 test: end of block
Executed by:
  • tst_QCommandLineParser
5
767 error = true;-
768 }
never executed: end of block
0
769 break;
executed 5 times by 1 test: break;
Executed by:
  • tst_QCommandLineParser
5
770 }-
771 }-
772 } else {
executed 21 times by 1 test: end of block
Executed by:
  • tst_QCommandLineParser
21
773 positionalArgumentList.append(argument);-
774 if (optionsAfterPositionalArgumentsMode == QCommandLineParser::ParseAsPositionalArguments)
optionsAfterPo...ionalArgumentsDescription
TRUEevaluated 2 times by 1 test
Evaluated by:
  • tst_QCommandLineParser
FALSEevaluated 5 times by 1 test
Evaluated by:
  • tst_QCommandLineParser
2-5
775 forcePositional = true;
executed 2 times by 1 test: forcePositional = true;
Executed by:
  • tst_QCommandLineParser
2
776 }
executed 7 times by 1 test: end of block
Executed by:
  • tst_QCommandLineParser
7
777 if (argumentIterator == args.end())
argumentIterator == args.end()Description
TRUEevaluated 1 time by 1 test
Evaluated by:
  • tst_QCommandLineParser
FALSEevaluated 60 times by 1 test
Evaluated by:
  • tst_QCommandLineParser
1-60
778 break;
executed 1 time by 1 test: break;
Executed by:
  • tst_QCommandLineParser
1
779 }
executed 60 times by 1 test: end of block
Executed by:
  • tst_QCommandLineParser
60
780 return !error;
executed 49 times by 1 test: return !error;
Executed by:
  • tst_QCommandLineParser
49
781}-
782-
783/*!-
784 Checks whether the option \a name was passed to the application.-
785-
786 Returns \c true if the option \a name was set, false otherwise.-
787-
788 The name provided can be any long or short name of any option that was-
789 added with \c addOption(). All the options names are treated as being-
790 equivalent. If the name is not recognized or that option was not present,-
791 false is returned.-
792-
793 Example:-
794 \snippet code/src_corelib_tools_qcommandlineparser.cpp 0-
795 */-
796-
797bool QCommandLineParser::isSet(const QString &name) const-
798{-
799 d->checkParsed("isSet");-
800 if (d->optionNames.contains(name))
d->optionNames.contains(name)Description
TRUEevaluated 18 times by 1 test
Evaluated by:
  • tst_QCommandLineParser
FALSEevaluated 29 times by 1 test
Evaluated by:
  • tst_QCommandLineParser
18-29
801 return true;
executed 18 times by 1 test: return true;
Executed by:
  • tst_QCommandLineParser
18
802 const QStringList aliases = d->aliases(name);-
803 foreach (const QString &optionName, d->optionNames) {-
804 if (aliases.contains(optionName))
aliases.contains(optionName)Description
TRUEevaluated 12 times by 1 test
Evaluated by:
  • tst_QCommandLineParser
FALSEevaluated 1 time by 1 test
Evaluated by:
  • tst_QCommandLineParser
1-12
805 return true;
executed 12 times by 1 test: return true;
Executed by:
  • tst_QCommandLineParser
12
806 }
executed 1 time by 1 test: end of block
Executed by:
  • tst_QCommandLineParser
1
807 return false;
executed 17 times by 1 test: return false;
Executed by:
  • tst_QCommandLineParser
17
808}-
809-
810/*!-
811 Returns the option value found for the given option name \a optionName, or-
812 an empty string if not found.-
813-
814 The name provided can be any long or short name of any option that was-
815 added with \c addOption(). All the option names are treated as being-
816 equivalent. If the name is not recognized or that option was not present, an-
817 empty string is returned.-
818-
819 For options found by the parser, the last value found for-
820 that option is returned. If the option wasn't specified on the command line,-
821 the default value is returned.-
822-
823 An empty string is returned if the option does not take a value.-
824-
825 \sa values(), QCommandLineOption::setDefaultValue(), QCommandLineOption::setDefaultValues()-
826 */-
827-
828QString QCommandLineParser::value(const QString &optionName) const-
829{-
830 d->checkParsed("value");-
831 const QStringList valueList = values(optionName);-
832-
833 if (!valueList.isEmpty())
!valueList.isEmpty()Description
TRUEevaluated 34 times by 1 test
Evaluated by:
  • tst_QCommandLineParser
FALSEevaluated 11 times by 1 test
Evaluated by:
  • tst_QCommandLineParser
11-34
834 return valueList.last();
executed 34 times by 1 test: return valueList.last();
Executed by:
  • tst_QCommandLineParser
34
835-
836 return QString();
executed 11 times by 1 test: return QString();
Executed by:
  • tst_QCommandLineParser
11
837}-
838-
839/*!-
840 Returns a list of option values found for the given option name \a-
841 optionName, or an empty list if not found.-
842-
843 The name provided can be any long or short name of any option that was-
844 added with \c addOption(). All the options names are treated as being-
845 equivalent. If the name is not recognized or that option was not present, an-
846 empty list is returned.-
847-
848 For options found by the parser, the list will contain an entry for-
849 each time the option was encountered by the parser. If the option wasn't-
850 specified on the command line, the default values are returned.-
851-
852 An empty list is returned if the option does not take a value.-
853-
854 \sa value(), QCommandLineOption::setDefaultValue(), QCommandLineOption::setDefaultValues()-
855 */-
856-
857QStringList QCommandLineParser::values(const QString &optionName) const-
858{-
859 d->checkParsed("values");-
860 const NameHash_t::const_iterator it = d->nameHash.constFind(optionName);-
861 if (it != d->nameHash.cend()) {
it != d->nameHash.cend()Description
TRUEevaluated 80 times by 1 test
Evaluated by:
  • tst_QCommandLineParser
FALSEevaluated 4 times by 1 test
Evaluated by:
  • tst_QCommandLineParser
4-80
862 const int optionOffset = *it;-
863 QStringList values = d->optionValuesHash.value(optionOffset);-
864 if (values.isEmpty())
values.isEmpty()Description
TRUEevaluated 30 times by 1 test
Evaluated by:
  • tst_QCommandLineParser
FALSEevaluated 50 times by 1 test
Evaluated by:
  • tst_QCommandLineParser
30-50
865 values = d->commandLineOptionList.at(optionOffset).defaultValues();
executed 30 times by 1 test: values = d->commandLineOptionList.at(optionOffset).defaultValues();
Executed by:
  • tst_QCommandLineParser
30
866 return values;
executed 80 times by 1 test: return values;
Executed by:
  • tst_QCommandLineParser
80
867 }-
868-
869 qWarning("QCommandLineParser: option not defined: \"%s\"", qPrintable(optionName));-
870 return QStringList();
executed 4 times by 1 test: return QStringList();
Executed by:
  • tst_QCommandLineParser
4
871}-
872-
873/*!-
874 \overload-
875 Checks whether the \a option was passed to the application.-
876-
877 Returns \c true if the \a option was set, false otherwise.-
878-
879 This is the recommended way to check for options with no values.-
880-
881 Example:-
882 \snippet code/src_corelib_tools_qcommandlineparser.cpp 1-
883*/-
884bool QCommandLineParser::isSet(const QCommandLineOption &option) const-
885{-
886 // option.names() might be empty if the constructor failed-
887 return !option.names().isEmpty() && isSet(option.names().first());
executed 8 times by 1 test: return !option.names().isEmpty() && isSet(option.names().first());
Executed by:
  • tst_QCommandLineParser
!option.names().isEmpty()Description
TRUEevaluated 8 times by 1 test
Evaluated by:
  • tst_QCommandLineParser
FALSEnever evaluated
isSet(option.names().first())Description
TRUEevaluated 6 times by 1 test
Evaluated by:
  • tst_QCommandLineParser
FALSEevaluated 2 times by 1 test
Evaluated by:
  • tst_QCommandLineParser
0-8
888}-
889-
890/*!-
891 \overload-
892 Returns the option value found for the given \a option, or-
893 an empty string if not found.-
894-
895 For options found by the parser, the last value found for-
896 that option is returned. If the option wasn't specified on the command line,-
897 the default value is returned.-
898-
899 An empty string is returned if the option does not take a value.-
900-
901 \sa values(), QCommandLineOption::setDefaultValue(), QCommandLineOption::setDefaultValues()-
902*/-
903QString QCommandLineParser::value(const QCommandLineOption &option) const-
904{-
905 return value(option.names().first());
never executed: return value(option.names().first());
0
906}-
907-
908/*!-
909 \overload-
910 Returns a list of option values found for the given \a option,-
911 or an empty list if not found.-
912-
913 For options found by the parser, the list will contain an entry for-
914 each time the option was encountered by the parser. If the option wasn't-
915 specified on the command line, the default values are returned.-
916-
917 An empty list is returned if the option does not take a value.-
918-
919 \sa value(), QCommandLineOption::setDefaultValue(), QCommandLineOption::setDefaultValues()-
920*/-
921QStringList QCommandLineParser::values(const QCommandLineOption &option) const-
922{-
923 return values(option.names().first());
executed 8 times by 1 test: return values(option.names().first());
Executed by:
  • tst_QCommandLineParser
8
924}-
925-
926/*!-
927 Returns a list of positional arguments.-
928-
929 These are all of the arguments that were not recognized as part of an-
930 option.-
931 */-
932-
933QStringList QCommandLineParser::positionalArguments() const-
934{-
935 d->checkParsed("positionalArguments");-
936 return d->positionalArgumentList;
executed 25 times by 1 test: return d->positionalArgumentList;
Executed by:
  • tst_QCommandLineParser
25
937}-
938-
939/*!-
940 Returns a list of option names that were found.-
941-
942 This returns a list of all the recognized option names found by the-
943 parser, in the order in which they were found. For any long options-
944 that were in the form {--option=value}, the value part will have been-
945 dropped.-
946-
947 The names in this list do not include the preceding dash characters.-
948 Names may appear more than once in this list if they were encountered-
949 more than once by the parser.-
950-
951 Any entry in the list can be used with \c value() or with-
952 \c values() to get any relevant option values.-
953 */-
954-
955QStringList QCommandLineParser::optionNames() const-
956{-
957 d->checkParsed("optionNames");-
958 return d->optionNames;
executed 35 times by 1 test: return d->optionNames;
Executed by:
  • tst_QCommandLineParser
35
959}-
960-
961/*!-
962 Returns a list of unknown option names.-
963-
964 This list will include both long an short name options that were not-
965 recognized. For any long options that were in the form {--option=value},-
966 the value part will have been dropped and only the long name is added.-
967-
968 The names in this list do not include the preceding dash characters.-
969 Names may appear more than once in this list if they were encountered-
970 more than once by the parser.-
971-
972 \sa optionNames()-
973 */-
974-
975QStringList QCommandLineParser::unknownOptionNames() const-
976{-
977 d->checkParsed("unknownOptionNames");-
978 return d->unknownOptionNames;
executed 26 times by 1 test: return d->unknownOptionNames;
Executed by:
  • tst_QCommandLineParser
26
979}-
980-
981/*!-
982 Displays the version information from QCoreApplication::applicationVersion(),-
983 and exits the application.-
984 This is automatically triggered by the --version option, but can also-
985 be used to display the version when not using process().-
986 The exit code is set to EXIT_SUCCESS (0).-
987-
988 \sa addVersionOption()-
989 \since 5.4-
990*/-
991Q_NORETURN void QCommandLineParser::showVersion()-
992{-
993 showParserMessage(QCoreApplication::applicationName() + QLatin1Char(' ')-
994 + QCoreApplication::applicationVersion() + QLatin1Char('\n'),-
995 UsageMessage);-
996 qt_call_post_routines();-
997 ::exit(EXIT_SUCCESS);
never executed: ::exit(0);
0
998}-
999-
1000/*!-
1001 Displays the help information, and exits the application.-
1002 This is automatically triggered by the --help option, but can also-
1003 be used to display the help when the user is not invoking the-
1004 application correctly.-
1005 The exit code is set to \a exitCode. It should be set to 0 if the-
1006 user requested to see the help, and to any other value in case of-
1007 an error.-
1008-
1009 \sa helpText()-
1010*/-
1011Q_NORETURN void QCommandLineParser::showHelp(int exitCode)-
1012{-
1013 showParserMessage(d->helpText(), UsageMessage);-
1014 qt_call_post_routines();-
1015 ::exit(exitCode);
never executed: ::exit(exitCode);
0
1016}-
1017-
1018/*!-
1019 Returns a string containing the complete help information.-
1020-
1021 \sa showHelp()-
1022*/-
1023QString QCommandLineParser::helpText() const-
1024{-
1025 return d->helpText();
executed 1 time by 1 test: return d->helpText();
Executed by:
  • tst_QCommandLineParser
1
1026}-
1027-
1028static QString wrapText(const QString &names, int longestOptionNameString, const QString &description)-
1029{-
1030 const QLatin1Char nl('\n');-
1031 QString text = QStringLiteral(" ") + names.leftJustified(longestOptionNameString) + QLatin1Char(' ');
executed 1 time by 1 test: return qstring_literal_temp;
Executed by:
  • tst_QCommandLineParser
1
1032 const int indent = text.length();-
1033 int lineStart = 0;-
1034 int lastBreakable = -1;-
1035 const int max = 79 - indent;-
1036 int x = 0;-
1037 const int len = description.length();-
1038-
1039 for (int i = 0; i < len; ++i) {
i < lenDescription
TRUEevaluated 30 times by 1 test
Evaluated by:
  • tst_QCommandLineParser
FALSEevaluated 1 time by 1 test
Evaluated by:
  • tst_QCommandLineParser
1-30
1040 ++x;-
1041 const QChar c = description.at(i);-
1042 if (c.isSpace())
c.isSpace()Description
TRUEevaluated 4 times by 1 test
Evaluated by:
  • tst_QCommandLineParser
FALSEevaluated 26 times by 1 test
Evaluated by:
  • tst_QCommandLineParser
4-26
1043 lastBreakable = i;
executed 4 times by 1 test: lastBreakable = i;
Executed by:
  • tst_QCommandLineParser
4
1044-
1045 int breakAt = -1;-
1046 int nextLineStart = -1;-
1047 if (x > max && lastBreakable != -1) {
x > maxDescription
TRUEnever evaluated
FALSEevaluated 30 times by 1 test
Evaluated by:
  • tst_QCommandLineParser
lastBreakable != -1Description
TRUEnever evaluated
FALSEnever evaluated
0-30
1048 // time to break and we know where-
1049 breakAt = lastBreakable;-
1050 nextLineStart = lastBreakable + 1;-
1051 } else if ((x > max - 1 && lastBreakable == -1) || i == len - 1) {
never executed: end of block
x > max - 1Description
TRUEnever evaluated
FALSEevaluated 30 times by 1 test
Evaluated by:
  • tst_QCommandLineParser
lastBreakable == -1Description
TRUEnever evaluated
FALSEnever evaluated
i == len - 1Description
TRUEevaluated 1 time by 1 test
Evaluated by:
  • tst_QCommandLineParser
FALSEevaluated 29 times by 1 test
Evaluated by:
  • tst_QCommandLineParser
0-30
1052 // time to break but found nowhere [-> break here], or end of last line-
1053 breakAt = i + 1;-
1054 nextLineStart = breakAt;-
1055 } else if (c == nl) {
executed 1 time by 1 test: end of block
Executed by:
  • tst_QCommandLineParser
c == nlDescription
TRUEnever evaluated
FALSEevaluated 29 times by 1 test
Evaluated by:
  • tst_QCommandLineParser
0-29
1056 // forced break-
1057 breakAt = i;-
1058 nextLineStart = i + 1;-
1059 }
never executed: end of block
0
1060-
1061 if (breakAt != -1) {
breakAt != -1Description
TRUEevaluated 1 time by 1 test
Evaluated by:
  • tst_QCommandLineParser
FALSEevaluated 29 times by 1 test
Evaluated by:
  • tst_QCommandLineParser
1-29
1062 const int numChars = breakAt - lineStart;-
1063 //qDebug() << "breakAt=" << description.at(breakAt) << "breakAtSpace=" << breakAtSpace << lineStart << "to" << breakAt << description.mid(lineStart, numChars);-
1064 if (lineStart > 0)
lineStart > 0Description
TRUEnever evaluated
FALSEevaluated 1 time by 1 test
Evaluated by:
  • tst_QCommandLineParser
0-1
1065 text += QString(indent, QLatin1Char(' '));
never executed: text += QString(indent, QLatin1Char(' '));
0
1066 text += description.midRef(lineStart, numChars) + nl;-
1067 x = 0;-
1068 lastBreakable = -1;-
1069 lineStart = nextLineStart;-
1070 if (lineStart < len && description.at(lineStart).isSpace())
lineStart < lenDescription
TRUEnever evaluated
FALSEevaluated 1 time by 1 test
Evaluated by:
  • tst_QCommandLineParser
description.at...art).isSpace()Description
TRUEnever evaluated
FALSEnever evaluated
0-1
1071 ++lineStart; // don't start a line with a space
never executed: ++lineStart;
0
1072 i = lineStart;-
1073 }
executed 1 time by 1 test: end of block
Executed by:
  • tst_QCommandLineParser
1
1074 }
executed 30 times by 1 test: end of block
Executed by:
  • tst_QCommandLineParser
30
1075-
1076 return text;
executed 1 time by 1 test: return text;
Executed by:
  • tst_QCommandLineParser
1
1077}-
1078-
1079QString QCommandLineParserPrivate::helpText() const-
1080{-
1081 const QLatin1Char nl('\n');-
1082 QString text;-
1083 const QString exeName = QCoreApplication::instance()->arguments().first();-
1084 QString usage = exeName;-
1085 if (!commandLineOptionList.isEmpty()) {
!commandLineOp...List.isEmpty()Description
TRUEevaluated 1 time by 1 test
Evaluated by:
  • tst_QCommandLineParser
FALSEnever evaluated
0-1
1086 usage += QLatin1Char(' ');-
1087 usage += QCommandLineParser::tr("[options]");-
1088 }
executed 1 time by 1 test: end of block
Executed by:
  • tst_QCommandLineParser
1
1089 foreach (const PositionalArgumentDefinition &arg, positionalArgumentDefinitions) {-
1090 usage += QLatin1Char(' ');-
1091 usage += arg.syntax;-
1092 }
never executed: end of block
0
1093 text += QCommandLineParser::tr("Usage: %1").arg(usage) + nl;-
1094 if (!description.isEmpty())
!description.isEmpty()Description
TRUEnever evaluated
FALSEevaluated 1 time by 1 test
Evaluated by:
  • tst_QCommandLineParser
0-1
1095 text += description + nl;
never executed: text += description + nl;
0
1096 text += nl;-
1097 if (!commandLineOptionList.isEmpty())
!commandLineOp...List.isEmpty()Description
TRUEevaluated 1 time by 1 test
Evaluated by:
  • tst_QCommandLineParser
FALSEnever evaluated
0-1
1098 text += QCommandLineParser::tr("Options:") + nl;
executed 1 time by 1 test: text += QCommandLineParser::tr("Options:") + nl;
Executed by:
  • tst_QCommandLineParser
1
1099 QStringList optionNameList;-
1100 int longestOptionNameString = 0;-
1101 foreach (const QCommandLineOption &option, commandLineOptionList) {-
1102 QStringList optionNames;-
1103 foreach (const QString &optionName, option.names()) {-
1104 if (optionName.length() == 1)
optionName.length() == 1Description
TRUEnever evaluated
FALSEevaluated 1 time by 1 test
Evaluated by:
  • tst_QCommandLineParser
0-1
1105 optionNames.append(QLatin1Char('-') + optionName);
never executed: optionNames.append(QLatin1Char('-') + optionName);
0
1106 else-
1107 optionNames.append(QStringLiteral("--") + optionName);
executed 1 time by 1 test: optionNames.append(([]() -> QString { enum { Size = sizeof(u"" "--")/2 - 1 }; static const QStaticStringData<Size> qstring_literal = { { { { -1 } }, Size, 0, 0, sizeof(QStringData) }, u"" "--" }; QStringDataPtr holder = { qstring_literal.data_ptr() }; const QString qstring_literal_temp(holder); return qstring_literal_temp; }()) + optionName);
Executed by:
  • tst_QCommandLineParser
executed 1 time by 1 test: return qstring_literal_temp;
Executed by:
  • tst_QCommandLineParser
1
1108 }-
1109 QString optionNamesString = optionNames.join(QStringLiteral(", "));
executed 1 time by 1 test: return qstring_literal_temp;
Executed by:
  • tst_QCommandLineParser
1
1110 if (!option.valueName().isEmpty())
!option.valueName().isEmpty()Description
TRUEevaluated 1 time by 1 test
Evaluated by:
  • tst_QCommandLineParser
FALSEnever evaluated
0-1
1111 optionNamesString += QStringLiteral(" <") + option.valueName() + QLatin1Char('>');
executed 1 time by 1 test: optionNamesString += ([]() -> QString { enum { Size = sizeof(u"" " <")/2 - 1 }; static const QStaticStringData<Size> qstring_literal = { { { { -1 } }, Size, 0, 0, sizeof(QStringData) }, u"" " <" }; QStringDataPtr holder = { qstring_literal.data_ptr() }; const QString qstring_literal_temp(holder); return qstring_literal_temp; }()) + option.valueName() + QLatin1Char('>');
Executed by:
  • tst_QCommandLineParser
executed 1 time by 1 test: return qstring_literal_temp;
Executed by:
  • tst_QCommandLineParser
1
1112 optionNameList.append(optionNamesString);-
1113 longestOptionNameString = qMax(longestOptionNameString, optionNamesString.length());-
1114 }
executed 1 time by 1 test: end of block
Executed by:
  • tst_QCommandLineParser
1
1115 ++longestOptionNameString;-
1116 for (int i = 0; i < commandLineOptionList.count(); ++i) {
i < commandLin...onList.count()Description
TRUEevaluated 1 time by 1 test
Evaluated by:
  • tst_QCommandLineParser
FALSEevaluated 1 time by 1 test
Evaluated by:
  • tst_QCommandLineParser
1
1117 const QCommandLineOption &option = commandLineOptionList.at(i);-
1118 if (option.isHidden())
option.isHidden()Description
TRUEnever evaluated
FALSEevaluated 1 time by 1 test
Evaluated by:
  • tst_QCommandLineParser
0-1
1119 continue;
never executed: continue;
0
1120 text += wrapText(optionNameList.at(i), longestOptionNameString, option.description());-
1121 }
executed 1 time by 1 test: end of block
Executed by:
  • tst_QCommandLineParser
1
1122 if (!positionalArgumentDefinitions.isEmpty()) {
!positionalArg...ions.isEmpty()Description
TRUEnever evaluated
FALSEevaluated 1 time by 1 test
Evaluated by:
  • tst_QCommandLineParser
0-1
1123 if (!commandLineOptionList.isEmpty())
!commandLineOp...List.isEmpty()Description
TRUEnever evaluated
FALSEnever evaluated
0
1124 text += nl;
never executed: text += nl;
0
1125 text += QCommandLineParser::tr("Arguments:") + nl;-
1126 foreach (const PositionalArgumentDefinition &arg, positionalArgumentDefinitions) {-
1127 text += wrapText(arg.name, longestOptionNameString, arg.description);-
1128 }
never executed: end of block
0
1129 }
never executed: end of block
0
1130 return text;
executed 1 time by 1 test: return text;
Executed by:
  • tst_QCommandLineParser
1
1131}-
1132-
1133QT_END_NAMESPACE-
Source codeSwitch to Preprocessed file

Generated by Squish Coco Non-Commercial 4.3.0-BETA-master-30-08-2018-4cb69e9