|
| 1 | +unit Baseline.Console; |
| 2 | + |
| 3 | +interface |
| 4 | + |
| 5 | +uses |
| 6 | + System.Classes, SysUtils; |
| 7 | + |
| 8 | +const |
| 9 | + cOptionHelp: array of string = ['-h', '--help']; |
| 10 | + cOptionVersion: array of string = ['-v', '--version']; |
| 11 | + cOptionInput: array of string = ['-i', '--input-file']; |
| 12 | + |
| 13 | + |
| 14 | +resourcestring |
| 15 | + rsAppTitle = 'One Billion Row Challenge Baseline'; |
| 16 | + rsGeneratorVersion = 'baseline v%s'; |
| 17 | + rsErrorMessage = 'ERROR: %s'; |
| 18 | + rsMissingInputFlag = 'Missing input file flag.'; |
| 19 | + rsNoInputFile = 'Input file does not exist.'; |
| 20 | + |
| 21 | +function ParseCmdLineParams (out aInputFile: string): Boolean; |
| 22 | + |
| 23 | + |
| 24 | +implementation |
| 25 | + |
| 26 | +uses |
| 27 | + System.IOUtils; |
| 28 | + |
| 29 | +{$I version.inc} |
| 30 | + |
| 31 | +procedure WriteHelp; |
| 32 | +begin |
| 33 | + WriteLn('Generates the output for the challenge'); |
| 34 | + WriteLn; |
| 35 | + WriteLn('USAGE'); |
| 36 | + WriteLn(' baseline <flags>'); |
| 37 | + WriteLn; |
| 38 | + WriteLn('FLAGS'); |
| 39 | + WriteLn(' -h|--help Writes this help message and exits'); |
| 40 | + WriteLn(' -v|--version Writes the version and exits'); |
| 41 | + WriteLn(' -i|--input-file <filename> The file containing the Weather Stations'); |
| 42 | +end; |
| 43 | + |
| 44 | +function ArrayContains (const aArray: array of string; const aValue: string): Boolean; |
| 45 | +var |
| 46 | + iValue: string; |
| 47 | +begin |
| 48 | + Result := False; |
| 49 | + for iValue in aArray do begin |
| 50 | + if aValue.ToLower = iValue then begin |
| 51 | + Result := True; |
| 52 | + break; |
| 53 | + end; |
| 54 | + end; |
| 55 | +end; |
| 56 | + |
| 57 | +function ParseCmdLineParams (out aInputFile: string): Boolean; |
| 58 | +var |
| 59 | + I: Integer; |
| 60 | +begin |
| 61 | + Result := False; |
| 62 | + aInputFile := ''; |
| 63 | + |
| 64 | + // 0 is the exe path, so we start at 1 |
| 65 | + for I := 1 to ParamCount do begin |
| 66 | + if ArrayContains (cOptionHelp, ParamStr(I)) then begin |
| 67 | + WriteHelp; |
| 68 | + exit; |
| 69 | + end |
| 70 | + else if ArrayContains (cOptionVersion, ParamStr(I)) then begin |
| 71 | + WriteLn(Format(rsGeneratorVersion, [ cVersion ])); |
| 72 | + exit; |
| 73 | + end |
| 74 | + else if ArrayContains (cOptionInput, ParamStr(I)) then begin |
| 75 | + // must be followed by the user's specified input file |
| 76 | + if (I+1) <= ParamCount then |
| 77 | + aInputFile := ExpandFileName (ParamStr (I+1)); |
| 78 | + if not TFile.Exists (aInputFile) then |
| 79 | + WriteLn(Format(rsErrorMessage, [ rsNoInputFile ])) |
| 80 | + else begin |
| 81 | + Result := True; |
| 82 | + exit; |
| 83 | + end; |
| 84 | + end |
| 85 | + else |
| 86 | + WriteLn(Format(rsErrorMessage, [ rsMissingInputFlag ])); |
| 87 | + end; |
| 88 | +end; |
| 89 | + |
| 90 | +end. |
| 91 | + |
| 92 | + |
0 commit comments