~igotu2gpx/igotu2gpx/trunk

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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
/******************************************************************************
 * Copyright (C) 2010  Michael Hofmann <mh21@mh21.de>                         *
 *                                                                            *
 * This program 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.                                        *
 *                                                                            *
 * This program 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.                               *
 *                                                                            *
 * You should have received a copy of the GNU General Public License along    *
 * with this program; if not, write to the Free Software Foundation, Inc.,    *
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.                *
 ******************************************************************************/

#ifndef _RBA_SRC_LIB_PLUMBING_OPTIONCONTEXT_H_
#define _RBA_SRC_LIB_PLUMBING_OPTIONCONTEXT_H_

#include "global.h"

#include <QCoreApplication>
#include <QSet>
#include <QSharedPointer>
#include <QStringList>
#include <QVariant>

namespace igotu
{

class IGOTU_EXPORT AbstractOptionValue
{
public:
    virtual ~AbstractOptionValue()
    {
    }

    virtual void setValue(const QVariant &value) = 0;
};

template<typename T, typename U>
class MapOptionValue : public AbstractOptionValue
{
public:
    MapOptionValue(QMap<T, U> *data) :
        data(data)
    {
    }

    virtual void setValue(const QVariant &value)
    {
        const QString stringValue = value.toString();
        const T k(QVariant(stringValue.section
                    (QLatin1Char('='), 0, 0)).value<T>());
        const U v(QVariant(stringValue.section
                    (QLatin1Char('='), 1)).value<U>());
        data->insert(k, v);
    }

private:
    QMap<T, U> *data;
};

template<typename T, typename U>
MapOptionValue<T, U> mapOptionValue(QMap<T, U> *data)
{
    return MapOptionValue<T, U>(data);
}

template<typename T>
class ListOptionValue : public AbstractOptionValue
{
public:
    ListOptionValue(QList<T> *data) :
        data(data)
    {
    }

    virtual void setValue(const QVariant &value)
    {
        data->append(value.value<T>());
    }

private:
    QList<T> *data;
};

template<typename T>
ListOptionValue<T> listOptionValue(QList<T> *data)
{
    return ListOptionValue<T>(data);
}

template<typename T>
class IncrementOptionValue : public AbstractOptionValue
{
public:
    IncrementOptionValue(T *data) :
        data(data)
    {
    }

    virtual void setValue(const QVariant &value)
    {
        if (value.toBool())
            data += 1;
        else
            data -= 1;
    }

private:
    T *data;
};

template<typename T>
IncrementOptionValue<T> incrementOptionValue(T *data)
{
    return IncrementOptionValue<T>(data);
}

template<typename T>
class OptionValue : public AbstractOptionValue
{
public:
    OptionValue(T *data) :
        data(data)
    {
    }

    virtual void setValue(const QVariant &value)
    {
        *data = value.value<T>();
    }

private:
    T *data;
};

class IGOTU_EXPORT OptionEntry
{
public:
    enum Flag {
        // option doesn't appear in --help output
        HiddenOption  = 0x01,
        // option appears in the main section
        MainOption    = 0x02,
        // reversed non-argument option
        ReverseOption = 0x04
    };
    Q_DECLARE_FLAGS(Flags, Flag)

    enum ArgumentType {
        NoArgument,
        RequiredArgument
    };

    QString longName;
    QChar shortName;
    Flags flags;
    ArgumentType argumentType;
    QSharedPointer<AbstractOptionValue> value;
    QString description;
    QString argDescription;

public:
    OptionEntry()
    {
    }

    /** An OptionEntry defines a single option. To have an effect, they must be
     * added to an OptionGroup.
     *
     * @param longName The long name of an option can be used to specify it in
     * a commandline as --long_name. Every option must have a long name. To
     * resolve conflicts if multiple option groups contain the same long name,
     * it is also possible to specify the option as --groupname-long_name.
     * @param shortName If an option has a short name, it can be specified
     * -short_name in a commandline.
     * @param flags flags
     * @param arg type of the option
     * @param argValue object to store the data
     * @param description description for the option in --help output
     * @param argDescription placeholder to use for the extra argument parsed
     * by the option in --help output
     */
    template<class T>
    OptionEntry(const QString &longName, const QChar &shortName,
        Flags flags, ArgumentType argumentType, const T &value,
        const QString &description, const QString &argDescription = QString()) :
        longName(longName),
        shortName(shortName),
        flags(flags),
        argumentType(argumentType),
        value(new T(value)),
        description(description),
        argDescription(argDescription)
    {
    }

    template<typename T>
    OptionEntry(const QString &longName, const QChar &shortName,
        Flags flags, ArgumentType argumentType, T *value,
        const QString &description, const QString &argDescription = QString()) :
        longName(longName),
        shortName(shortName),
        flags(flags),
        argumentType(argumentType),
        value(new OptionValue<T>(value)),
        description(description),
        argDescription(argDescription)
    {
    }
};

class IGOTU_EXPORT OptionGroup
{
public:
    QString name;
    QString description;
    QString helpDescription;
    QString longDescription;
    QList<OptionEntry> entries;

    OptionGroup()
    {
    }

    /** Creates a new OptionGroup.
     *
     * @param name the name for the option group, this is used to provide help
     * for the options in this group with --help-name
     * @param description a description for this group to be shown in --help.
     * @param helpDescription a description for the --help-name option.
     * @param longDescription a string which is shown after all the other
     * options if these options are actually displayed.
     * @param entries the commandline options belonging to this group.
     */
    OptionGroup(const QString &name, const QString &description,
        const QString &helpDescription, const QString &longDescription,
        const QList<OptionEntry> &entries) :
        name(name),
        description(description),
        helpDescription(helpDescription),
        longDescription(longDescription),
        entries(entries)
    {
    }
};

class IGOTU_EXPORT OptionContext
{
    Q_DECLARE_TR_FUNCTIONS(OptionContext)
private:
    const static int totalLength = 80;
    const static int optionLength = 30;

    QStringList args;
    QString description;
    OptionGroup mainGroup;
    QList<OptionGroup> groups;

    bool helpEnabled;

public:
    OptionContext
        (const QStringList &args,
         const QString &description,
         const OptionGroup &mainGroup,
         const QList<OptionGroup> &groups = QList<OptionGroup>());
    void addGroup(const OptionGroup &group);
    void setMainGroup(const OptionGroup &group);
    QStringList parse() const;

    void setHelpEnabled(bool value);
    bool isHelpEnabled() const;

    // call exit() at the end, will not return
    void printAllHelp() const;
    void printShortHelp() const;
    void printSpecialHelp(const OptionGroup &group) const;

private:
    void printHelpUsage() const;
    void printHelpHeading(const QString &header) const;
    void printHelpHelp() const;
    void printHelpEntry(const OptionEntry &entry) const;
    void printHelpEntry(const QString &longName, const QChar &shortName,
            const QString &description, const QString &argDescription) const;
    QString printHelpGroup(const OptionGroup &group) const;
    void printHelpDescription(const QString &description) const;

    QString formatDescription(const QString &description,
            unsigned leftIndent) const;
};

} // namespace igotu

#endif