File: CommandLineParser.h

package info (click to toggle)
qtop 2.3.3-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 3,840 kB
  • ctags: 5,775
  • sloc: cpp: 38,795; makefile: 9
file content (330 lines) | stat: -rw-r--r-- 8,010 bytes parent folder | download
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
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
#ifndef CommandLineParser_h
#define CommandLineParser_h

/******************************************************************************
*
* Copyright (C) 2002 Hugo PEREIRA <mailto: hugo.pereira@free.fr>
*
* This 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 2 of the License, or (at your option) any later
* version.
*
* This software 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, see <http://www.gnu.org/licenses/>.
*
*******************************************************************************/

#include "CommandLineArguments.h"
#include "Counter.h"
#include "Debug.h"

#include <QStringList>
#include <QHash>
#include <QMap>

//* parse command line argument.
class CommandLineParser: public Counter
{

    public:

    //* constructor
    CommandLineParser( void );

    //*@name default group names
    //@{
    static const QString applicationGroupName;
    static const QString serverGroupName;
    static const QString qtGroupName;
    //@}

    //* used to register options/flags
    class Tag: public Counter
    {

        public:

        //* constructor
        explicit Tag( QString longName, QString shortName = QString() ):
            Counter( "CommandLineParser::Tag" ),
            longName_( longName ),
            shortName_( shortName )
        {}

        //* equal to operator
        bool operator == (const Tag& other ) const
        {
            if( shortName_.isNull() ) return longName_ == other.longName_ || longName_ == other.shortName_;
            else if( other.shortName_.isNull() ) return longName_ == other.longName_ || shortName_ == other.longName_;
            else return longName_ == other.longName_ && shortName_ == other.shortName_;
        }

        //* less than operator
        bool operator < (const Tag& other ) const
        {
            if( shortName_.isNull() )
            {
                if( other.shortName_.isNull() ) return longName_ < other.longName_;
                else return false;
            } else {
                if( other.shortName_.isNull() ) return true;
                else if( shortName_ != other.shortName_ ) return shortName_ < other.shortName_;
                else return  longName_ < other.longName_;
            }
        }

        //*@name accessors
        //@{

        //* long name
        const QString& longName( void ) const
        { return longName_; }

        //* short name
        const QString& shortName( void ) const
        { return shortName_; }

        //* convert to string
        QString toString( void ) const
        { return shortName_.isNull() ? longName_:(shortName_+", "+longName_); }

        //* size
        int size( void ) const
        { return toString().size(); }

        using List = QList<Tag>;

        private:

        QString longName_;
        QString shortName_;

    };

    //* accessors
    //@{

    //* print help
    void usage( void ) const;

    //* return 'rectified' arguments
    CommandLineArguments arguments( void ) const;

    //* application name
    QString applicationName( void ) const
    { return applicationName_; }

    //* return true if flag is on
    bool hasFlag( const QString& ) const;

    //* return true if option is set
    bool hasOption( const QString& ) const;

    //* return option associated to tag
    QString option( const QString& ) const;

    //* orphans
    /** list of command line arguments located at the end of the list and that do not match any option */
    const QStringList& orphans( void ) const
    { return orphans_; }

    //@}

    //* modifiers
    //@{

    //* register group
    void setGroup( QString groupName )
    {
        currentGroup_ = groupName;
        if( !groupNames_.contains( currentGroup_ ) )
        { groupNames_.append( currentGroup_ ); }
    }

    //* register option
    void registerOption( const QString& tag, const QString& type, QString helpText )
    { registerOption( Tag( tag ), type, helpText ); }

    //* register option
    void registerOption( const Tag&, const QString& type, const QString& helpText );

    //* register flag
    void registerFlag( const QString& tag, const QString& helpText )
    { registerFlag( Tag( tag ), helpText ); }

    //* register flag
    void registerFlag( const Tag&, const QString& );

    //* parse
    CommandLineParser& parse( const CommandLineArguments&, bool ignoreWarnings = true );

    //* clear
    void clear( void );

    //* orphans
    /** list of command line arguments located at the end of the list and that do not match any option */
    QStringList& orphans( void )
    { return orphans_; }

    //@}

    private:

    //* discard orphans
    void _discardOrphans( bool ignoreWarnings );

    //* returns true if string is a tag
    bool _isTag( const QString& ) const;

    //* flag class
    class Flag: public Counter
    {

        public:

        //* constructor
        Flag( QString helpText = QString() ):
            Counter( "CommandLineParser::Flag" ),
            helpText_( helpText )
        {}

        //* help text
        QString helpText_;

        //* true if set
        bool set_ = false;

    };

    //* option class
    class Option: public Flag
    {

        public:

        //* constructor
        Option( QString type = QString(), QString helpText = QString() ):
            Flag( helpText ),
            type_( type )
        {}

        //* type
        QString type_;

        //* value
        QString value_;

    };

    //* used to select tag of maximum length
    class MinLengthFTor
    {

        public:

        //* predicate
        bool operator () ( const Tag& first, const Tag& second )
        { return first.size() < second.size(); }

    };

    //* used to select tag of maximum length
    class MinTypeLengthFTor
    {

        public:

        //* predicate
        bool operator () ( const Option& first, const Option& second )
        { return first.type_.size() < second.type_.size(); }

    };

    //* used to find matching flag
    class SameTagFTor
    {

        public:

        //* constructor
        SameTagFTor( const QString& tag ):
            tag_( tag )
            {}

        //* predicate
        bool operator() ( const Tag& tag ) const
        { return tag.shortName() == tag_ || tag.longName() == tag_; }

        private:

        //* tag
        QString tag_;

    };

    class Group
    {

        public:

        //* clear
        void clear( void );

        using Map = QHash< QString, Group>;

        //* flags
        using FlagMap = QMap<Tag, Flag>;

        //* flags
        FlagMap flags_;

        //* options
        using OptionMap = QMap<Tag, Option>;

        //* options
        OptionMap options_;

    };

    //* return all flags
    Group::FlagMap _allFlags( void ) const;

    //* return all options
    Group::OptionMap _allOptions( void ) const;

    //* find tag in list of flags
    Group::FlagMap::iterator _findTag( Group::FlagMap&, const QString& ) const;

    //* find tag in list of flags
    Group::FlagMap::const_iterator _findTag( const Group::FlagMap&, const QString& ) const;

    //* find tag in list of options
    Group::OptionMap::iterator _findTag( Group::OptionMap&, const QString& ) const;

    //* find tag in list of options
    Group::OptionMap::const_iterator _findTag( const Group::OptionMap&, const QString& ) const;

    //* group list
    Group::Map groups_;

    //* group list
    QStringList groupNames_;

    //* current group
    QString currentGroup_;

    //* application name
    QString applicationName_;

    //* orphan arguments
    QStringList orphans_;

};

#endif