1 /****************************************************************************
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** All rights reserved.
5 ** Contact: Nokia Corporation (qt-info@nokia.com)
7 ** This file is part of the tools applications of the Qt Toolkit.
9 ** $QT_BEGIN_LICENSE:LGPL$
10 ** GNU Lesser General Public License Usage
11 ** This file may be used under the terms of the GNU Lesser General Public
12 ** License version 2.1 as published by the Free Software Foundation and
13 ** appearing in the file LICENSE.LGPL included in the packaging of this
14 ** file. Please review the following information to ensure the GNU Lesser
15 ** General Public License version 2.1 requirements will be met:
16 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
18 ** In addition, as a special exception, Nokia gives you certain additional
19 ** rights. These rights are described in the Nokia Qt LGPL Exception
20 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
22 ** GNU General Public License Usage
23 ** Alternatively, this file may be used under the terms of the GNU General
24 ** Public License version 3.0 as published by the Free Software Foundation
25 ** and appearing in the file LICENSE.GPL included in the packaging of this
26 ** file. Please review the following information to ensure the GNU General
27 ** Public License version 3.0 requirements will be met:
28 ** http://www.gnu.org/copyleft/gpl.html.
31 ** Alternatively, this file may be used in accordance with the terms and
32 ** conditions contained in a signed written agreement between you and Nokia.
40 ****************************************************************************/
45 #include "qapplicationargument_p.h"
50 \class QApplicationArgument
51 \brief The QApplicationArgument class is a declared of a command line
52 argument for QApplicationArgumentParser.
57 QApplicationArgument describes a valid command line argument,
58 by having a set of characteristics:
65 \o A name. For instance, "backend"
66 \o setName() and name()
68 \o A description, for human consumption.
69 \o setDescription() and description()
71 \o How many times the argument can occur. For instance, whether the argument is optional or not.
72 \o setMinimumOccurrence() & minimumOccurrence(), setMaximumOccurrence() & maximumOccurrence()
74 \o The type of the argument's value, if it has one. For instance, \c int or \c bool.
75 \o setType() and type()
77 \o The value that should be used in case the argument isn't used.
78 \o setDefaultValue() and defaultValue()
81 \sa QApplicationArgumentParser
84 class QApplicationArgumentPrivate
87 inline QApplicationArgumentPrivate(const QString &newName,
89 const int newType) : name(newName)
101 QVariant defaultValue;
108 Constructs an invalid QApplicationArgument instance.
110 QApplicationArgument::QApplicationArgument() : d_ptr(new QApplicationArgumentPrivate(QString(), QString(), QVariant::Invalid))
115 Constructs an QApplicationArgument instance that is a copy of \a other.
117 QApplicationArgument::QApplicationArgument(const QApplicationArgument &other) : d_ptr(new QApplicationArgumentPrivate(*other.d_ptr))
122 Destructs this QApplicationArgument instance.
124 QApplicationArgument::~QApplicationArgument()
130 Constructs an argument that has the name \a name and is of type
133 Calling this constructor is equivalent to calling setName() and setType()
134 on a default constructed QApplicationArgument instance.
136 \sa setName(), setType()
138 QApplicationArgument::QApplicationArgument(const QString &name,
139 const QString &description,
140 int aType) : d_ptr(new QApplicationArgumentPrivate(name, description, aType))
145 Assigns \a other to this QApplicationArgument instance.
147 QApplicationArgument &QApplicationArgument::operator=(const QApplicationArgument &other)
150 *d_ptr = *other.d_ptr;
155 // TODO is this really what we want?
157 Returns true if this QApplicationArgument instance is equal to \a other.
159 Equalness is defined to only consider name(). If for instance the type() differs
160 but the names are equal, this operator will return \c true.
162 bool QApplicationArgument::operator==(const QApplicationArgument &other) const
164 return name() == other.name();
168 \fn qHash(const QApplicationArgument &);
171 Returns a hash index of \a argument. This function is used when QApplicationArgument
174 The hash index is computed on name(). The other properties are ignored.
176 \relates QApplicationArgument
180 Sets this argument's name to \a newName. The name does not
181 include any dash, or other prefix that is used by the parser.
183 void QApplicationArgument::setName(const QString &newName)
185 d_ptr->name = newName;
189 Returns the name that this argument has.
193 QString QApplicationArgument::name() const
199 Sets the tupe to \a newType.
201 If \a newType is QVariant::Invalid, it signals that this
202 argument does not accept a value at all.
204 \a newType can be a QVariant::type() value, or QVariant::userType().
208 void QApplicationArgument::setType(int newType)
210 d_ptr->type = newType;
214 Returns the type that the value of this argument has. If it
215 is QVariant::Invalid, it means this argument cannot have a value
216 and is a switch only.
218 The type is by default QVariant::Invalid.
221 int QApplicationArgument::type() const
226 void QApplicationArgument::setDefaultValue(const QVariant &value)
228 d_ptr->defaultValue = value;
231 QVariant QApplicationArgument::defaultValue() const
233 return d_ptr->defaultValue;
237 Sets the minimum amount of times this argument can occur, to \a minimum.
238 For instance, if \a minimum is 2, the argument must be used at least two times.
240 If \a minimum is zero, it means the argument is optional.
242 \sa minimumOccurrence(), setMaximumOccurrence()
244 void QApplicationArgument::setMinimumOccurrence(int minimum)
246 Q_ASSERT_X(minimum >= 0, Q_FUNC_INFO,
247 "The minimum cannot be less than zero.");
248 d_ptr->minimum = minimum;
252 Returns the minimum amount of times an an argument must occur.
256 \sa setMinimumOccurrence(), maximumOccurrence()
258 int QApplicationArgument::minimumOccurrence() const
260 return d_ptr->minimum;
264 Sets the maximum occurrence to \a maximum.
266 If \a maximum is -1, it means the argument can appear an unlimited
267 amount of times. Setting it to zero or less than -1, yields
270 \sa maximumOccurrence(), setMinimumOccurrence()
272 void QApplicationArgument::setMaximumOccurrence(int maximum)
274 Q_ASSERT_X(maximum == -1 || maximum >= 1, Q_FUNC_INFO,
275 "The maximum can only be -1 or 1 or larger.");
276 d_ptr->maximum = maximum;
280 Returns the maximum amount of times this argument can occur. For instance,
281 if the maximum occurrence is 2, it would be an error if 3 were specified
284 If the maximum occurrence is -1, it signals the argument can appear an unlimited
289 \sa setMaximumOccurrence()
291 int QApplicationArgument::maximumOccurrence() const
293 return d_ptr->maximum;
297 Sets the description to \a newDescription. The description
298 should describe the argument in a sentence or two. It is used
299 when displaying a help message, if requested.
301 The description should be terminated as if it was a paragraph. This
302 typically means a period.
304 The description should be translated by wrapping the
305 string literal in a call to tr().
308 void QApplicationArgument::setDescription(const QString &newDescription)
310 d_ptr->description = newDescription;
314 Returns the description of this argument.
318 QString QApplicationArgument::description() const
320 return d_ptr->description;
325 \relates QApplicationArgument
327 Computes a hash key on \a argument's name and returns it.
329 uint qHash(const QApplicationArgument &argument)
331 return qHash(argument.name());
334 void QApplicationArgument::setNameless(bool value)
336 d_ptr->isNameless = value;
339 bool QApplicationArgument::isNameless() const
341 return d_ptr->isNameless;