Skip to content

Commit 5ad5d94

Browse files
Made it compile in XE2 again.
1 parent 553bd03 commit 5ad5d94

File tree

3 files changed

+70
-8
lines changed

3 files changed

+70
-8
lines changed

Src/VSoft.CommandLine.Options.pas

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,7 @@ class function TOptionsRegistry.RegisterCommand(const name: string; const alias
229229
begin
230230
cmdDef := TCommandDefImpl.Create(name,alias, usage, description, helpString,visible);
231231
result := TCommandDefinition.Create(cmdDef);
232-
FCommandDefs.Add(name.ToLower,cmdDef);
232+
FCommandDefs.Add(LowerCase(name),cmdDef);
233233
end;
234234

235235

@@ -259,7 +259,7 @@ class procedure TOptionsRegistry.Clear;
259259
class function TOptionsRegistry.GetCommandByName(const name: string): ICommandDefinition;
260260
begin
261261
result := nil;
262-
FCommandDefs.TryGetValue(name.ToLower,Result);
262+
FCommandDefs.TryGetValue(LowerCase(name), Result);
263263

264264
end;
265265

@@ -280,7 +280,7 @@ class procedure TOptionsRegistry.EmumerateCommandOptions(const commandName: stri
280280
var
281281
cmd : ICommandDefinition;
282282
begin
283-
if not FCommandDefs.TryGetValue(commandName.ToLower,cmd) then
283+
if not FCommandDefs.TryGetValue(LowerCase(commandName), cmd) then
284284
raise Exception.Create('Unknown command : ' + commandName);
285285

286286
cmd.EmumerateCommandOptions(proc);
@@ -338,7 +338,7 @@ class procedure TOptionsRegistry.PrintUsage(const command: ICommandDefinition; c
338338
i: Integer;
339339
printOption : TConstProc<IOptionDefinition>;
340340
begin
341-
exeName := ChangeFileExt(ExtractFileName(ParamStr(0)), '').ToLower();
341+
exeName := LowerCase(ChangeFileExt(ExtractFileName(ParamStr(0)), ''));
342342
if not command.IsDefault then
343343
begin
344344
proc('');
@@ -375,7 +375,7 @@ class procedure TOptionsRegistry.PrintUsage(const command: ICommandDefinition; c
375375
begin
376376
s := WrapText(opt.HelpText, sLineBreak, [' ', '-', #9, ','], maxDescW -1);
377377

378-
descStrings := s.Split([sLineBreak], TStringSplitOptions.None);
378+
descStrings := TStringUtils.Split(s, sLineBreak);
379379
for i := 0 to length(descStrings) -1 do
380380
descStrings[i] := Trim(descStrings[i]);
381381

@@ -468,7 +468,7 @@ class procedure TOptionsRegistry.PrintUsage(const proc: TConstProc<string>; cons
468468
exeName : string;
469469
begin
470470
proc('');
471-
exeName := ChangeFileExt(ExtractFileName(ParamStr(0)), '').ToLower();
471+
exeName := LowerCase(ChangeFileExt(ExtractFileName(ParamStr(0)), ''));
472472

473473
//if we have more than 1 command then we are using command mode
474474
if FCommandDefs.Count > 0 then
@@ -494,7 +494,7 @@ class procedure TOptionsRegistry.PrintUsage(const proc: TConstProc<string>; cons
494494
continue;
495495

496496
s := WrapText(cmd.Description,maxDescW);
497-
descStrings := s.Split([sLineBreak], TStringSplitOptions.None);
497+
descStrings := TStringUtils.Split(s, sLineBreak);
498498
proc(' ' + PadRight(cmd.Name, descriptionTab -1) + descStrings[0]);
499499
numDescStrings := Length(descStrings);
500500
if numDescStrings > 1 then

Src/VSoft.CommandLine.Utils.pas

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,11 @@ interface
55

66
function GetConsoleWidth : integer;
77

8+
type
9+
TStringUtils = class
10+
class function Split(const theString : string; const separator : string): TArray<string>;
11+
end;
12+
813
implementation
914

1015
uses
@@ -13,9 +18,51 @@ implementation
1318
{$ENDIF}
1419
System.SysUtils,
1520
System.StrUtils;
16-
1721

22+
function IndexOf(const theString : string; const value : string; const startIndex : integer) : integer;
23+
begin
24+
Result := PosEx(Value, theString, StartIndex + 1) - 1;
25+
end;
1826

27+
class function TStringUtils.Split(const theString : string; const separator : string): TArray<string>;
28+
const
29+
DeltaGrow = 32;
30+
var
31+
NextSeparator, LastIndex: Integer;
32+
Total: Integer;
33+
CurrentLength: Integer;
34+
S: string;
35+
begin
36+
Total := 0;
37+
LastIndex := 0;
38+
CurrentLength := 0;
39+
NextSeparator := IndexOf(theString, Separator, LastIndex);
40+
while (NextSeparator > 0) do
41+
begin
42+
S := Copy(theString, LastIndex + 1, NextSeparator - LastIndex);
43+
if (S <> '') then
44+
begin
45+
Inc(Total);
46+
if CurrentLength < Total then
47+
begin
48+
CurrentLength := Total + DeltaGrow;
49+
SetLength(Result, CurrentLength);
50+
end;
51+
Result[Total - 1] := S;
52+
end;
53+
LastIndex := NextSeparator + Length(Separator);
54+
NextSeparator := IndexOf(theString, Separator, LastIndex);
55+
end;
56+
57+
if (LastIndex < Length(theString)) then
58+
begin
59+
Inc(Total);
60+
SetLength(Result, Total);
61+
Result[Total - 1] := Copy(theString, LastIndex + 1, Length(theString) - LastIndex);
62+
end
63+
else
64+
SetLength(Result, Total);
65+
end;
1966

2067
{$IFDEF MSWINDOWS}
2168
function GetConsoleWidth : integer;

Tests/TestCommandLineParser.pas

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,13 +101,16 @@ TCommandLineParserTests = class
101101
[Test]
102102
procedure Can_Parse_SpaceNameValueSeparator;
103103

104+
[Test]
105+
procedure Test_String_Split;
104106

105107
end;
106108

107109
implementation
108110

109111
uses
110112
Classes,
113+
VSoft.CommandLine.Utils,
111114
VSoft.CommandLine.OptionDef;
112115

113116
{ TCommandLineParserTests }
@@ -395,6 +398,18 @@ procedure TCommandLineParserTests.Test_Single_Option;
395398

396399
end;
397400

401+
procedure TCommandLineParserTests.Test_String_Split;
402+
const
403+
s : string = 'Hello' + #13#10 + 'World' + #13#10;
404+
var
405+
values : TArray<string>;
406+
begin
407+
values := TStringUtils.Split(s, #13#10);
408+
Assert.AreEqual(2, length(values));
409+
Assert.AreEqual('hello', values[0]);
410+
Assert.AreEqual('world', values[1]);
411+
end;
412+
398413
procedure TCommandLineParserTests.Will_Generate_Error_For_Extra_Unamed_Parameter;
399414
var
400415
def : IOptionDefinition;

0 commit comments

Comments
 (0)