| 1 | /**************************************************************************** |
| 2 | ** |
| 3 | ** Copyright (C) 2016 The Qt Company Ltd. |
| 4 | ** Contact: https://www.qt.io/licensing/ |
| 5 | ** |
| 6 | ** This file is part of the tools applications of the Qt Toolkit. |
| 7 | ** |
| 8 | ** $QT_BEGIN_LICENSE:GPL-EXCEPT$ |
| 9 | ** Commercial License Usage |
| 10 | ** Licensees holding valid commercial Qt licenses may use this file in |
| 11 | ** accordance with the commercial license agreement provided with the |
| 12 | ** Software or, alternatively, in accordance with the terms contained in |
| 13 | ** a written agreement between you and The Qt Company. For licensing terms |
| 14 | ** and conditions see https://www.qt.io/terms-conditions. For further |
| 15 | ** information use the contact form at https://www.qt.io/contact-us. |
| 16 | ** |
| 17 | ** GNU General Public License Usage |
| 18 | ** Alternatively, this file may be used under the terms of the GNU |
| 19 | ** General Public License version 3 as published by the Free Software |
| 20 | ** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT |
| 21 | ** included in the packaging of this file. Please review the following |
| 22 | ** information to ensure the GNU General Public License requirements will |
| 23 | ** be met: https://www.gnu.org/licenses/gpl-3.0.html. |
| 24 | ** |
| 25 | ** $QT_END_LICENSE$ |
| 26 | ** |
| 27 | ****************************************************************************/ |
| 28 | |
| 29 | #include <QHash> |
| 30 | #include <QString> |
| 31 | |
| 32 | #include "qapplicationargument_p.h" |
| 33 | |
| 34 | QT_BEGIN_NAMESPACE |
| 35 | |
| 36 | /*! |
| 37 | \class QApplicationArgument |
| 38 | \brief The QApplicationArgument class is a declared of a command line |
| 39 | argument for QApplicationArgumentParser. |
| 40 | \reentrant |
| 41 | \internal |
| 42 | \since 4.4 |
| 43 | |
| 44 | QApplicationArgument describes a valid command line argument, |
| 45 | by having a set of characteristics: |
| 46 | |
| 47 | \table |
| 48 | \header |
| 49 | \li Characteristic |
| 50 | \li Functions |
| 51 | \row |
| 52 | \li A name. For instance, "backend" |
| 53 | \li setName() and name() |
| 54 | \row |
| 55 | \li A description, for human consumption. |
| 56 | \li setDescription() and description() |
| 57 | \row |
| 58 | \li How many times the argument can occur. For instance, whether the argument is optional or not. |
| 59 | \li setMinimumOccurrence() & minimumOccurrence(), setMaximumOccurrence() & maximumOccurrence() |
| 60 | \row |
| 61 | \li The type of the argument's value, if it has one. For instance, \c int or \c bool. |
| 62 | \li setType() and type() |
| 63 | \row |
| 64 | \li The value that should be used in case the argument isn't used. |
| 65 | \li setDefaultValue() and defaultValue() |
| 66 | \endtable |
| 67 | |
| 68 | \sa QApplicationArgumentParser |
| 69 | */ |
| 70 | |
| 71 | class QApplicationArgumentPrivate |
| 72 | { |
| 73 | public: |
| 74 | inline QApplicationArgumentPrivate(const QString &newName, |
| 75 | const QString &desc, |
| 76 | const int newType) : name(newName) |
| 77 | , description(desc) |
| 78 | , type(newType) |
| 79 | , minimum(0) |
| 80 | , maximum(1) |
| 81 | , isNameless(false) |
| 82 | { |
| 83 | } |
| 84 | |
| 85 | QString name; |
| 86 | QString description; |
| 87 | int type; |
| 88 | QVariant defaultValue; |
| 89 | int minimum; |
| 90 | int maximum; |
| 91 | bool isNameless; |
| 92 | }; |
| 93 | |
| 94 | /*! |
| 95 | Constructs an invalid QApplicationArgument instance. |
| 96 | */ |
| 97 | QApplicationArgument::QApplicationArgument() : d_ptr(new QApplicationArgumentPrivate(QString(), QString(), QVariant::Invalid)) |
| 98 | { |
| 99 | } |
| 100 | |
| 101 | /*! |
| 102 | Constructs an QApplicationArgument instance that is a copy of \a other. |
| 103 | */ |
| 104 | QApplicationArgument::QApplicationArgument(const QApplicationArgument &other) : d_ptr(new QApplicationArgumentPrivate(*other.d_ptr)) |
| 105 | { |
| 106 | } |
| 107 | |
| 108 | /*! |
| 109 | Destructs this QApplicationArgument instance. |
| 110 | */ |
| 111 | QApplicationArgument::~QApplicationArgument() |
| 112 | { |
| 113 | delete d_ptr; |
| 114 | } |
| 115 | |
| 116 | /*! |
| 117 | Constructs an argument that has the name \a name and is of type |
| 118 | \a aType. |
| 119 | |
| 120 | Calling this constructor is equivalent to calling setName() and setType() |
| 121 | on a default constructed QApplicationArgument instance. |
| 122 | |
| 123 | \sa setName(), setType() |
| 124 | */ |
| 125 | QApplicationArgument::QApplicationArgument(const QString &name, |
| 126 | const QString &description, |
| 127 | int aType) : d_ptr(new QApplicationArgumentPrivate(name, description, aType)) |
| 128 | { |
| 129 | } |
| 130 | |
| 131 | /*! |
| 132 | Assigns \a other to this QApplicationArgument instance. |
| 133 | */ |
| 134 | QApplicationArgument &QApplicationArgument::operator=(const QApplicationArgument &other) |
| 135 | { |
| 136 | if(this != &other) |
| 137 | *d_ptr = *other.d_ptr; |
| 138 | |
| 139 | return *this; |
| 140 | } |
| 141 | |
| 142 | // TODO is this really what we want? |
| 143 | /*! |
| 144 | Returns true if this QApplicationArgument instance is equal to \a other. |
| 145 | |
| 146 | Equalness is defined to only consider name(). If for instance the type() differs |
| 147 | but the names are equal, this operator will return \c true. |
| 148 | */ |
| 149 | bool QApplicationArgument::operator==(const QApplicationArgument &other) const |
| 150 | { |
| 151 | return name() == other.name(); |
| 152 | } |
| 153 | |
| 154 | bool QApplicationArgument::operator<(const QApplicationArgument &other) const |
| 155 | { |
| 156 | return name() < other.name(); |
| 157 | } |
| 158 | |
| 159 | /*! |
| 160 | \fn qHash(const QApplicationArgument &); |
| 161 | \internal |
| 162 | |
| 163 | Returns a hash index of \a argument. This function is used when QApplicationArgument |
| 164 | is used with QHash. |
| 165 | |
| 166 | The hash index is computed on name(). The other properties are ignored. |
| 167 | |
| 168 | \relates QApplicationArgument |
| 169 | */ |
| 170 | |
| 171 | /*! |
| 172 | Sets this argument's name to \a newName. The name does not |
| 173 | include any dash, or other prefix that is used by the parser. |
| 174 | */ |
| 175 | void QApplicationArgument::setName(const QString &newName) |
| 176 | { |
| 177 | d_ptr->name = newName; |
| 178 | } |
| 179 | |
| 180 | /*! |
| 181 | Returns the name that this argument has. |
| 182 | |
| 183 | \sa setName() |
| 184 | */ |
| 185 | QString QApplicationArgument::name() const |
| 186 | { |
| 187 | return d_ptr->name; |
| 188 | } |
| 189 | |
| 190 | /*! |
| 191 | Sets the tupe to \a newType. |
| 192 | |
| 193 | If \a newType is QVariant::Invalid, it signals that this |
| 194 | argument does not accept a value at all. |
| 195 | |
| 196 | \a newType can be a QVariant::type() value, or QVariant::userType(). |
| 197 | |
| 198 | \sa type() |
| 199 | */ |
| 200 | void QApplicationArgument::setType(int newType) |
| 201 | { |
| 202 | d_ptr->type = newType; |
| 203 | } |
| 204 | |
| 205 | /*! |
| 206 | Returns the type that the value of this argument has. If it |
| 207 | is QVariant::Invalid, it means this argument cannot have a value |
| 208 | and is a switch only. |
| 209 | |
| 210 | The type is by default QVariant::Invalid. |
| 211 | \sa setType() |
| 212 | */ |
| 213 | int QApplicationArgument::type() const |
| 214 | { |
| 215 | return d_ptr->type; |
| 216 | } |
| 217 | |
| 218 | void QApplicationArgument::setDefaultValue(const QVariant &value) |
| 219 | { |
| 220 | d_ptr->defaultValue = value; |
| 221 | } |
| 222 | |
| 223 | QVariant QApplicationArgument::defaultValue() const |
| 224 | { |
| 225 | return d_ptr->defaultValue; |
| 226 | } |
| 227 | |
| 228 | /*! |
| 229 | Sets the minimum amount of times this argument can occur, to \a minimum. |
| 230 | For instance, if \a minimum is 2, the argument must be used at least two times. |
| 231 | |
| 232 | If \a minimum is zero, it means the argument is optional. |
| 233 | |
| 234 | \sa minimumOccurrence(), setMaximumOccurrence() |
| 235 | */ |
| 236 | void QApplicationArgument::setMinimumOccurrence(int minimum) |
| 237 | { |
| 238 | Q_ASSERT_X(minimum >= 0, Q_FUNC_INFO, |
| 239 | "The minimum cannot be less than zero." ); |
| 240 | d_ptr->minimum = minimum; |
| 241 | } |
| 242 | |
| 243 | /*! |
| 244 | Returns the minimum amount of times an an argument must occur. |
| 245 | |
| 246 | The default is 0. |
| 247 | |
| 248 | \sa setMinimumOccurrence(), maximumOccurrence() |
| 249 | */ |
| 250 | int QApplicationArgument::minimumOccurrence() const |
| 251 | { |
| 252 | return d_ptr->minimum; |
| 253 | } |
| 254 | |
| 255 | /*! |
| 256 | Sets the maximum occurrence to \a maximum. |
| 257 | |
| 258 | If \a maximum is -1, it means the argument can appear an unlimited |
| 259 | amount of times. Setting it to zero or less than -1, yields |
| 260 | undefined behavior. |
| 261 | |
| 262 | \sa maximumOccurrence(), setMinimumOccurrence() |
| 263 | */ |
| 264 | void QApplicationArgument::setMaximumOccurrence(int maximum) |
| 265 | { |
| 266 | Q_ASSERT_X(maximum == -1 || maximum >= 1, Q_FUNC_INFO, |
| 267 | "The maximum can only be -1 or 1 or larger." ); |
| 268 | d_ptr->maximum = maximum; |
| 269 | } |
| 270 | |
| 271 | /*! |
| 272 | Returns the maximum amount of times this argument can occur. For instance, |
| 273 | if the maximum occurrence is 2, it would be an error if 3 were specified |
| 274 | on the command line. |
| 275 | |
| 276 | If the maximum occurrence is -1, it signals the argument can appear an unlimited |
| 277 | amount of times. |
| 278 | |
| 279 | The default is 1. |
| 280 | |
| 281 | \sa setMaximumOccurrence() |
| 282 | */ |
| 283 | int QApplicationArgument::maximumOccurrence() const |
| 284 | { |
| 285 | return d_ptr->maximum; |
| 286 | } |
| 287 | |
| 288 | /*! |
| 289 | Sets the description to \a newDescription. The description |
| 290 | should describe the argument in a sentence or two. It is used |
| 291 | when displaying a help message, if requested. |
| 292 | |
| 293 | The description should be terminated as if it was a paragraph. This |
| 294 | typically means a period. |
| 295 | |
| 296 | The description should be translated by wrapping the |
| 297 | string literal in a call to tr(). |
| 298 | |
| 299 | */ |
| 300 | void QApplicationArgument::setDescription(const QString &newDescription) |
| 301 | { |
| 302 | d_ptr->description = newDescription; |
| 303 | } |
| 304 | |
| 305 | /*! |
| 306 | Returns the description of this argument. |
| 307 | |
| 308 | \sa setDescription() |
| 309 | */ |
| 310 | QString QApplicationArgument::description() const |
| 311 | { |
| 312 | return d_ptr->description; |
| 313 | } |
| 314 | |
| 315 | /*! |
| 316 | \internal |
| 317 | \relates QApplicationArgument |
| 318 | |
| 319 | Computes a hash key on \a argument's name and returns it. |
| 320 | */ |
| 321 | uint qHash(const QApplicationArgument &argument) |
| 322 | { |
| 323 | return qHash(key: argument.name()); |
| 324 | } |
| 325 | |
| 326 | void QApplicationArgument::setNameless(bool value) |
| 327 | { |
| 328 | d_ptr->isNameless = value; |
| 329 | } |
| 330 | |
| 331 | bool QApplicationArgument::isNameless() const |
| 332 | { |
| 333 | return d_ptr->isNameless; |
| 334 | } |
| 335 | |
| 336 | QT_END_NAMESPACE |
| 337 | |