File: option_parser.hpp

package info (click to toggle)
comparepdf 1.0.1-1.1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, sid
  • size: 192 kB
  • sloc: cpp: 1,035; makefile: 4
file content (243 lines) | stat: -rw-r--r-- 8,775 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
#ifndef OPTION_PARSER_HPP
#define OPTION_PARSER_HPP
/*
    Copyright (c) 2009-10 Qtrac Ltd. All rights reserved.

    This program or module is free software: you can redistribute it
    and/or modify it under the terms of the GNU General Public License
    as published by the Free Software Foundation, either version 3 of
    the License, or (at your option) any later version. It is provided
    for educational purposes and is distributed in the hope that it will
    be useful, but WITHOUT ANY WARRANTY; without even the implied
    warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
    the GNU General Public License for more details.
*/

#include <QCoreApplication>
#include <QSet>
#if QT_VERSION >= 0x040500
#include <QSharedPointer>
#else
#include <tr1/memory>
#endif
#include <QString>
#include <QStringList>
#include <QVariant>


namespace AQP {

class PrivateOption
{
public:
    explicit PrivateOption(const QString &shortName=QString(),
                           const QString &longName=QString())
        : m_shortName(shortName), m_longName(longName),
          m_required(false)
        {
            Q_ASSERT(!(longName.isEmpty() && shortName.isEmpty()));
        }
    virtual ~PrivateOption() {}

    bool required() const { return m_required; }
    virtual void setRequired(bool on=true) { m_required = on; }
    QString help() const { return m_help; }
    void setHelp(const QString &help) { m_help = help; }
    bool hasHelp() const { return !m_help.isEmpty(); }

    QString shortName() const { return m_shortName; }
    QString longName() const { return m_longName; }

    bool hasValue() const { return m_value.isValid(); }
    bool hasDefaultValue() const { return m_defaultValue.isValid(); }

    virtual QVariant::Type type() const = 0;

protected:
    QVariant m_defaultValue;
    QVariant m_value;

private:
    const QString m_shortName;
    const QString m_longName;
    bool m_required;
    QString m_help;
};


class IntegerOption : public PrivateOption
{
public:
    explicit IntegerOption(const QString &shortName=QString(),
                           const QString &longName=QString())
        : PrivateOption(shortName, longName) {}

    int defaultValue() const { return m_defaultValue.toInt(); }
    void setDefaultValue(int value) { m_defaultValue = value; }
    bool hasDefaultValue() const { return m_defaultValue.isValid(); }
    int minimum() const { return m_minimum.toInt(); }
    void setMinimum(int minimum) { m_minimum = minimum; }
    bool hasMinimum() const { return m_minimum.isValid(); }
    int maximum() const { return m_maximum.toInt(); }
    void setMaximum(int maximum) { m_maximum = maximum; }
    bool hasMaximum() const { return m_maximum.isValid(); }
    void setRange(int minimum, int maximum)
        { m_minimum = minimum; m_maximum = maximum; }
    QSet<int> acceptableValues() const { return m_acceptableValues; }
    void setAcceptableValues(const QList<int> &acceptableValues)
        { m_acceptableValues = QSet<int>::fromList(acceptableValues); }
    void setAcceptableValues(const QSet<int> &acceptableValues)
        { m_acceptableValues = acceptableValues; }
    int value() const { return m_value.toInt(); }
    void setValue(int value) { m_value = value; }
    bool hasAcceptableValues() const
        { return !m_acceptableValues.isEmpty(); }

    int integer() const { return m_value.toInt(); }
    QVariant::Type type() const { return QVariant::Int; }

private:
    QVariant m_minimum;
    QVariant m_maximum;
    QSet<int> m_acceptableValues;
};


class RealOption : public PrivateOption
{
public:
    explicit RealOption(const QString &shortName=QString(),
                        const QString &longName=QString())
        : PrivateOption(shortName, longName) {}

    double defaultValue() const { return m_defaultValue.toInt(); }
    void setDefaultValue(double value) { m_defaultValue = value; }
    bool hasDefaultValue() const { return m_defaultValue.isValid(); }
    double minimum() const { return m_minimum.toInt(); }
    void setMinimum(double minimum) { m_minimum = minimum; }
    bool hasMinimum() const { return m_minimum.isValid(); }
    double maximum() const { return m_maximum.toInt(); }
    void setMaximum(double maximum) { m_maximum = maximum; }
    bool hasMaximum() const { return m_maximum.isValid(); }
    void setRange(double minimum, double maximum)
        { m_minimum = minimum; m_maximum = maximum; }
    double value() const { return m_value.toDouble(); }
    void setValue(double value) { m_value = value; }

    double real() const { return m_value.toDouble(); }
    QVariant::Type type() const { return QVariant::Double; }

private:
    QVariant m_minimum;
    QVariant m_maximum;
};


class BooleanOption : public PrivateOption
{
public:
    explicit BooleanOption(const QString &shortName=QString(),
                           const QString &longName=QString())
        : PrivateOption(shortName, longName) {}

    bool defaultValue() const { return m_defaultValue.toInt(); }
    void setDefaultValue(bool value) { m_defaultValue = value; }
    bool hasDefaultValue() const { return m_defaultValue.isValid(); }
    bool value() const { return m_value.toBool(); }
    void setValue(bool value) { m_value = value; }

    bool boolean() const { return m_value.toBool(); }
    QVariant::Type type() const { return QVariant::Bool; }

    void setRequired(bool=true) { Q_ASSERT(false); }
};


class StringOption : public PrivateOption
{
public:
    explicit StringOption(const QString &shortName=QString(),
                          const QString &longName=QString())
        : PrivateOption(shortName, longName) {}

    QString defaultValue() const { return m_defaultValue.toString(); }
    void setDefaultValue(const QString &value) { m_defaultValue = value; }
    bool hasDefaultValue() const { return m_defaultValue.isValid(); }
    QSet<QString> acceptableValues() const { return m_acceptableValues; }
    void setAcceptableValues(const QStringList &acceptableValues)
        { m_acceptableValues = QSet<QString>::fromList(acceptableValues); }
    void setAcceptableValues(const QSet<QString> &acceptableValues)
        { m_acceptableValues = acceptableValues; }
    bool hasAcceptableValues() const
        { return !m_acceptableValues.isEmpty(); }
    QString value() const { return m_value.toString(); }
    void setValue(const QString &value) { m_value = value; }

    QString string() const { return m_value.toString(); }
    QVariant::Type type() const { return QVariant::String; }

private:
    QSet<QString> m_acceptableValues;
};


#if QT_VERSION >= 0x040500
typedef QSharedPointer<PrivateOption> OptionPtr;
typedef QSharedPointer<IntegerOption> IntegerOptionPtr;
typedef QSharedPointer<RealOption> RealOptionPtr;
typedef QSharedPointer<BooleanOption> BooleanOptionPtr;
typedef QSharedPointer<StringOption> StringOptionPtr;
#else
typedef std::tr1::shared_ptr<PrivateOption> OptionPtr;
typedef std::tr1::shared_ptr<IntegerOption> IntegerOptionPtr;
typedef std::tr1::shared_ptr<RealOption> RealOptionPtr;
typedef std::tr1::shared_ptr<BooleanOption> BooleanOptionPtr;
typedef std::tr1::shared_ptr<StringOption> StringOptionPtr;
#endif


class OptionParser
{
    Q_DECLARE_TR_FUNCTIONS(OptionParser)

public:
    explicit OptionParser(const QStringList &arguments=QStringList(),
                          const QString &help=QString(),
                          const QString &copyright=QString());

    bool parse();
    int usage(const QString &error=QString());

    int integer(const QString &name) const;
    double real(const QString &name) const;
    bool boolean(const QString &name) const;
    QString string(const QString &name) const;
    bool hasValue(const QString &name) const
        { return m_options[name]->hasValue(); }
    QStringList remainder() const { return m_remainder; }
    OptionPtr option(const QString &name)
        { return m_options[name]; }

    IntegerOptionPtr addIntegerOption(const QString &shortName=QString(),
                                      const QString &longName=QString());
    RealOptionPtr addRealOption(const QString &shortName=QString(),
                                const QString &longName=QString());
    BooleanOptionPtr addBooleanOption(const QString &shortName=QString(),
                                      const QString &longName=QString());
    StringOptionPtr addStringOption(const QString &shortName=QString(),
                                    const QString &longName=QString());

private:
    BooleanOptionPtr addHelpOption();
    void addOption(OptionPtr option);

    QStringList m_arguments;
    QString m_help;
    QString m_copyright;
    QStringList m_remainder;
    QMap<QString, OptionPtr> m_options;
};

} // namespace AQP

#endif // OPTION_PARSER_HPP