-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcliparser.cc
363 lines (294 loc) · 10.7 KB
/
cliparser.cc
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
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
/*!
* \file
* C++ implementation for the \c CliParser class.
*
* \author Stephen Bryant <[email protected]>
* \date $Date: 2010-12-14 16:46:09 +0100 (Tue, 14 Dec 2010) $
*/
#include "cliparser.h"
#include <QCoreApplication>
CliParser::CliParser()
: m_bStopParsingAtDoubleDash( true ),
m_bRemoveUnknownLongNames( true )
{
}
bool CliParser::addOption( const CliOption & option )
{
bool bOptionAdded = false;
if ( ( m_optionList.size() < m_maxOptionCount ) &&
! ( option.m_longnameSet.isEmpty() &&
option.m_shortnameSet.isEmpty() ) )
{
// Whether a duplicate name has been found.
bool bDuplicateName = false;
QSet< QString >::const_iterator longIter;
// Check for duplicate names.
for ( longIter = option.m_longnameSet.constBegin();
longIter != option.m_longnameSet.constEnd();
longIter ++ )
{
if ( m_nameHash.contains( *longIter ) )
{
bDuplicateName = true;
break;
}
}
if ( ! bDuplicateName )
{
QSet< QChar >::const_iterator shortIter;
// Check for duplicate names.
for ( shortIter = option.m_shortnameSet.constBegin();
shortIter != option.m_shortnameSet.constEnd();
shortIter ++ )
{
if ( m_nameHash.contains( *shortIter ) )
{
bDuplicateName = true;
break;
}
}
if ( ! bDuplicateName )
{
// Add it
try
{
m_optionList.append( option );
const quint16 offset = (quint16) ( m_optionList.size() - 1 );
for ( longIter = option.m_longnameSet.constBegin();
longIter != option.m_longnameSet.constEnd();
longIter ++ )
{
m_nameHash.insert( *longIter, offset );
}
for ( shortIter = option.m_shortnameSet.constBegin();
shortIter != option.m_shortnameSet.constEnd();
shortIter ++ )
{
m_nameHash.insert( *shortIter, offset );
}
bOptionAdded = true;
}
catch ( std::bad_alloc & )
{
}
} // if no duplicate name found
} // if no duplicate name found
} // if not too many options and option has a name
return bOptionAdded;
}
bool CliParser::parse()
{
bool bParseOk = false;
QCoreApplication *pApp = QCoreApplication::instance();
if ( NULL != pApp )
{
QStringList args = pApp->arguments();
if ( ! args.isEmpty() )
{
args.removeFirst();
bParseOk = parse( args );
}
}
return bParseOk;
} // CliParser::parse()
bool CliParser::parse( const QStringList & arguments )
{
bool bParseOk = false;
const int argumentsSize = arguments.size();
int argumentsOffset;
int argSize;
int assignOffset;
int i;
QString name;
bool bParsingStopped = false;
NameHash_t::const_iterator nameIter;
NameHash_t::mapped_type optionOffset;
static const QStringList emptyList;
static const QString emptyString( QLatin1String( "" ) );
static const QLatin1Char dashChar( '-' );
static const QLatin1Char assignChar( '=' );
m_leftoverArgs.clear();
m_optionNames.clear();
m_unknownOptionNames.clear();
m_optionArgsList.clear();
try
{
// Prepare an empty list of arguments for each option - as nothing
// has been found yet.
for ( i = m_optionList.size(); i > 0; i -- )
{
m_optionArgsList.append( emptyList );
}
for ( argumentsOffset = 0;
argumentsOffset < argumentsSize;
argumentsOffset ++ )
{
const QString & arg = arguments.at( argumentsOffset );
argSize = arg.size();
if ( bParsingStopped ||
//arg.isEmpty() ||
( 1 >= argSize ) ||
( arg.at( 0 ) != dashChar ) )
{
// Not an option
m_leftoverArgs.append( arg );
}
else if ( arg.at( 1 ) != dashChar )
{
// Short option
for ( i = 1; i < argSize; i ++ )
{
// Look for the short option's name (a single char)
nameIter = m_nameHash.find( arg.at( i ) );
if ( m_nameHash.constEnd() == nameIter )
{
// Not found
m_unknownOptionNames.append( arg.at( i ) );
}
else
{
// Found!
m_optionNames.append( nameIter.key() );
optionOffset = *nameIter;
if ( ! m_optionList.at( optionOffset ).m_bHasArg )
{
// This option has no argument
m_optionArgsList[ optionOffset ].append( emptyString );
}
else
{
// This option has an argument
if ( argSize - 1 == i )
{
// No more string left - use next arg.
argumentsOffset ++;
if ( argumentsOffset < argumentsSize )
{
m_optionArgsList[ optionOffset ].append( arguments.at( argumentsOffset ) );
}
}
else
{
// Use rest of string.
m_optionArgsList[ optionOffset ].append( arg.mid( i + 1 ) );
}
// Don't process the rest of this string.
break;
} // if option takes an argument
} // if short option name found
} // for loop through short option chars
} // if char at pos 1 is not '-' (ie: it's a short option)
else
{
// Long option
if ( 2 == argSize )
{
// Double dash found
if ( m_bStopParsingAtDoubleDash )
{
bParsingStopped = true;
}
else
{
m_leftoverArgs.append( arg );
}
}
else
{
assignOffset = arg.indexOf( assignChar, 2 );
name = arg.mid( 2, ( -1 == assignOffset ? argSize : assignOffset ) - 2 );
// Look for the long option's name
nameIter = m_nameHash.find( name );
if ( m_nameHash.constEnd() == nameIter )
{
// Not found
m_unknownOptionNames.append( name );
if ( !m_bRemoveUnknownLongNames )
{
m_leftoverArgs.append( arg );
}
}
else
{
// Found!
m_optionNames.append( nameIter.key() );
optionOffset = *nameIter;
if ( ! m_optionList.at( optionOffset ).m_bHasArg )
{
// This option has no argument
m_optionArgsList[ optionOffset ].append( emptyString );
}
else
{
// This option has an argument
if ( -1 == assignOffset )
{
// No assignment character present - use next arg.
argumentsOffset ++;
if ( argumentsOffset < argumentsSize )
{
m_optionArgsList[ optionOffset ].append( arguments.at( argumentsOffset ) );
}
}
else
{
// Assignment char was present
if ( argSize - 1 == assignOffset )
{
// No more chars after assignment char.
m_optionArgsList[ optionOffset ].append( emptyString );
}
else
{
// Use rest of string after assignment char.
m_optionArgsList[ optionOffset ].append( arg.mid( assignOffset + 1 ) );
}
} // if assignment char present
} // if option takes an argument
} // if long option name found
} // if not "--"
} // if it's a long option
}
bParseOk = true;
}
catch ( std::bad_alloc & )
{
}
return bParseOk;
} // CliParser::parse()
QString CliParser::getArgument( const QString & name ) const
{
QString arg; // start of with a null string
QStringList argList = getArgumentList( name );
if ( ! argList.isEmpty() )
{
arg = argList.last();
}
return arg;
} // CliParser::getArgument()
QStringList CliParser::getArgumentList( const QString & name ) const
{
QStringList argList;
const NameHash_t::const_iterator nameIter = m_nameHash.find( name );
if ( m_nameHash.constEnd() != nameIter )
{
const NameHash_t::mapped_type optionOffset = *nameIter;
if ( m_optionArgsList.size() > optionOffset )
{
argList = m_optionArgsList.at( optionOffset );
}
}
return argList;
} // CliParser::getArgumentList()
QStringList CliParser::getLeftoverArgs() const
{
return m_leftoverArgs;
}
QStringList CliParser::getOptionNames() const
{
return m_optionNames;
}
QStringList CliParser::getUnknownOptionNames() const
{
return m_unknownOptionNames;
}