|
| 1 | +// Copyright 2026, Jerome Shidel |
| 2 | +// The Clear BSD License |
| 3 | +// All rights reserved. |
| 4 | + |
| 5 | +unit CfgOpts; |
| 6 | + |
| 7 | +{$mode objfpc}{$H+} |
| 8 | + |
| 9 | +{$I patches.pp} // Various compiler directives to "fix" things. |
| 10 | +{$I version.def} // Version information defines |
| 11 | + |
| 12 | +interface |
| 13 | + |
| 14 | +uses |
| 15 | + {$IFDEF USES_CWString} cwstring, {$ENDIF} |
| 16 | + {$IFDEF UNIX} |
| 17 | + cthreads, |
| 18 | + {$ENDIF} |
| 19 | + SysUtils, |
| 20 | + { you can add units after this } |
| 21 | + Version, PasExt; |
| 22 | + |
| 23 | +var |
| 24 | + Source : String; |
| 25 | + Output : String; |
| 26 | + |
| 27 | +{ Display Program Banner and Version } |
| 28 | +procedure Banner; |
| 29 | + |
| 30 | +{ Process Command Line Options } |
| 31 | +procedure Options; |
| 32 | + |
| 33 | +{ Display the "See Help" Message and terminate with an error code. } |
| 34 | +procedure SeeHelp(Message : String); |
| 35 | + |
| 36 | +{ Display program help and terminate. } |
| 37 | +procedure Help; |
| 38 | + |
| 39 | + |
| 40 | +implementation |
| 41 | + |
| 42 | +procedure Banner; |
| 43 | +begin |
| 44 | + LogMessage(vbNormal,APP_PRODUCTNAME + ' v' + APP_VERSION); |
| 45 | + LogMessage(vbNormal,'Copyright ' + APP_LEGALCOPYRIGHT); |
| 46 | + LogMessage(vbNormal,'The Clear BSD License '); |
| 47 | + LogMessage(vbNormal, ''); |
| 48 | +end; |
| 49 | + |
| 50 | +procedure Help; |
| 51 | + |
| 52 | +const |
| 53 | + Switches : array of record |
| 54 | + S, L, V, M : String; |
| 55 | + end = ( |
| 56 | + (S:''; L:'--version'; V:''; M:'Display version information and exit.'), |
| 57 | + (S:'-h'; L:'--help'; V:''; M:'Display command-line help and exit.'), |
| 58 | + (S:''; L:''; V:''; M:''), |
| 59 | + (S:'-v'; L:'--verbose'; V:'(level)'; M:'Specify a verbosity level (0-4).'), |
| 60 | + (S:''; L:''; V:''; M:''), |
| 61 | + (S:'-s'; L:'--source'; V:'(path)'; M:'Specify a path that contains "The List" files.'), |
| 62 | + (S:'-o'; L:'--output'; V:'(path)'; M:'Specify a path to store the HTML files.') |
| 63 | +// (S:''; L:''; V:''; M:''), |
| 64 | +// (S:''; L:'--cicd'; V:''; M:'More strict verification for usage with CI/CD.') |
| 65 | +// (S:''; L:'--issues'; V:'(file)'; M:'Write a list of the files with issues to a file.') |
| 66 | + ); |
| 67 | + |
| 68 | +var |
| 69 | + I, J, WA, WB : Integer; |
| 70 | + S : String; |
| 71 | +begin |
| 72 | + WriteLn('usage: ', ExtractFileName(ParamStr(0)), ' [option]'); |
| 73 | + WriteLn; |
| 74 | + WA:=0; |
| 75 | + WB:=0; |
| 76 | + for I := 0 to High(Switches) do begin |
| 77 | + J:=Length(Switches[I].S); |
| 78 | + if Length(Switches[I].L) > 0 then Inc(J, Length(Switches[I].L) + 3); |
| 79 | + if WA < J then WA:=J; |
| 80 | + if Length(Switches[I].V) >= WB then WB:=Length(Switches[I].V) + 1; |
| 81 | + end; |
| 82 | + for I := 0 to High(Switches) do begin |
| 83 | + S:=RightPad(Switches[I].S, 2); |
| 84 | + if Switches[I].L <> '' then |
| 85 | + Cat(S, WhenTrue(Trim(S) <> '', ', ', SPACE2) + Switches[I].L); |
| 86 | + if S = '' then begin |
| 87 | + WriteLn; |
| 88 | + if Switches[I].M <> '' then begin |
| 89 | + WriteLn(Switches[I].M); |
| 90 | + WriteLn; |
| 91 | + end; |
| 92 | + continue; |
| 93 | + end else |
| 94 | + S:=RightPad(S, WA) + RightPad(Switches[I].V, WB) + Switches[I].M; |
| 95 | + WriteLn(TrimRight(S)); |
| 96 | + end; |
| 97 | + WriteLn; |
| 98 | + Halt(0); |
| 99 | +end; |
| 100 | + |
| 101 | +procedure SeeHelp(Message : String); |
| 102 | +begin |
| 103 | + LogMessage(vbCritical, Message); |
| 104 | + LogMessage(vbCritical, 'see: ' + ExtractFileName(ParamStr(0)) + ' --help'); |
| 105 | + Halt(1); |
| 106 | +end; |
| 107 | + |
| 108 | + |
| 109 | +procedure Options; |
| 110 | +var |
| 111 | + I : Integer; |
| 112 | + Opt : String; |
| 113 | + |
| 114 | + function NextOpt(Required : boolean = true) : string; |
| 115 | + begin |
| 116 | + if I > ParamCount then |
| 117 | + SeeHelp('Missing parameter for command-line option: ' + Opt); |
| 118 | + if Required and (Trim(ParamStr(I)) = '') then |
| 119 | + SeeHelp('Cannot provide blank parameter for command-line option: ' + Opt); |
| 120 | + Result:=ParamStr(I); |
| 121 | + Inc(I); |
| 122 | + end; |
| 123 | + |
| 124 | +var |
| 125 | + V, E : Integer; |
| 126 | + |
| 127 | +begin |
| 128 | + I:=1; |
| 129 | + While I <= ParamCount do begin |
| 130 | + Opt:=NextOpt; |
| 131 | + case Opt of |
| 132 | + '--version' : begin |
| 133 | + Banner; |
| 134 | + Halt(0); |
| 135 | + end; |
| 136 | + '-h', '--help' : begin |
| 137 | + Help; |
| 138 | + end; |
| 139 | + '-v', '--verbose' : begin |
| 140 | + Val(NextOpt, V, E); |
| 141 | + if (E<>0) then V:=-1; |
| 142 | + case V of |
| 143 | + 0 : VerboseLevel:=vbCritical; |
| 144 | + 1 : VerboseLevel:=vbMinimal; |
| 145 | + 2 : VerboseLevel:=vbNormal; |
| 146 | + 3 : VerboseLevel:=vbVerbose; |
| 147 | + 4 : VerboseLevel:=vbExcessive; |
| 148 | + else |
| 149 | + SeeHelp('Invalid verbosity level: ' + IntToStr(V)); |
| 150 | + end; |
| 151 | + end; |
| 152 | +// '--cicd' : CICD:=True; |
| 153 | + // '--issues' : Issues:=Trim(NextOpt); |
| 154 | + '-s', '--source' : Source:=IncludeTrailingPathDelimiter(Trim(NextOpt)); |
| 155 | + '-o', '--output' : Output:=IncludeTrailingPathDelimiter(Trim(NextOpt)); |
| 156 | + else |
| 157 | + SeeHelp('Invalid command-line option: ' + Opt); |
| 158 | + end; |
| 159 | + end; |
| 160 | +end; |
| 161 | + |
| 162 | +end. |
0 commit comments