Skip to content

Commit 833a60d

Browse files
committed
refactor: Moving WriteHelp + Resource Strings
1 parent cdbf3f4 commit 833a60d

File tree

4 files changed

+84
-47
lines changed

4 files changed

+84
-47
lines changed

Diff for: generator/Common/generate.console.pas

+64
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
unit Generate.Console;
2+
3+
{$mode ObjFPC}{$H+}
4+
5+
interface
6+
7+
uses
8+
Classes
9+
, SysUtils
10+
;
11+
12+
const
13+
cShortOptHelp: Char = 'h';
14+
cLongOptHelp = 'help';
15+
cShortOptVersion: Char = 'v';
16+
cLongOptVersion = 'version';
17+
cShortOptInput: Char = 'i';
18+
cLongOptInput = 'input-file';
19+
cShortOptOutput: Char = 'o';
20+
cLongOptOutput = 'output-file';
21+
cShortOptNumner: Char = 'n';
22+
cLongOptNumber = 'line-count';
23+
24+
25+
resourcestring
26+
rsAppTitle = 'One Billion Row Challenge Generator';
27+
rsGeneratorVersion = 'generator v%s';
28+
rsErrorMessage = 'ERROR: %s';
29+
rsMissingInputFlag = 'Missing input file flag.';
30+
rsMissingOutputFlag = 'Missing output file flag.';
31+
rsMissingLineCountFlag = 'Missing line count flag.';
32+
rsInvalidInteger = 'ERROR: Invalid integer "%s".';
33+
rsInvalidLineNumber = 'Number of lines should be a positive number, greater than 0.';
34+
rsInputFile = 'Input Filename: "%s"';
35+
rsOutputFile = 'Output Filename: "%s"';
36+
rsLineCount = 'Line Count: %.n';
37+
38+
var
39+
inputFilename: String = '';
40+
outputFilename: String = '';
41+
lineCount: Int64 = 0;
42+
43+
procedure WriteHelp;
44+
45+
46+
implementation
47+
48+
procedure WriteHelp;
49+
begin
50+
WriteLn('Generates the measurement file with the specified number of lines');
51+
WriteLn;
52+
WriteLn('USAGE');
53+
WriteLn(' generator <flags>');
54+
WriteLn;
55+
WriteLn('FLAGS');
56+
WriteLn(' -h|--help Writes this help message and exits');
57+
WriteLn(' -v|--version Writes the version and exits');
58+
WriteLn(' -i|--input-file <filename> The file containing the Weather Stations');
59+
WriteLn(' -o|--output-file <filename> The file that will contain the generated lines');
60+
WriteLn(' -n|--line-count <number> The amount of lines to be generated');
61+
end;
62+
63+
end.
64+

Diff for: generator/Common/version.inc

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
'0.2'
1+
'0.3'

Diff for: generator/Lazarus/src/generator.lpi

+6-1
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@
131131
<RunParams>
132132
<FormatVersion Value="2"/>
133133
</RunParams>
134-
<Units Count="2">
134+
<Units Count="3">
135135
<Unit0>
136136
<Filename Value="generator.lpr"/>
137137
<IsPartOfProject Value="True"/>
@@ -141,6 +141,11 @@
141141
<IsPartOfProject Value="True"/>
142142
<UnitName Value="Generate.Common"/>
143143
</Unit1>
144+
<Unit2>
145+
<Filename Value="../../Common/generate.console.pas"/>
146+
<IsPartOfProject Value="True"/>
147+
<UnitName Value="Generate.Console"/>
148+
</Unit2>
144149
</Units>
145150
</ProjectOptions>
146151
<CompilerOptions>

Diff for: generator/Lazarus/src/generator.lpr

+13-45
Original file line numberDiff line numberDiff line change
@@ -9,28 +9,13 @@
99
Classes
1010
, SysUtils
1111
, CustApp
12+
, Generate.Console
1213
, Generate.Common
1314
;
1415

1516
const
1617
cVersion = {$I version.inc};
1718

18-
cShortOptHelp: Char = 'h';
19-
cLongOptHelp = 'help';
20-
cShortOptVersion: Char = 'v';
21-
cLongOptVersion = 'version';
22-
cShortOptInput: Char = 'i';
23-
cLongOptInput = 'input-file';
24-
cShortOptOutput: Char = 'o';
25-
cLongOptOutput = 'output-file';
26-
cShortOptNumner: Char = 'n';
27-
cLongOptNumber = 'line-count';
28-
29-
var
30-
inputFilename: String = '';
31-
outputFilename: String = '';
32-
lineCount: Int64 = 0;
33-
3419
type
3520

3621
{ TOneBRCGenerator }
@@ -43,7 +28,6 @@ TOneBRCGenerator = class(TCustomApplication)
4328
public
4429
constructor Create(TheOwner: TComponent); override;
4530
destructor Destroy; override;
46-
procedure WriteHelp; virtual;
4731
published
4832
end;
4933

@@ -73,7 +57,7 @@ procedure TOneBRCGenerator.DoRun;
7357
if ErrorMsg<>'' then
7458
begin
7559
//ShowException(Exception.Create(ErrorMsg));
76-
WriteLn('ERROR: ', ErrorMsg);
60+
WriteLn(Format(rsErrorMessage, [ ErrorMsg ]));
7761
Terminate;
7862
Exit;
7963
end;
@@ -88,7 +72,7 @@ procedure TOneBRCGenerator.DoRun;
8872

8973
if HasOption(cShortOptVersion, cLongOptVersion) then
9074
begin
91-
WriteLn('generator v', cVersion);
75+
WriteLn(Format(rsGeneratorVersion, [ cVersion ]));
9276
Terminate;
9377
Exit;
9478
end;
@@ -102,7 +86,7 @@ procedure TOneBRCGenerator.DoRun;
10286
end
10387
else
10488
begin
105-
WriteLn('ERROR: Missing input file flag');
89+
WriteLn(Format(rsErrorMessage, [ rsMissingInputFlag ]));
10690
Terminate;
10791
Exit;
10892
end;
@@ -116,7 +100,7 @@ procedure TOneBRCGenerator.DoRun;
116100
end
117101
else
118102
begin
119-
WriteLn('ERROR: Missing output file flag');
103+
WriteLn(Format(rsErrorMessage, [ rsMissingOutputFlag ]));
120104
Terminate;
121105
Exit;
122106
end;
@@ -130,30 +114,30 @@ procedure TOneBRCGenerator.DoRun;
130114
tmpLineCount:= StringReplace(tmpLineCount, '_', '', [rfReplaceAll]);
131115
if not TryStrToInt64(tmpLineCount, lineCount) then
132116
begin
133-
WriteLn('ERROR: Invalid integer "',tmpLineCount,'"');
117+
WriteLn(Format(rsInvalidInteger, [ tmpLineCount ]));
134118
Terminate;
135119
Exit;
136120
end;
137121
if not (lineCount > 0) then
138122
begin
139-
WriteLn('ERROR: Number of lines should be a positive number, greater than 0.');
123+
WriteLn(Format(rsErrorMessage, [ rsInvalidLineNumber ]));
140124
Terminate;
141125
Exit;
142126
end;
143127
end
144128
else
145129
begin
146-
WriteLn('ERROR: Missing line count flag');
130+
WriteLn(Format(rsErrorMessage, [ rsMissingLineCountFlag ]));
147131
Terminate;
148132
Exit;
149133
end;
150134

151135
inputFilename:= ExpandFileName(inputFilename);
152136
outputFilename:= ExpandFileName(outputFilename);
153137

154-
WriteLn('Input File : ', inputFilename);
155-
WriteLn('Output File: ', outputFilename);
156-
WriteLn('Line Count : ', Format('%.0n', [ Double(lineCount) ]));
138+
WriteLn(Format(rsInputFile, [ inputFilename ]));
139+
WriteLn(Format(rsOutputFile, [ outputFilename ]));
140+
WriteLn(Format(rsLineCount, [ Double(lineCount) ]));
157141
WriteLn;
158142

159143
FGenerator:= TGenerator.Create(inputFilename, outputFilename, lineCount);
@@ -163,7 +147,7 @@ procedure TOneBRCGenerator.DoRun;
163147
except
164148
on E: Exception do
165149
begin
166-
WriteLn('ERROR: ', E.Message);
150+
WriteLn(Format(rsErrorMessage, [ E.Message ]));
167151
end;
168152
end;
169153
finally
@@ -185,27 +169,11 @@ destructor TOneBRCGenerator.Destroy;
185169
inherited Destroy;
186170
end;
187171

188-
procedure TOneBRCGenerator.WriteHelp;
189-
begin
190-
{ add your help code here }
191-
WriteLn('Generates the measurement file with the specified number of lines');
192-
WriteLn;
193-
WriteLn('USAGE');
194-
WriteLn(' generator <flags>');
195-
WriteLn;
196-
WriteLn('FLAGS');
197-
WriteLn(' -h|--help Writes this help message and exits');
198-
WriteLn(' -v|--version Writes the version and exits');
199-
WriteLn(' -i|--input-file <filename> The file containing the Weather Stations');
200-
WriteLn(' -o|--output-file <filename> The file that will contain the generated lines');
201-
WriteLn(' -n|--line-count <number> The amount of lines to be generated');
202-
end;
203-
204172
var
205173
Application: TOneBRCGenerator;
206174
begin
207175
Application:=TOneBRCGenerator.Create(nil);
208-
Application.Title:='One Billion Row Challenge Generator';
176+
Application.Title:= rsAppTitle;
209177
Application.Run;
210178
Application.Free;
211179
end.

0 commit comments

Comments
 (0)