OpenCoverage

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

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