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