forked from VSoftTechnologies/DUnitX
-
Notifications
You must be signed in to change notification settings - Fork 0
/
DUnitX.CommandLine.Parser.pas
354 lines (317 loc) · 9.45 KB
/
DUnitX.CommandLine.Parser.pas
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
{***************************************************************************}
{ }
{ DUnitX }
{ }
{ Copyright (C) 2015 Vincent Parrett & Contributors }
{ }
{ http://www.finalbuilder.com }
{ }
{ }
{***************************************************************************}
{ }
{ Licensed under the Apache License, Version 2.0 (the "License"); }
{ you may not use this file except in compliance with the License. }
{ You may obtain a copy of the License at }
{ }
{ http://www.apache.org/licenses/LICENSE-2.0 }
{ }
{ Unless required by applicable law or agreed to in writing, software }
{ distributed under the License is distributed on an "AS IS" BASIS, }
{ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. }
{ See the License for the specific language governing permissions and }
{ limitations under the License. }
{ }
{***************************************************************************}
unit DUnitX.CommandLine.Parser;
interface
{$I DUnitX.inc}
uses
{$IFDEF USE_NS}
System.Classes,
{$ELSE}
Classes,
{$ENDIF}
DUnitX.CommandLine.Options,
DUnitX.CommandLine.OptionDef;
type
IParseResultAddError = interface
['{9EADABED-511B-4095-9ACA-A5E431AB653D}']
procedure AddError(const value : string);
end;
TCommandLineParseResult = class(TInterfacedObject,ICommandLineParseResult,IParseResultAddError)
private
FErrors : TStringList;
protected
function GetErrorText: string;
function GetHasErrors: Boolean;
procedure AddError(const value: string);
public
constructor Create;
destructor Destroy;override;
end;
TCommandLineParser = class(TInterfacedObject,ICommandLineParser)
private
FUnamedIndex : integer;
protected
procedure InternalValidate(const parseErrors : IParseResultAddError);
procedure InternalParseFile(const fileName : string; const parseErrors : IParseResultAddError);
procedure InternalParse(const values : TStrings; const parseErrors : IParseResultAddError);
function Parse: ICommandLineParseResult;overload;
function Parse(const values : TStrings) : ICommandLineParseResult;overload;
public
constructor Create;
destructor Destroy;override;
end;
implementation
uses
DUnitX.ResStrs,
{$IFDEF USE_NS}
System.Generics.Collections,
System.StrUtils,
System.SysUtils;
{$ELSE}
Generics.Collections,
StrUtils,
SysUtils;
{$ENDIF}
procedure StripQuotes(var value : string);
var
l : integer;
begin
{$IFDEF NEXTGEN}
l := value.Length - 1;
if l < 1 then
exit;
if CharInSet(value.Chars[0],['''','"']) and CharInSet(value.Chars[l],['''','"']) then
begin
value := value.Remove(l, 1);
value := value.Remove(0, 1);
end;
{$ELSE}
l := Length(value);
if l < 2 then
exit;
if CharInSet(value[1],['''','"']) and CharInSet(value[l],['''','"']) then
begin
Delete(value,l,1);
Delete(value,1,1);
end;
{$ENDIF}
end;
{ TCommandLineParser }
constructor TCommandLineParser.Create;
begin
FUnamedIndex := 0;
end;
destructor TCommandLineParser.Destroy;
begin
inherited;
end;
procedure TCommandLineParser.InternalParse(const values: TStrings; const parseErrors: IParseResultAddError);
var
i : integer;
j : integer;
value : string;
key : string;
option : IOptionDefintion;
bTryValue : boolean;
bUseKey : boolean;
begin
for i := 0 to values.Count -1 do
begin
j := 0;
option := nil;
bTryValue := true;
bUseKey := false;
value := values.Strings[i];
{$IFDEF NEXTGEN}
if value = string.Empty then
continue;
if value.StartsWith('--') then
value := value.Remove(0, 2)
else if value.StartsWith('-') then
value := value.Remove(0, 1)
else if value.StartsWith('/') then
value := value.Remove(0, 1)
else if value.StartsWith('@') then
value := value.Remove(0, 1)
{$ELSE}
if value = '' then
continue;
if StartsStr('--',value) then
Delete(value,1,2)
else if StartsStr('-',value) then
Delete(value,1,1)
else if StartsStr('/',value) then
Delete(value,1,1)
else if StartsStr('@',value) then
Delete(value,1,1)
{$ENDIF}
else if FUnamedIndex < TOptionsRegistry.RegisteredUnamedOptions.Count then
begin
option := TOptionsRegistry.RegisteredUnamedOptions.Items[FUnamedIndex];
Inc(FUnamedIndex);
bTryValue := false;
bUseKey := True;
end
else
begin
//don't recognise the start so report it and continue.
parseErrors.AddError(SUnknownOptionStart + values.Strings[i]);
continue;
end;
{$IFDEF NEXTGEN}
if bTryValue then
j := value.IndexOf(':');
if j > -1 then
begin
//separate out into key and value
key := value.SubString(0, j);
value := value.Remove(0, j+1);
//it should already come in here without quotes when parsing paramstr(x).
//but it might have quotes if it came in from a parameter file;
StripQuotes(value);
end
{$ELSE}
if bTryValue then
j := Pos(':',value);
if j > 0 then
begin
//separate out into key and value
key := Copy(value,1,j-1);
Delete(value,1,j);
//it should already come in here without quotes when parsing paramstr(x).
//but it might have quotes if it came in from a parameter file;
StripQuotes(value);
end
{$ENDIF}
else
begin
//no value just a key
key := value;
value := '';
end;
if option = nil then
TOptionsRegistry.RegisteredOptions.TryGetValue(LowerCase(key), option);
if option <> nil then
begin
if option.HasValue and (value = '') then
begin
parseErrors.AddError(Format(SOptionExpectedValue, [key]));
continue;
end;
if option.IsOptionFile then
begin
if not option.HasValue then
value := key;
//TODO : should options file override other options or vica versa?
if not FileExists(value) then
begin
parseErrors.AddError(Format(SParameterFileDoesNotExist, [value]));
continue;
end;
try
InternalParseFile(value,parseErrors);
except
on e : Exception do
begin
parseErrors.AddError(Format(SErrorParsingParameterFile, [value]) + e.Message);
end;
end;
end
else
begin
try
if bUseKey then
(option as IOptionDefInvoke).Invoke(key)
else
(option as IOptionDefInvoke).Invoke(value);
except
on e : Exception do
begin
parseErrors.AddError(Format(SErrorSettingOption, [key, value]) + e.Message );
end;
end;
end;
end
else
begin
parseErrors.AddError(SUnknownCommandLineOption + values.Strings[i]);
continue;
end;
end;
end;
procedure TCommandLineParser.InternalParseFile(const fileName: string; const parseErrors: IParseResultAddError);
var
sList : TStringList;
begin
sList := TStringList.Create;
try
sList.LoadFromFile(fileName);
InternalParse(sList,parseErrors);
finally
sList.Free;
end;
end;
procedure TCommandLineParser.InternalValidate(const parseErrors: IParseResultAddError);
var
option : IOptionDefintion;
begin
for option in TOptionsRegistry.AllRegisteredOptions do
begin
if option.Required then
begin
if not (option as IOptionDefInvoke).WasFound then
begin
parseErrors.AddError(Format(SOptionNotSpecified, [option.LongName]));
end;
end;
end;
end;
function TCommandLineParser.Parse(const values: TStrings): ICommandLineParseResult;
begin
result := TCommandLineParseResult.Create;
InternalParse(values,result as IParseResultAddError);
InternalValidate(result as IParseResultAddError);
end;
function TCommandLineParser.Parse: ICommandLineParseResult;
var
sList : TStringList;
i : integer;
begin
sList := TStringList.Create;
try
if ParamCount > 0 then
begin
for i := 1 to ParamCount do
sList.Add(ParamStr(i));
end;
result := Self.Parse(sList);
finally
sList.Free;
end;
end;
{ TCommandLineParseResult }
procedure TCommandLineParseResult.AddError(const value: string);
begin
FErrors.Add(value)
end;
constructor TCommandLineParseResult.Create;
begin
FErrors := TStringList.Create;
end;
destructor TCommandLineParseResult.Destroy;
begin
FErrors.Free;
inherited;
end;
function TCommandLineParseResult.GetErrorText: string;
begin
result := FErrors.Text;
end;
function TCommandLineParseResult.GetHasErrors: Boolean;
begin
result := FErrors.Count > 0;
end;
end.