qcommandlineoption.cpp

Absolute File Name:/home/qt/qt5_coco/qt5/qtbase/src/corelib/tools/qcommandlineoption.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 "qcommandlineoption.h"-
42-
43#include "qset.h"-
44-
45QT_BEGIN_NAMESPACE-
46-
47class QCommandLineOptionPrivate : public QSharedData-
48{-
49public:-
50 Q_NEVER_INLINE-
51 explicit QCommandLineOptionPrivate(const QString &name)-
52 : names(removeInvalidNames(QStringList(name))),-
53 hidden(false)-
54 { }
executed 167 times by 2 tests: end of block
Executed by:
  • tst_QCommandLineParser
  • tst_qdbusxml2cpp - unknown status
167
55-
56 Q_NEVER_INLINE-
57 explicit QCommandLineOptionPrivate(const QStringList &names)-
58 : names(removeInvalidNames(names)),-
59 hidden(false)-
60 { }
executed 1110 times by 2 tests: end of block
Executed by:
  • tst_QCommandLineParser
  • tst_qdbusxml2cpp - unknown status
1110
61-
62 static QStringList removeInvalidNames(QStringList nameList);-
63-
64 //! The list of names used for this option.-
65 QStringList names;-
66-
67 //! The documentation name for the value, if one is expected-
68 //! Example: "-o <file>" means valueName == "file"-
69 QString valueName;-
70-
71 //! The description used for this option.-
72 QString description;-
73-
74 //! The list of default values used for this option.-
75 QStringList defaultValues;-
76-
77 //! Show or hide in --help-
78 bool hidden;-
79};-
80-
81/*!-
82 \since 5.2-
83 \class QCommandLineOption-
84 \brief The QCommandLineOption class defines a possible command-line option.-
85 \inmodule QtCore-
86 \ingroup shared-
87 \ingroup tools-
88-
89 This class is used to describe an option on the command line. It allows-
90 different ways of defining the same option with multiple aliases possible.-
91 It is also used to describe how the option is used - it may be a flag (e.g. \c{-v})-
92 or take a value (e.g. \c{-o file}).-
93-
94 Examples:-
95 \snippet code/src_corelib_tools_qcommandlineoption.cpp 0-
96-
97 \sa QCommandLineParser-
98*/-
99-
100/*!-
101 \fn QCommandLineOption &QCommandLineOption::operator=(QCommandLineOption &&other)-
102-
103 Move-assigns \a other to this QCommandLineOption instance.-
104-
105 \since 5.2-
106*/-
107-
108/*!-
109 Constructs a command line option object with the name \a name.-
110-
111 The name can be either short or long. If the name is one character in-
112 length, it is considered a short name. Option names must not be empty,-
113 must not start with a dash or a slash character, must not contain a \c{=}-
114 and cannot be repeated.-
115-
116 \sa setDescription(), setValueName(), setDefaultValues()-
117*/-
118QCommandLineOption::QCommandLineOption(const QString &name)-
119 : d(new QCommandLineOptionPrivate(name))-
120{-
121}
executed 2 times by 1 test: end of block
Executed by:
  • tst_QCommandLineParser
2
122-
123/*!-
124 Constructs a command line option object with the names \a names.-
125-
126 This overload allows to set multiple names for the option, for instance-
127 \c{o} and \c{output}.-
128-
129 The names can be either short or long. Any name in the list that is one-
130 character in length is a short name. Option names must not be empty,-
131 must not start with a dash or a slash character, must not contain a \c{=}-
132 and cannot be repeated.-
133-
134 \sa setDescription(), setValueName(), setDefaultValues()-
135*/-
136QCommandLineOption::QCommandLineOption(const QStringList &names)-
137 : d(new QCommandLineOptionPrivate(names))-
138{-
139}
never executed: end of block
0
140-
141/*!-
142 Constructs a command line option object with the given arguments.-
143-
144 The name of the option is set to \a name.-
145 The name can be either short or long. If the name is one character in-
146 length, it is considered a short name. Option names must not be empty,-
147 must not start with a dash or a slash character, must not contain a \c{=}-
148 and cannot be repeated.-
149-
150 The description is set to \a description. It is customary to add a "."-
151 at the end of the description.-
152-
153 In addition, the \a valueName needs to be set if the option expects a value.-
154 The default value for the option is set to \a defaultValue.-
155-
156 In Qt versions before 5.4, this constructor was \c explicit. In Qt 5.4-
157 and later, it no longer is and can be used for C++11-style uniform-
158 initialization:-
159-
160 \snippet code/src_corelib_tools_qcommandlineoption.cpp cxx11-init-
161-
162 \sa setDescription(), setValueName(), setDefaultValues()-
163*/-
164QCommandLineOption::QCommandLineOption(const QString &name, const QString &description,-
165 const QString &valueName,-
166 const QString &defaultValue)-
167 : d(new QCommandLineOptionPrivate(name))-
168{-
169 setValueName(valueName);-
170 setDescription(description);-
171 setDefaultValue(defaultValue);-
172}
executed 165 times by 2 tests: end of block
Executed by:
  • tst_QCommandLineParser
  • tst_qdbusxml2cpp - unknown status
165
173-
174/*!-
175 Constructs a command line option object with the given arguments.-
176-
177 This overload allows to set multiple names for the option, for instance-
178 \c{o} and \c{output}.-
179-
180 The names of the option are set to \a names.-
181 The names can be either short or long. Any name in the list that is one-
182 character in length is a short name. Option names must not be empty,-
183 must not start with a dash or a slash character, must not contain a \c{=}-
184 and cannot be repeated.-
185-
186 The description is set to \a description. It is customary to add a "."-
187 at the end of the description.-
188-
189 In addition, the \a valueName needs to be set if the option expects a value.-
190 The default value for the option is set to \a defaultValue.-
191-
192 In Qt versions before 5.4, this constructor was \c explicit. In Qt 5.4-
193 and later, it no longer is and can be used for C++11-style uniform-
194 initialization:-
195-
196 \snippet code/src_corelib_tools_qcommandlineoption.cpp cxx11-init-list-
197-
198 \sa setDescription(), setValueName(), setDefaultValues()-
199*/-
200QCommandLineOption::QCommandLineOption(const QStringList &names, const QString &description,-
201 const QString &valueName,-
202 const QString &defaultValue)-
203 : d(new QCommandLineOptionPrivate(names))-
204{-
205 setValueName(valueName);-
206 setDescription(description);-
207 setDefaultValue(defaultValue);-
208}
executed 1110 times by 2 tests: end of block
Executed by:
  • tst_QCommandLineParser
  • tst_qdbusxml2cpp - unknown status
1110
209-
210/*!-
211 Constructs a QCommandLineOption object that is a copy of the QCommandLineOption-
212 object \a other.-
213-
214 \sa operator=()-
215*/-
216QCommandLineOption::QCommandLineOption(const QCommandLineOption &other)-
217 : d(other.d)-
218{-
219}
executed 1278 times by 2 tests: end of block
Executed by:
  • tst_QCommandLineParser
  • tst_qdbusxml2cpp - unknown status
1278
220-
221/*!-
222 Destroys the command line option object.-
223*/-
224QCommandLineOption::~QCommandLineOption()-
225{-
226}-
227-
228/*!-
229 Makes a copy of the \a other object and assigns it to this QCommandLineOption-
230 object.-
231*/-
232QCommandLineOption &QCommandLineOption::operator=(const QCommandLineOption &other)-
233{-
234 d = other.d;-
235 return *this;
never executed: return *this;
0
236}-
237-
238/*!-
239 \fn void QCommandLineOption::swap(QCommandLineOption &other)-
240-
241 Swaps option \a other with this option. This operation is very-
242 fast and never fails.-
243*/-
244-
245/*!-
246 Returns the names set for this option.-
247 */-
248QStringList QCommandLineOption::names() const-
249{-
250 return d->names;
executed 2763 times by 2 tests: return d->names;
Executed by:
  • tst_QCommandLineParser
  • tst_qdbusxml2cpp - unknown status
2763
251}-
252-
253namespace {-
254 struct IsInvalidName-
255 {-
256 typedef bool result_type;-
257 typedef QString argument_type;-
258-
259 Q_NEVER_INLINE-
260 result_type operator()(const QString &name) const Q_DECL_NOEXCEPT-
261 {-
262 if (Q_UNLIKELY(name.isEmpty()))
__builtin_expe...pty()), false)Description
TRUEnever evaluated
FALSEevaluated 2387 times by 2 tests
Evaluated by:
  • tst_QCommandLineParser
  • tst_qdbusxml2cpp - unknown status
0-2387
263 return warn("be empty");
never executed: return warn("be empty");
0
264-
265 const QChar c = name.at(0);-
266 if (Q_UNLIKELY(c == QLatin1Char('-')))
__builtin_expe...('-')), false)Description
TRUEevaluated 1 time by 1 test
Evaluated by:
  • tst_QCommandLineParser
FALSEevaluated 2386 times by 2 tests
Evaluated by:
  • tst_QCommandLineParser
  • tst_qdbusxml2cpp - unknown status
1-2386
267 return warn("start with a '-'");
executed 1 time by 1 test: return warn("start with a '-'");
Executed by:
  • tst_QCommandLineParser
1
268 if (Q_UNLIKELY(c == QLatin1Char('/')))
__builtin_expe...('/')), false)Description
TRUEnever evaluated
FALSEevaluated 2386 times by 2 tests
Evaluated by:
  • tst_QCommandLineParser
  • tst_qdbusxml2cpp - unknown status
0-2386
269 return warn("start with a '/'");
never executed: return warn("start with a '/'");
0
270 if (Q_UNLIKELY(name.contains(QLatin1Char('='))))
__builtin_expe...'='))), false)Description
TRUEnever evaluated
FALSEevaluated 2386 times by 2 tests
Evaluated by:
  • tst_QCommandLineParser
  • tst_qdbusxml2cpp - unknown status
0-2386
271 return warn("contain a '='");
never executed: return warn("contain a '='");
0
272-
273 return false;
executed 2386 times by 2 tests: return false;
Executed by:
  • tst_QCommandLineParser
  • tst_qdbusxml2cpp - unknown status
2386
274 }-
275-
276 Q_NEVER_INLINE-
277 static bool warn(const char *what) Q_DECL_NOEXCEPT-
278 {-
279 qWarning("QCommandLineOption: Option names cannot %s", what);-
280 return true;
executed 1 time by 1 test: return true;
Executed by:
  • tst_QCommandLineParser
1
281 }-
282 };-
283} // unnamed namespace-
284-
285// static-
286QStringList QCommandLineOptionPrivate::removeInvalidNames(QStringList nameList)-
287{-
288 if (Q_UNLIKELY(nameList.isEmpty()))
__builtin_expe...pty()), false)Description
TRUEnever evaluated
FALSEevaluated 1277 times by 2 tests
Evaluated by:
  • tst_QCommandLineParser
  • tst_qdbusxml2cpp - unknown status
0-1277
289 qWarning("QCommandLineOption: Options must have at least one name");
never executed: QMessageLogger(__FILE__, 289, __PRETTY_FUNCTION__).warning("QCommandLineOption: Options must have at least one name");
0
290 else-
291 nameList.erase(std::remove_if(nameList.begin(), nameList.end(), IsInvalidName()),
executed 1277 times by 2 tests: nameList.erase(std::remove_if(nameList.begin(), nameList.end(), IsInvalidName()), nameList.end());
Executed by:
  • tst_QCommandLineParser
  • tst_qdbusxml2cpp - unknown status
1277
292 nameList.end());
executed 1277 times by 2 tests: nameList.erase(std::remove_if(nameList.begin(), nameList.end(), IsInvalidName()), nameList.end());
Executed by:
  • tst_QCommandLineParser
  • tst_qdbusxml2cpp - unknown status
1277
293 return nameList;
executed 1277 times by 2 tests: return nameList;
Executed by:
  • tst_QCommandLineParser
  • tst_qdbusxml2cpp - unknown status
1277
294}-
295-
296/*!-
297 Sets the name of the expected value, for the documentation, to \a valueName.-
298-
299 Options without a value assigned have a boolean-like behavior:-
300 either the user specifies --option or they don't.-
301-
302 Options with a value assigned need to set a name for the expected value,-
303 for the documentation of the option in the help output. An option with names \c{o} and \c{output},-
304 and a value name of \c{file} will appear as \c{-o, --output <file>}.-
305-
306 Call QCommandLineParser::value() if you expect the option to be present-
307 only once, and QCommandLineParser::values() if you expect that option-
308 to be present multiple times.-
309-
310 \sa valueName()-
311 */-
312void QCommandLineOption::setValueName(const QString &valueName)-
313{-
314 d->valueName = valueName;-
315}
executed 1277 times by 2 tests: end of block
Executed by:
  • tst_QCommandLineParser
  • tst_qdbusxml2cpp - unknown status
1277
316-
317/*!-
318 Returns the name of the expected value.-
319-
320 If empty, the option doesn't take a value.-
321-
322 \sa setValueName()-
323 */-
324QString QCommandLineOption::valueName() const-
325{-
326 return d->valueName;
executed 541 times by 2 tests: return d->valueName;
Executed by:
  • tst_QCommandLineParser
  • tst_qdbusxml2cpp - unknown status
541
327}-
328-
329/*!-
330 Sets the description used for this option to \a description.-
331-
332 It is customary to add a "." at the end of the description.-
333-
334 The description is used by QCommandLineParser::showHelp().-
335-
336 \sa description()-
337 */-
338void QCommandLineOption::setDescription(const QString &description)-
339{-
340 d->description = description;-
341}
executed 1275 times by 2 tests: end of block
Executed by:
  • tst_QCommandLineParser
  • tst_qdbusxml2cpp - unknown status
1275
342-
343/*!-
344 Returns the description set for this option.-
345-
346 \sa setDescription()-
347 */-
348QString QCommandLineOption::description() const-
349{-
350 return d->description;
executed 1 time by 1 test: return d->description;
Executed by:
  • tst_QCommandLineParser
1
351}-
352-
353/*!-
354 Sets the default value used for this option to \a defaultValue.-
355-
356 The default value is used if the user of the application does not specify-
357 the option on the command line.-
358-
359 If \a defaultValue is empty, the option has no default values.-
360-
361 \sa defaultValues() setDefaultValues()-
362 */-
363void QCommandLineOption::setDefaultValue(const QString &defaultValue)-
364{-
365 QStringList newDefaultValues;-
366 if (!defaultValue.isEmpty()) {
!defaultValue.isEmpty()Description
TRUEevaluated 2 times by 1 test
Evaluated by:
  • tst_QCommandLineParser
FALSEevaluated 1275 times by 2 tests
Evaluated by:
  • tst_QCommandLineParser
  • tst_qdbusxml2cpp - unknown status
2-1275
367 newDefaultValues.reserve(1);-
368 newDefaultValues << defaultValue;-
369 }
executed 2 times by 1 test: end of block
Executed by:
  • tst_QCommandLineParser
2
370 // commit:-
371 d->defaultValues.swap(newDefaultValues);-
372}
executed 1277 times by 2 tests: end of block
Executed by:
  • tst_QCommandLineParser
  • tst_qdbusxml2cpp - unknown status
1277
373-
374/*!-
375 Sets the list of default values used for this option to \a defaultValues.-
376-
377 The default values are used if the user of the application does not specify-
378 the option on the command line.-
379-
380 \sa defaultValues() setDefaultValue()-
381 */-
382void QCommandLineOption::setDefaultValues(const QStringList &defaultValues)-
383{-
384 d->defaultValues = defaultValues;-
385}
executed 4 times by 1 test: end of block
Executed by:
  • tst_QCommandLineParser
4
386-
387/*!-
388 Returns the default values set for this option.-
389-
390 \sa setDefaultValues()-
391 */-
392QStringList QCommandLineOption::defaultValues() const-
393{-
394 return d->defaultValues;
executed 513 times by 2 tests: return d->defaultValues;
Executed by:
  • tst_QCommandLineParser
  • tst_qdbusxml2cpp - unknown status
513
395}-
396-
397/*!-
398 Sets whether to hide this option in the user-visible help output.-
399-
400 All options are visible by default. Setting \a hide to true for-
401 a particular option makes it internal, i.e. not listed in the help output.-
402-
403 \since 5.6-
404 \sa isHidden-
405 */-
406void QCommandLineOption::setHidden(bool hide)-
407{-
408 d->hidden = hide;-
409}
never executed: end of block
0
410-
411/*!-
412 Returns true if this option is omitted from the help output,-
413 false if the option is listed.-
414-
415 \since 5.6-
416 \sa setHidden()-
417 */-
418bool QCommandLineOption::isHidden() const-
419{-
420 return d->hidden;
executed 2 times by 1 test: return d->hidden;
Executed by:
  • tst_QCommandLineParser
2
421}-
422-
423QT_END_NAMESPACE-
Source codeSwitch to Preprocessed file

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