Browse Source

command line

old
Robin Thoni 9 years ago
parent
commit
fcfe275564

+ 4
- 5
server/mainclass.cpp View File

@@ -2,7 +2,7 @@
2 2
 #include <QStringList>
3 3
 #include <iostream>
4 4
 #include <sysexits.h>
5
-#include <QCommandLineParser>
5
+#include "qcommandlineparser.h"
6 6
 #include "mainclass.h"
7 7
 #include "gpiomanager.h"
8 8
 #include "randommanager.h"
@@ -10,6 +10,9 @@
10 10
 MainClass::MainClass(QObject *parent)
11 11
     : QObject(parent)
12 12
     , m_input(nullptr)
13
+    , m_device(Gpio)
14
+    , m_address(QHostAddress::Any)
15
+    , m_port(39415)
13 16
 {
14 17
 }
15 18
 
@@ -43,10 +46,6 @@ void MainClass::main()
43 46
 
44 47
 void MainClass::getOpts()
45 48
 {
46
-    m_device = Gpio;
47
-    m_address = QHostAddress::Any;
48
-    m_port = 39415;
49
-
50 49
     QCommandLineParser parser;
51 50
     parser.setApplicationDescription("Server for GPIO monitoring");
52 51
     parser.addHelpOption();

+ 365
- 0
server/qcommandlineoption.cpp View File

@@ -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

+ 83
- 0
server/qcommandlineoption.h View File

@@ -0,0 +1,83 @@
1
+/****************************************************************************
2
+**
3
+** Copyright (C) 2013 Laszlo Papp <lpapp@kde.org>
4
+** Contact: http://www.qt.io/licensing/
5
+**
6
+** This file is part of the QtCore module of the Qt Toolkit.
7
+**
8
+** $QT_BEGIN_LICENSE:LGPL21$
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 http://www.qt.io/terms-conditions. For further
15
+** information use the contact form at http://www.qt.io/contact-us.
16
+**
17
+** GNU Lesser General Public License Usage
18
+** Alternatively, this file may be used under the terms of the GNU Lesser
19
+** General Public License version 2.1 or version 3 as published by the Free
20
+** Software Foundation and appearing in the file LICENSE.LGPLv21 and
21
+** LICENSE.LGPLv3 included in the packaging of this file. Please review the
22
+** following information to ensure the GNU Lesser General Public License
23
+** requirements will be met: https://www.gnu.org/licenses/lgpl.html and
24
+** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
25
+**
26
+** As a special exception, The Qt Company gives you certain additional
27
+** rights. These rights are described in The Qt Company LGPL Exception
28
+** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
29
+**
30
+** $QT_END_LICENSE$
31
+**
32
+****************************************************************************/
33
+
34
+#ifndef QCOMMANDLINEOPTION_H
35
+#define QCOMMANDLINEOPTION_H
36
+
37
+#include <QtCore/qstringlist.h>
38
+#include <QtCore/qshareddata.h>
39
+
40
+class QCommandLineOptionPrivate;
41
+
42
+class Q_CORE_EXPORT QCommandLineOption
43
+{
44
+public:
45
+    explicit QCommandLineOption(const QString &name);
46
+    explicit QCommandLineOption(const QStringList &names);
47
+    /*implicit*/ QCommandLineOption(const QString &name, const QString &description,
48
+                                const QString &valueName = QString(),
49
+                                const QString &defaultValue = QString());
50
+    /*implicit*/ QCommandLineOption(const QStringList &names, const QString &description,
51
+                                const QString &valueName = QString(),
52
+                                const QString &defaultValue = QString());
53
+    QCommandLineOption(const QCommandLineOption &other);
54
+
55
+    ~QCommandLineOption();
56
+
57
+    QCommandLineOption &operator=(const QCommandLineOption &other);
58
+#ifdef Q_COMPILER_RVALUE_REFS
59
+    inline QCommandLineOption &operator=(QCommandLineOption &&other)
60
+    { qSwap(d, other.d); return *this; }
61
+#endif
62
+
63
+    inline void swap(QCommandLineOption &other)
64
+    { qSwap(d, other.d); }
65
+
66
+    QStringList names() const;
67
+
68
+    void setValueName(const QString &name);
69
+    QString valueName() const;
70
+
71
+    void setDescription(const QString &description);
72
+    QString description() const;
73
+
74
+    void setDefaultValue(const QString &defaultValue);
75
+    void setDefaultValues(const QStringList &defaultValues);
76
+    QStringList defaultValues() const;
77
+
78
+private:
79
+    QSharedDataPointer<QCommandLineOptionPrivate> d;
80
+};
81
+
82
+
83
+#endif // QCOMMANDLINEOPTION_H

+ 1078
- 0
server/qcommandlineparser.cpp
File diff suppressed because it is too large
View File


+ 96
- 0
server/qcommandlineparser.h View File

@@ -0,0 +1,96 @@
1
+/****************************************************************************
2
+**
3
+** Copyright (C) 2013 Laszlo Papp <lpapp@kde.org>
4
+** Contact: http://www.qt.io/licensing/
5
+**
6
+** This file is part of the QtCore module of the Qt Toolkit.
7
+**
8
+** $QT_BEGIN_LICENSE:LGPL21$
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 http://www.qt.io/terms-conditions. For further
15
+** information use the contact form at http://www.qt.io/contact-us.
16
+**
17
+** GNU Lesser General Public License Usage
18
+** Alternatively, this file may be used under the terms of the GNU Lesser
19
+** General Public License version 2.1 or version 3 as published by the Free
20
+** Software Foundation and appearing in the file LICENSE.LGPLv21 and
21
+** LICENSE.LGPLv3 included in the packaging of this file. Please review the
22
+** following information to ensure the GNU Lesser General Public License
23
+** requirements will be met: https://www.gnu.org/licenses/lgpl.html and
24
+** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
25
+**
26
+** As a special exception, The Qt Company gives you certain additional
27
+** rights. These rights are described in The Qt Company LGPL Exception
28
+** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
29
+**
30
+** $QT_END_LICENSE$
31
+**
32
+****************************************************************************/
33
+
34
+#ifndef QCOMMANDLINEPARSER_H
35
+#define QCOMMANDLINEPARSER_H
36
+
37
+#include <QtCore/qstringlist.h>
38
+
39
+#include <QtCore/qcoreapplication.h>
40
+#include "qcommandlineoption.h"
41
+
42
+class QCommandLineParserPrivate;
43
+class QCoreApplication;
44
+
45
+class Q_CORE_EXPORT QCommandLineParser
46
+{
47
+    Q_DECLARE_TR_FUNCTIONS(QCommandLineParser)
48
+public:
49
+    QCommandLineParser();
50
+    ~QCommandLineParser();
51
+
52
+    enum SingleDashWordOptionMode {
53
+        ParseAsCompactedShortOptions,
54
+        ParseAsLongOptions
55
+    };
56
+    void setSingleDashWordOptionMode(SingleDashWordOptionMode parsingMode);
57
+
58
+    bool addOption(const QCommandLineOption &commandLineOption);
59
+    bool addOptions(const QList<QCommandLineOption> &options);
60
+
61
+    QCommandLineOption addVersionOption();
62
+    QCommandLineOption addHelpOption();
63
+    void setApplicationDescription(const QString &description);
64
+    QString applicationDescription() const;
65
+    void addPositionalArgument(const QString &name, const QString &description, const QString &syntax = QString());
66
+    void clearPositionalArguments();
67
+
68
+    void process(const QStringList &arguments);
69
+    void process(const QCoreApplication &app);
70
+
71
+    bool parse(const QStringList &arguments);
72
+    QString errorText() const;
73
+
74
+    bool isSet(const QString &name) const;
75
+    QString value(const QString &name) const;
76
+    QStringList values(const QString &name) const;
77
+
78
+    bool isSet(const QCommandLineOption &option) const;
79
+    QString value(const QCommandLineOption &option) const;
80
+    QStringList values(const QCommandLineOption &option) const;
81
+
82
+    QStringList positionalArguments() const;
83
+    QStringList optionNames() const;
84
+    QStringList unknownOptionNames() const;
85
+
86
+    void showVersion();
87
+    void showHelp(int exitCode = 0);
88
+    QString helpText() const;
89
+
90
+private:
91
+    Q_DISABLE_COPY(QCommandLineParser)
92
+
93
+    QCommandLineParserPrivate * const d;
94
+};
95
+
96
+#endif // QCOMMANDLINEPARSER_H

+ 6
- 2
server/server.pro View File

@@ -24,7 +24,9 @@ SOURCES += main.cpp \
24 24
     gpiomanager.cpp \
25 25
     servermanager.cpp \
26 26
     inputbusiness.cpp \
27
-    randommanager.cpp
27
+    randommanager.cpp \
28
+    qcommandlineoption.cpp \
29
+    qcommandlineparser.cpp
28 30
 
29 31
 HEADERS += \
30 32
     mainclass.h \
@@ -32,4 +34,6 @@ HEADERS += \
32 34
     gpiomanager.h \
33 35
     servermanager.h \
34 36
     inputbusiness.h \
35
-    randommanager.h
37
+    randommanager.h \
38
+    qcommandlineoption.h \
39
+    qcommandlineparser.h

Loading…
Cancel
Save