|
@@ -0,0 +1,365 @@
|
|
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
|
+ inline QCommandLineOptionPrivate()
|
|
45
|
+ { }
|
|
46
|
+
|
|
47
|
+ void setNames(const QStringList &nameList);
|
|
48
|
+
|
|
49
|
+ //! The list of names used for this option.
|
|
50
|
+ QStringList names;
|
|
51
|
+
|
|
52
|
+ //! The documentation name for the value, if one is expected
|
|
53
|
+ //! Example: "-o <file>" means valueName == "file"
|
|
54
|
+ QString valueName;
|
|
55
|
+
|
|
56
|
+ //! The description used for this option.
|
|
57
|
+ QString description;
|
|
58
|
+
|
|
59
|
+ //! The list of default values used for this option.
|
|
60
|
+ QStringList defaultValues;
|
|
61
|
+};
|
|
62
|
+
|
|
63
|
+/*!
|
|
64
|
+ \since 5.2
|
|
65
|
+ \class QCommandLineOption
|
|
66
|
+ \brief The QCommandLineOption class defines a possible command-line option.
|
|
67
|
+ \inmodule QtCore
|
|
68
|
+ \ingroup shared
|
|
69
|
+ \ingroup tools
|
|
70
|
+
|
|
71
|
+ This class is used to describe an option on the command line. It allows
|
|
72
|
+ different ways of defining the same option with multiple aliases possible.
|
|
73
|
+ It is also used to describe how the option is used - it may be a flag (e.g. \c{-v})
|
|
74
|
+ or take a value (e.g. \c{-o file}).
|
|
75
|
+
|
|
76
|
+ Examples:
|
|
77
|
+ \snippet code/src_corelib_tools_qcommandlineoption.cpp 0
|
|
78
|
+
|
|
79
|
+ \sa QCommandLineParser
|
|
80
|
+*/
|
|
81
|
+
|
|
82
|
+/*!
|
|
83
|
+ \fn QCommandLineOption &QCommandLineOption::operator=(QCommandLineOption &&other)
|
|
84
|
+
|
|
85
|
+ Move-assigns \a other to this QCommandLineOption instance.
|
|
86
|
+
|
|
87
|
+ \since 5.2
|
|
88
|
+*/
|
|
89
|
+
|
|
90
|
+/*!
|
|
91
|
+ Constructs a command line option object with the name \a name.
|
|
92
|
+
|
|
93
|
+ The name can be either short or long. If the name is one character in
|
|
94
|
+ length, it is considered a short name. Option names must not be empty,
|
|
95
|
+ must not start with a dash or a slash character, must not contain a \c{=}
|
|
96
|
+ and cannot be repeated.
|
|
97
|
+
|
|
98
|
+ \sa setDescription(), setValueName(), setDefaultValues()
|
|
99
|
+*/
|
|
100
|
+QCommandLineOption::QCommandLineOption(const QString &name)
|
|
101
|
+ : d(new QCommandLineOptionPrivate)
|
|
102
|
+{
|
|
103
|
+ d->setNames(QStringList(name));
|
|
104
|
+}
|
|
105
|
+
|
|
106
|
+/*!
|
|
107
|
+ Constructs a command line option object with the names \a names.
|
|
108
|
+
|
|
109
|
+ This overload allows to set multiple names for the option, for instance
|
|
110
|
+ \c{o} and \c{output}.
|
|
111
|
+
|
|
112
|
+ The names can be either short or long. Any name in the list that is one
|
|
113
|
+ character in length is a short name. Option names must not be empty,
|
|
114
|
+ must not start with a dash or a slash character, must not contain a \c{=}
|
|
115
|
+ and cannot be repeated.
|
|
116
|
+
|
|
117
|
+ \sa setDescription(), setValueName(), setDefaultValues()
|
|
118
|
+*/
|
|
119
|
+QCommandLineOption::QCommandLineOption(const QStringList &names)
|
|
120
|
+ : d(new QCommandLineOptionPrivate)
|
|
121
|
+{
|
|
122
|
+ d->setNames(names);
|
|
123
|
+}
|
|
124
|
+
|
|
125
|
+/*!
|
|
126
|
+ Constructs a command line option object with the given arguments.
|
|
127
|
+
|
|
128
|
+ The name of the option is set to \a name.
|
|
129
|
+ The name can be either short or long. If the name is one character in
|
|
130
|
+ length, it is considered 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
|
+ The description is set to \a description. It is customary to add a "."
|
|
135
|
+ at the end of the description.
|
|
136
|
+
|
|
137
|
+ In addition, the \a valueName can be set if the option expects a value.
|
|
138
|
+ The default value for the option is set to \a defaultValue.
|
|
139
|
+
|
|
140
|
+ In Qt versions before 5.4, this constructor was \c explicit. In Qt 5.4
|
|
141
|
+ and later, it no longer is and can be used for C++11-style uniform
|
|
142
|
+ initialization:
|
|
143
|
+
|
|
144
|
+ \snippet code/src_corelib_tools_qcommandlineoption.cpp cxx11-init
|
|
145
|
+
|
|
146
|
+ \sa setDescription(), setValueName(), setDefaultValues()
|
|
147
|
+*/
|
|
148
|
+QCommandLineOption::QCommandLineOption(const QString &name, const QString &description,
|
|
149
|
+ const QString &valueName,
|
|
150
|
+ const QString &defaultValue)
|
|
151
|
+ : d(new QCommandLineOptionPrivate)
|
|
152
|
+{
|
|
153
|
+ d->setNames(QStringList(name));
|
|
154
|
+ setValueName(valueName);
|
|
155
|
+ setDescription(description);
|
|
156
|
+ setDefaultValue(defaultValue);
|
|
157
|
+}
|
|
158
|
+
|
|
159
|
+/*!
|
|
160
|
+ Constructs a command line option object with the given arguments.
|
|
161
|
+
|
|
162
|
+ This overload allows to set multiple names for the option, for instance
|
|
163
|
+ \c{o} and \c{output}.
|
|
164
|
+
|
|
165
|
+ The names of the option are set to \a names.
|
|
166
|
+ The names can be either short or long. Any name in the list that is one
|
|
167
|
+ character in length is a short name. Option names must not be empty,
|
|
168
|
+ must not start with a dash or a slash character, must not contain a \c{=}
|
|
169
|
+ and cannot be repeated.
|
|
170
|
+
|
|
171
|
+ The description is set to \a description. It is customary to add a "."
|
|
172
|
+ at the end of the description.
|
|
173
|
+
|
|
174
|
+ In addition, the \a valueName can be set if the option expects a value.
|
|
175
|
+ The default value for the option is set to \a defaultValue.
|
|
176
|
+
|
|
177
|
+ In Qt versions before 5.4, this constructor was \c explicit. In Qt 5.4
|
|
178
|
+ and later, it no longer is and can be used for C++11-style uniform
|
|
179
|
+ initialization:
|
|
180
|
+
|
|
181
|
+ \snippet code/src_corelib_tools_qcommandlineoption.cpp cxx11-init-list
|
|
182
|
+
|
|
183
|
+ \sa setDescription(), setValueName(), setDefaultValues()
|
|
184
|
+*/
|
|
185
|
+QCommandLineOption::QCommandLineOption(const QStringList &names, const QString &description,
|
|
186
|
+ const QString &valueName,
|
|
187
|
+ const QString &defaultValue)
|
|
188
|
+ : d(new QCommandLineOptionPrivate)
|
|
189
|
+{
|
|
190
|
+ d->setNames(names);
|
|
191
|
+ setValueName(valueName);
|
|
192
|
+ setDescription(description);
|
|
193
|
+ setDefaultValue(defaultValue);
|
|
194
|
+}
|
|
195
|
+
|
|
196
|
+/*!
|
|
197
|
+ Constructs a QCommandLineOption object that is a copy of the QCommandLineOption
|
|
198
|
+ object \a other.
|
|
199
|
+
|
|
200
|
+ \sa operator=()
|
|
201
|
+*/
|
|
202
|
+QCommandLineOption::QCommandLineOption(const QCommandLineOption &other)
|
|
203
|
+ : d(other.d)
|
|
204
|
+{
|
|
205
|
+}
|
|
206
|
+
|
|
207
|
+/*!
|
|
208
|
+ Destroys the command line option object.
|
|
209
|
+*/
|
|
210
|
+QCommandLineOption::~QCommandLineOption()
|
|
211
|
+{
|
|
212
|
+}
|
|
213
|
+
|
|
214
|
+/*!
|
|
215
|
+ Makes a copy of the \a other object and assigns it to this QCommandLineOption
|
|
216
|
+ object.
|
|
217
|
+*/
|
|
218
|
+QCommandLineOption &QCommandLineOption::operator=(const QCommandLineOption &other)
|
|
219
|
+{
|
|
220
|
+ d = other.d;
|
|
221
|
+ return *this;
|
|
222
|
+}
|
|
223
|
+
|
|
224
|
+/*!
|
|
225
|
+ \fn void QCommandLineOption::swap(QCommandLineOption &other)
|
|
226
|
+
|
|
227
|
+ Swaps option \a other with this option. This operation is very
|
|
228
|
+ fast and never fails.
|
|
229
|
+*/
|
|
230
|
+
|
|
231
|
+/*!
|
|
232
|
+ Returns the names set for this option.
|
|
233
|
+ */
|
|
234
|
+QStringList QCommandLineOption::names() const
|
|
235
|
+{
|
|
236
|
+ return d->names;
|
|
237
|
+}
|
|
238
|
+
|
|
239
|
+void QCommandLineOptionPrivate::setNames(const QStringList &nameList)
|
|
240
|
+{
|
|
241
|
+ QStringList newNames;
|
|
242
|
+ newNames.reserve(nameList.size());
|
|
243
|
+ if (nameList.isEmpty())
|
|
244
|
+ qWarning("QCommandLineOption: Options must have at least one name");
|
|
245
|
+ foreach (const QString &name, nameList) {
|
|
246
|
+ if (name.isEmpty()) {
|
|
247
|
+ qWarning("QCommandLineOption: Option names cannot be empty");
|
|
248
|
+ } else {
|
|
249
|
+ const QChar c = name.at(0);
|
|
250
|
+ if (c == QLatin1Char('-'))
|
|
251
|
+ qWarning("QCommandLineOption: Option names cannot start with a '-'");
|
|
252
|
+ else if (c == QLatin1Char('/'))
|
|
253
|
+ qWarning("QCommandLineOption: Option names cannot start with a '/'");
|
|
254
|
+ else if (name.contains(QLatin1Char('=')))
|
|
255
|
+ qWarning("QCommandLineOption: Option names cannot contain a '='");
|
|
256
|
+ else
|
|
257
|
+ newNames.append(name);
|
|
258
|
+ }
|
|
259
|
+ }
|
|
260
|
+ // commit
|
|
261
|
+ names.swap(newNames);
|
|
262
|
+}
|
|
263
|
+
|
|
264
|
+/*!
|
|
265
|
+ Sets the name of the expected value, for the documentation, to \a valueName.
|
|
266
|
+
|
|
267
|
+ Options without a value assigned have a boolean-like behavior:
|
|
268
|
+ either the user specifies --option or they don't.
|
|
269
|
+
|
|
270
|
+ Options with a value assigned need to set a name for the expected value,
|
|
271
|
+ for the documentation of the option in the help output. An option with names \c{o} and \c{output},
|
|
272
|
+ and a value name of \c{file} will appear as \c{-o, --output <file>}.
|
|
273
|
+
|
|
274
|
+ Call QCommandLineParser::value() if you expect the option to be present
|
|
275
|
+ only once, and QCommandLineParser::values() if you expect that option
|
|
276
|
+ to be present multiple times.
|
|
277
|
+
|
|
278
|
+ \sa valueName()
|
|
279
|
+ */
|
|
280
|
+void QCommandLineOption::setValueName(const QString &valueName)
|
|
281
|
+{
|
|
282
|
+ d->valueName = valueName;
|
|
283
|
+}
|
|
284
|
+
|
|
285
|
+/*!
|
|
286
|
+ Returns the name of the expected value.
|
|
287
|
+
|
|
288
|
+ If empty, the option doesn't take a value.
|
|
289
|
+
|
|
290
|
+ \sa setValueName()
|
|
291
|
+ */
|
|
292
|
+QString QCommandLineOption::valueName() const
|
|
293
|
+{
|
|
294
|
+ return d->valueName;
|
|
295
|
+}
|
|
296
|
+
|
|
297
|
+/*!
|
|
298
|
+ Sets the description used for this option to \a description.
|
|
299
|
+
|
|
300
|
+ It is customary to add a "." at the end of the description.
|
|
301
|
+
|
|
302
|
+ The description is used by QCommandLineParser::showHelp().
|
|
303
|
+
|
|
304
|
+ \sa description()
|
|
305
|
+ */
|
|
306
|
+void QCommandLineOption::setDescription(const QString &description)
|
|
307
|
+{
|
|
308
|
+ d->description = description;
|
|
309
|
+}
|
|
310
|
+
|
|
311
|
+/*!
|
|
312
|
+ Returns the description set for this option.
|
|
313
|
+
|
|
314
|
+ \sa setDescription()
|
|
315
|
+ */
|
|
316
|
+QString QCommandLineOption::description() const
|
|
317
|
+{
|
|
318
|
+ return d->description;
|
|
319
|
+}
|
|
320
|
+
|
|
321
|
+/*!
|
|
322
|
+ Sets the default value used for this option to \a defaultValue.
|
|
323
|
+
|
|
324
|
+ The default value is used if the user of the application does not specify
|
|
325
|
+ the option on the command line.
|
|
326
|
+
|
|
327
|
+ If \a defaultValue is empty, the option has no default values.
|
|
328
|
+
|
|
329
|
+ \sa defaultValues() setDefaultValues()
|
|
330
|
+ */
|
|
331
|
+void QCommandLineOption::setDefaultValue(const QString &defaultValue)
|
|
332
|
+{
|
|
333
|
+ QStringList newDefaultValues;
|
|
334
|
+ if (!defaultValue.isEmpty()) {
|
|
335
|
+ newDefaultValues.reserve(1);
|
|
336
|
+ newDefaultValues << defaultValue;
|
|
337
|
+ }
|
|
338
|
+ // commit:
|
|
339
|
+ d->defaultValues.swap(newDefaultValues);
|
|
340
|
+}
|
|
341
|
+
|
|
342
|
+/*!
|
|
343
|
+ Sets the list of default values used for this option to \a defaultValues.
|
|
344
|
+
|
|
345
|
+ The default values are used if the user of the application does not specify
|
|
346
|
+ the option on the command line.
|
|
347
|
+
|
|
348
|
+ \sa defaultValues() setDefaultValue()
|
|
349
|
+ */
|
|
350
|
+void QCommandLineOption::setDefaultValues(const QStringList &defaultValues)
|
|
351
|
+{
|
|
352
|
+ d->defaultValues = defaultValues;
|
|
353
|
+}
|
|
354
|
+
|
|
355
|
+/*!
|
|
356
|
+ Returns the default values set for this option.
|
|
357
|
+
|
|
358
|
+ \sa setDefaultValues()
|
|
359
|
+ */
|
|
360
|
+QStringList QCommandLineOption::defaultValues() const
|
|
361
|
+{
|
|
362
|
+ return d->defaultValues;
|
|
363
|
+}
|
|
364
|
+
|
|
365
|
+QT_END_NAMESPACE
|