QtBase  v6.3.1
qcommandlineoption.cpp
Go to the documentation of this file.
1 /****************************************************************************
2 **
3 ** Copyright (C) 2020 The Qt Company Ltd.
4 ** Copyright (C) 2013 Laszlo Papp <lpapp@kde.org>
5 ** Copyright (C) 2013 David Faure <faure@kde.org>
6 ** Contact: https://www.qt.io/licensing/
7 **
8 ** This file is part of the QtCore module of the Qt Toolkit.
9 **
10 ** $QT_BEGIN_LICENSE:LGPL$
11 ** Commercial License Usage
12 ** Licensees holding valid commercial Qt licenses may use this file in
13 ** accordance with the commercial license agreement provided with the
14 ** Software or, alternatively, in accordance with the terms contained in
15 ** a written agreement between you and The Qt Company. For licensing terms
16 ** and conditions see https://www.qt.io/terms-conditions. For further
17 ** information use the contact form at https://www.qt.io/contact-us.
18 **
19 ** GNU Lesser General Public License Usage
20 ** Alternatively, this file may be used under the terms of the GNU Lesser
21 ** General Public License version 3 as published by the Free Software
22 ** Foundation and appearing in the file LICENSE.LGPL3 included in the
23 ** packaging of this file. Please review the following information to
24 ** ensure the GNU Lesser General Public License version 3 requirements
25 ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
26 **
27 ** GNU General Public License Usage
28 ** Alternatively, this file may be used under the terms of the GNU
29 ** General Public License version 2.0 or (at your option) the GNU General
30 ** Public license version 3 or any later version approved by the KDE Free
31 ** Qt Foundation. The licenses are as published by the Free Software
32 ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
33 ** included in the packaging of this file. Please review the following
34 ** information to ensure the GNU General Public License requirements will
35 ** be met: https://www.gnu.org/licenses/gpl-2.0.html and
36 ** https://www.gnu.org/licenses/gpl-3.0.html.
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41 
42 #include "qcommandlineoption.h"
43 
44 #include "qset.h"
45 
47 
49 {
50 public:
51  Q_NEVER_INLINE
54  { }
55 
56  Q_NEVER_INLINE
59  { }
60 
62 
65 
69 
72 
75 
77 };
78 
118 {
119 }
120 
136 {
137 }
138 
163  const QString &valueName,
164  const QString &defaultValue)
166 {
169  setDefaultValue(defaultValue);
170 }
171 
199  const QString &valueName,
200  const QString &defaultValue)
202 {
205  setDefaultValue(defaultValue);
206 }
207 
215  : d(other.d)
216 {
217 }
218 
223 {
224 }
225 
231 {
232  d = other.d;
233  return *this;
234 }
235 
247 {
248  return d->names;
249 }
250 
251 namespace {
252  struct IsInvalidName
253  {
254  typedef bool result_type;
255  typedef QString argument_type;
256 
257  Q_NEVER_INLINE
258  result_type operator()(const QString &name) const noexcept
259  {
260  if (Q_UNLIKELY(name.isEmpty()))
261  return warn("be empty");
262 
263  const QChar c = name.at(0);
264  if (Q_UNLIKELY(c == QLatin1Char('-')))
265  return warn("start with a '-'");
266  if (Q_UNLIKELY(c == QLatin1Char('/')))
267  return warn("start with a '/'");
268  if (Q_UNLIKELY(name.contains(QLatin1Char('='))))
269  return warn("contain a '='");
270 
271  return false;
272  }
273 
274  Q_NEVER_INLINE
275  static bool warn(const char *what) noexcept
276  {
277  qWarning("QCommandLineOption: Option names cannot %s", what);
278  return true;
279  }
280  };
281 } // unnamed namespace
282 
283 // static
285 {
286  if (Q_UNLIKELY(nameList.isEmpty()))
287  qWarning("QCommandLineOption: Options must have at least one name");
288  else
289  nameList.removeIf(IsInvalidName());
290  return nameList;
291 }
292 
310 {
311  d->valueName = valueName;
312 }
313 
322 {
323  return d->valueName;
324 }
325 
336 {
338 }
339 
346 {
347  return d->description;
348 }
349 
361 {
362  QStringList newDefaultValues;
363  if (!defaultValue.isEmpty()) {
364  newDefaultValues.reserve(1);
365  newDefaultValues << defaultValue;
366  }
367  // commit:
368  d->defaultValues.swap(newDefaultValues);
369 }
370 
380 {
382 }
383 
390 {
391  return d->defaultValues;
392 }
393 
401 {
402  return d->flags;
403 }
404 
412 {
413  d->flags = flags;
414 }
415 
The QChar class provides a 16-bit Unicode character.
Definition: qchar.h:84
The QCommandLineOption class defines a possible command-line option. \inmodule QtCore.
void setDescription(const QString &description)
void setDefaultValue(const QString &defaultValue)
QString valueName() const
QString description() const
void setFlags(Flags aflags)
void setDefaultValues(const QStringList &defaultValues)
void setValueName(const QString &name)
QStringList defaultValues() const
QCommandLineOption & operator=(const QCommandLineOption &other)
QStringList names() const
QCommandLineOption(const QString &name)
QCommandLineOption::Flags flags
static QStringList removeInvalidNames(QStringList nameList)
Q_NEVER_INLINE QCommandLineOptionPrivate(const QString &name)
Q_NEVER_INLINE QCommandLineOptionPrivate(const QStringList &names)
QStringList names
The list of names used for this option.
QStringList defaultValues
The list of default values used for this option.
QString description
The description used for this option.
The QSharedData class is a base class for shared data objects. \reentrant.
Definition: qshareddata.h:55
The QString class provides a Unicode character string.
Definition: qstring.h:388
bool isEmpty() const
Definition: qstring.h:1216
The QStringList class provides a list of strings.
constexpr T & operator()(T &v) const
Definition: hb-algs.hh:2
#define Q_UNLIKELY(x)
Flags
#define qWarning
Definition: qlogging.h:179
GLbitfield flags
GLuint name
const GLubyte * c
Definition: qopenglext.h:12701
GLuint GLuint * names
Definition: qopenglext.h:5654
QSharedPointer< T > other(t)
[5]
The QLatin1Char class provides an 8-bit ASCII/Latin-1 character.
Definition: qchar.h:53