Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Toggle main menu visibility
Loading...
Searching...
No Matches
qcommandlineoption.cpp
Go to the documentation of this file.
1
// Copyright (C) 2020 The Qt Company Ltd.
2
// Copyright (C) 2013 Laszlo Papp <lpapp@kde.org>
3
// Copyright (C) 2013 David Faure <faure@kde.org>
4
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
5
// Qt-Security score:critical reason:data-parser
6
7
#
include
"qcommandlineoption.h"
8
9
#
include
"qset.h"
10
11
QT_BEGIN_NAMESPACE
12
13
class
QCommandLineOptionPrivate
:
public
QSharedData
14
{
15
public
:
16
Q_NEVER_INLINE
17
explicit
QCommandLineOptionPrivate
(
const
QString
&
name
)
18
:
names
(
removeInvalidNames
(
QStringList
(
name
)))
19
{ }
20
21
Q_NEVER_INLINE
22
explicit
QCommandLineOptionPrivate
(
const
QStringList
&
names
)
23
:
names
(
removeInvalidNames
(
names
))
24
{ }
25
26
static
QStringList
removeInvalidNames
(QStringList nameList);
27
28
//! The list of names used for this option.
29
QStringList
names
;
30
31
//! The documentation name for the value, if one is expected
32
//! Example: "-o <file>" means valueName == "file"
33
QString
valueName
;
34
35
//! The description used for this option.
36
QString
description
;
37
38
//! The list of default values used for this option.
39
QStringList
defaultValues
;
40
41
QCommandLineOption
::
Flags
flags
;
42
};
43
44
/*!
45
\since 5.2
46
\class QCommandLineOption
47
\brief The QCommandLineOption class defines a possible command-line option.
48
\inmodule QtCore
49
\ingroup shared
50
\ingroup tools
51
52
This class is used to describe an option on the command line. It allows
53
different ways of defining the same option with multiple aliases possible.
54
It is also used to describe how the option is used - it may be a flag (e.g. \c{-v})
55
or take a value (e.g. \c{-o file}).
56
57
Examples:
58
\snippet code/src_corelib_tools_qcommandlineoption.cpp 0
59
60
\sa QCommandLineParser
61
*/
62
63
/*!
64
\fn QCommandLineOption &QCommandLineOption::operator=(QCommandLineOption &&other)
65
66
Move-assigns \a other to this QCommandLineOption instance.
67
68
\since 5.2
69
*/
70
71
/*!
72
Constructs a command line option object with the name \a name.
73
74
The name can be either short or long. If the name is one character in
75
length, it is considered a short name. Option names must not be empty,
76
must not start with a dash or a slash character, must not contain a \c{=}
77
and cannot be repeated.
78
79
\sa setDescription(), setValueName(), setDefaultValues()
80
*/
81
QCommandLineOption::QCommandLineOption(
const
QString &name)
82
: d(
new
QCommandLineOptionPrivate(name))
83
{
84
}
85
86
/*!
87
Constructs a command line option object with the names \a names.
88
89
This overload allows to set multiple names for the option, for instance
90
\c{o} and \c{output}.
91
92
The names can be either short or long. Any name in the list that is one
93
character in length is a short name. Option names must not be empty,
94
must not start with a dash or a slash character, must not contain a \c{=}
95
and cannot be repeated.
96
97
\sa setDescription(), setValueName(), setDefaultValues()
98
*/
99
QCommandLineOption::QCommandLineOption(
const
QStringList &names)
100
: d(
new
QCommandLineOptionPrivate(names))
101
{
102
}
103
104
/*!
105
Constructs a command line option object with the given arguments.
106
107
The name of the option is set to \a name.
108
The name can be either short or long. If the name is one character in
109
length, it is considered a short name. Option names must not be empty,
110
must not start with a dash or a slash character, must not contain a \c{=}
111
and cannot be repeated.
112
113
The description is set to \a description. It is customary to add a "."
114
at the end of the description.
115
116
In addition, the \a valueName needs to be set if the option expects a value.
117
The default value for the option is set to \a defaultValue.
118
119
In Qt versions before 5.4, this constructor was \c explicit. In Qt 5.4
120
and later, it no longer is and can be used for uniform initialization:
121
122
\snippet code/src_corelib_tools_qcommandlineoption.cpp cxx11-init
123
124
\sa setDescription(), setValueName(), setDefaultValues()
125
*/
126
QCommandLineOption::QCommandLineOption(
const
QString &name,
const
QString &description,
127
const
QString &valueName,
128
const
QString &defaultValue)
129
: d(
new
QCommandLineOptionPrivate(name))
130
{
131
setValueName(valueName);
132
setDescription(description);
133
setDefaultValue(defaultValue);
134
}
135
136
/*!
137
Constructs a command line option object with the given arguments.
138
139
This overload allows to set multiple names for the option, for instance
140
\c{o} and \c{output}.
141
142
The names of the option are set to \a names.
143
The names can be either short or long. Any name in the list that is one
144
character in length is a short name. Option names must not be empty,
145
must not start with a dash or a slash character, must not contain a \c{=}
146
and cannot be repeated.
147
148
The description is set to \a description. It is customary to add a "."
149
at the end of the description.
150
151
In addition, the \a valueName needs to be set if the option expects a value.
152
The default value for the option is set to \a defaultValue.
153
154
In Qt versions before 5.4, this constructor was \c explicit. In Qt 5.4
155
and later, it no longer is and can be used for uniform initialization:
156
157
\snippet code/src_corelib_tools_qcommandlineoption.cpp cxx11-init-list
158
159
\sa setDescription(), setValueName(), setDefaultValues()
160
*/
161
QCommandLineOption::QCommandLineOption(
const
QStringList &names,
const
QString &description,
162
const
QString &valueName,
163
const
QString &defaultValue)
164
: d(
new
QCommandLineOptionPrivate(names))
165
{
166
setValueName(valueName);
167
setDescription(description);
168
setDefaultValue(defaultValue);
169
}
170
171
/*!
172
Constructs a QCommandLineOption object that is a copy of the QCommandLineOption
173
object \a other.
174
175
\sa operator=()
176
*/
177
QCommandLineOption::QCommandLineOption(
const
QCommandLineOption &other)
178
: d(other.d)
179
{
180
}
181
182
/*!
183
Destroys the command line option object.
184
*/
185
QCommandLineOption::~QCommandLineOption()
186
{
187
}
188
189
/*!
190
Makes a copy of the \a other object and assigns it to this QCommandLineOption
191
object.
192
*/
193
QCommandLineOption &QCommandLineOption::operator=(
const
QCommandLineOption &other)
194
{
195
d = other.d;
196
return
*
this
;
197
}
198
199
/*!
200
\fn void QCommandLineOption::swap(QCommandLineOption &other)
201
\memberswap{option}
202
*/
203
204
/*!
205
Returns the names set for this option.
206
*/
207
QStringList QCommandLineOption::names()
const
208
{
209
return
d->names;
210
}
211
212
namespace
{
213
struct
IsInvalidName
214
{
215
typedef
bool
result_type;
216
typedef
QString argument_type;
217
218
Q_NEVER_INLINE
219
result_type operator()(
const
QString &name)
const
noexcept
220
{
221
if
(Q_UNLIKELY(name.isEmpty()))
222
return
warn(
"be empty"
);
223
224
const
QChar c = name.at(0);
225
if
(Q_UNLIKELY(c == u'-'))
226
return
warn(
"start with a '-'"
);
227
if
(Q_UNLIKELY(c == u'/'))
228
return
warn(
"start with a '/'"
);
229
if
(Q_UNLIKELY(name.contains(u'=')))
230
return
warn(
"contain a '='"
);
231
232
return
false
;
233
}
234
235
Q_NEVER_INLINE
236
static
bool
warn(
const
char
*what)
noexcept
237
{
238
qWarning(
"QCommandLineOption: Option names cannot %s"
, what);
239
return
true
;
240
}
241
};
242
}
// unnamed namespace
243
244
// static
245
QStringList
QCommandLineOptionPrivate
::removeInvalidNames(QStringList nameList)
246
{
247
if
(Q_UNLIKELY(nameList.isEmpty()))
248
qWarning(
"QCommandLineOption: Options must have at least one name"
);
249
else
250
nameList.removeIf(IsInvalidName());
251
return
nameList;
252
}
253
254
/*!
255
Sets the name of the expected value, for the documentation, to \a valueName.
256
257
Options without a value assigned have a boolean-like behavior:
258
either the user specifies --option or they don't.
259
260
Options with a value assigned need to set a name for the expected value,
261
for the documentation of the option in the help output. An option with names \c{o} and \c{output},
262
and a value name of \c{file} will appear as \c{-o, --output <file>}.
263
264
Call QCommandLineParser::value() if you expect the option to be present
265
only once, and QCommandLineParser::values() if you expect that option
266
to be present multiple times.
267
268
\sa valueName()
269
*/
270
void
QCommandLineOption::setValueName(
const
QString &valueName)
271
{
272
d->valueName = valueName;
273
}
274
275
/*!
276
Returns the name of the expected value.
277
278
If empty, the option doesn't take a value.
279
280
\sa setValueName()
281
*/
282
QString QCommandLineOption::valueName()
const
283
{
284
return
d->valueName;
285
}
286
287
/*!
288
Sets the description used for this option to \a description.
289
290
It is customary to add a "." at the end of the description.
291
292
The description is used by QCommandLineParser::showHelp().
293
294
\sa description()
295
*/
296
void
QCommandLineOption::setDescription(
const
QString &description)
297
{
298
d->description = description;
299
}
300
301
/*!
302
Returns the description set for this option.
303
304
\sa setDescription()
305
*/
306
QString QCommandLineOption::description()
const
307
{
308
return
d->description;
309
}
310
311
/*!
312
Sets the default value used for this option to \a defaultValue.
313
314
The default value is used if the user of the application does not specify
315
the option on the command line.
316
317
If \a defaultValue is empty, the option has no default values.
318
319
\sa defaultValues(), setDefaultValues()
320
*/
321
void
QCommandLineOption::setDefaultValue(
const
QString &defaultValue)
322
{
323
QStringList newDefaultValues;
324
if
(!defaultValue.isEmpty()) {
325
newDefaultValues.reserve(1);
326
newDefaultValues << defaultValue;
327
}
328
// commit:
329
d->defaultValues.swap(newDefaultValues);
330
}
331
332
/*!
333
Sets the list of default values used for this option to \a defaultValues.
334
335
The default values are used if the user of the application does not specify
336
the option on the command line.
337
338
\sa defaultValues(), setDefaultValue()
339
*/
340
void
QCommandLineOption::setDefaultValues(
const
QStringList &defaultValues)
341
{
342
d->defaultValues = defaultValues;
343
}
344
345
/*!
346
Returns the default values set for this option.
347
348
\sa setDefaultValues()
349
*/
350
QStringList QCommandLineOption::defaultValues()
const
351
{
352
return
d->defaultValues;
353
}
354
355
/*!
356
Returns a set of flags that affect this command-line option.
357
358
\since 5.8
359
\sa setFlags(), QCommandLineOption::Flags
360
*/
361
QCommandLineOption::Flags QCommandLineOption::flags()
const
362
{
363
return
d->flags;
364
}
365
366
/*!
367
Set the set of flags that affect this command-line option to \a flags.
368
369
\since 5.8
370
\sa flags(), QCommandLineOption::Flags
371
*/
372
void
QCommandLineOption::setFlags(Flags flags)
373
{
374
d->flags = flags;
375
}
376
377
/*!
378
\enum QCommandLineOption::Flag
379
380
\value HiddenFromHelp Hide this option in the user-visible help output. All
381
options are visible by default. Setting this flag for a particular
382
option makes it internal, i.e. not listed in the help output.
383
384
\value ShortOptionStyle The option will always be understood as a short
385
option, regardless of what was set by
386
QCommandLineParser::setSingleDashWordOptionMode.
387
This allows flags such as \c{-DDEFINE=VALUE} or \c{-I/include/path} to be
388
interpreted as short flags even when the parser is in
389
QCommandLineParser::ParseAsLongOptions mode.
390
391
\value [since 6.9] IgnoreOptionsAfter No options beyond this one will be parsed. Useful
392
for cases where you need to send extra command line arguments to a secondary
393
application. If a value is provided for this option, it will be ignored.
394
395
\sa QCommandLineOption::setFlags(), QCommandLineOption::flags()
396
*/
397
398
QT_END_NAMESPACE
QCommandLineOptionPrivate
Definition
qcommandlineoption.cpp:14
QCommandLineOptionPrivate::removeInvalidNames
static QStringList removeInvalidNames(QStringList nameList)
Definition
qcommandlineoption.cpp:245
QCommandLineOptionPrivate::names
QStringList names
The list of names used for this option.
Definition
qcommandlineoption.cpp:29
QCommandLineOptionPrivate::valueName
QString valueName
The documentation name for the value, if one is expected Example: "-o <file>" means valueName == "fil...
Definition
qcommandlineoption.cpp:33
QCommandLineOptionPrivate::defaultValues
QStringList defaultValues
The list of default values used for this option.
Definition
qcommandlineoption.cpp:39
QCommandLineOptionPrivate::description
QString description
The description used for this option.
Definition
qcommandlineoption.cpp:36
QT_BEGIN_NAMESPACE
Combined button and popup list for selecting options.
Definition
qsequentialanimationgroup.cpp:47
qtbase
src
corelib
tools
qcommandlineoption.cpp
Generated on
for Qt by
1.18.0