Skip to content

Commit 78c47db

Browse files
refactored; params validation now accounts for some that are combined with a value
1 parent aa9bac3 commit 78c47db

File tree

1 file changed

+41
-17
lines changed

1 file changed

+41
-17
lines changed

generator/Delphi/src/generator.dpr

Lines changed: 41 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ type
1818
private
1919
FGenerator: TGenerator;
2020
FParams: TStringList;
21+
function CheckShortParams(const AParam: char): Boolean;
22+
function CheckLongParams(const AParam: string): Boolean;
2123
protected
2224
function ParseConsoleParams: boolean;
2325
procedure Run;
@@ -58,6 +60,34 @@ begin
5860
end;
5961
end;
6062

63+
function TOneBRCGenerator.CheckLongParams(const AParam: string): Boolean;
64+
var
65+
J: Integer;
66+
begin
67+
for J := 0 to Pred(Length(cLongOptions)) do
68+
begin
69+
if (AParam = cLongOptions[J]) then
70+
begin
71+
Result := True;
72+
Break;
73+
end;
74+
end;
75+
end;
76+
77+
function TOneBRCGenerator.CheckShortParams(const AParam: char): Boolean;
78+
var
79+
J: Integer;
80+
begin
81+
for J := 0 to Pred(Length(cShortOptions)) do
82+
begin
83+
if (AParam = cShortOptions[J]) then
84+
begin
85+
Result := True;
86+
Break;
87+
end;
88+
end;
89+
end;
90+
6191
destructor TOneBRCGenerator.Destroy;
6292
begin
6393
if Assigned(FParams) then
@@ -70,6 +100,7 @@ var
70100
I, J, invalid, valid: Integer;
71101
tmpLineCount: String;
72102
ParamOK: Boolean;
103+
SkipNext: Boolean;
73104
begin
74105
Result := false;
75106
// initialize the params list
@@ -99,35 +130,28 @@ begin
99130
// check for invalid input
100131
if FParams.Count > 0 then
101132
begin
133+
SkipNext := False;
102134
for I := 0 to FParams.Count - 1 do
103135
begin
104-
ParamOK := False;
105-
for J := 0 to Pred(Length(cShortOptions)) do
136+
if SkipNext then
106137
begin
107-
if (FParams[I] = cShortOptions[J]) then
108-
begin
109-
ParamOK := True;
110-
Break;
111-
end;
138+
SkipNext := False;
139+
Continue;
112140
end;
113141

114-
if not ParamOK then
115-
for J := 0 to Pred(Length(cLongOptions)) do
116-
begin
117-
if (FParams[I] = cLongOptions[J]) then
118-
begin
119-
ParamOK := True;
120-
Break;
121-
end;
122-
end;
142+
if (Length(FParams[I]) = 1) or (FParams[I][2] = '=') then
143+
ParamOK := CheckShortParams(FParams[I][1])
144+
else
145+
ParamOK := CheckLongParams(Copy(FParams[I], 1, Pos('=', FParams[I]) - 1));
123146

147+
// if we found a bad parameter, don't need to check the rest of them
124148
if not ParamOK then
125149
Break;
126150
end;
127151

128152
if not ParamOK then
129153
begin
130-
WriteLn(Format(rsErrorMessage, [FParams.Text]));
154+
WriteLn(Format(rsErrorMessage, [FParams.CommaText]));
131155
Result := false;
132156
exit;
133157
end;

0 commit comments

Comments
 (0)