From e75d61df84dc920f5e460cf56127b51e777559ca Mon Sep 17 00:00:00 2001 From: Erich Eckner Date: Mon, 19 Dec 2016 11:00:21 +0100 Subject: bissl was gemacht --- analyzer.lpr | 134 ++++++++++++++++++++++++++++++++++++++++++++++------------- analyzer.lps | 31 +++++++++++++- 2 files changed, 133 insertions(+), 32 deletions(-) diff --git a/analyzer.lpr b/analyzer.lpr index 02dfd2a..348518c 100644 --- a/analyzer.lpr +++ b/analyzer.lpr @@ -6,72 +6,146 @@ uses {$IFDEF UNIX}{$IFDEF UseCThreads} cthreads, {$ENDIF}{$ENDIF} - Classes, SysUtils, CustApp + classes, sysUtils, custApp { you can add units after this }; type + tOption = (oHelp, oInput); + { TAnalyzer } - TAnalyzer = class(TCustomApplication) + tAnalyzer = class(tCustomApplication) protected - procedure DoRun; override; + options: array of tOption; + oValues: array of string; + procedure doRun; override; + procedure parseOptions; + procedure illegalOption(i: longint); public - constructor Create(TheOwner: TComponent); override; - destructor Destroy; override; - procedure WriteHelp; virtual; + constructor create(theOwner: tComponent); override; + destructor destroy; override; + procedure writeHelp; virtual; end; +const + shortOptions: array[tOption] of char = + ('h','i'); + longOptions: array[tOption] of string = ( + 'help', + 'input' + ); + optionValue: array[tOption] of byte = ( // bit 0: kann Wert haben; bit 1: muss Wert haben + 0, + 3 + ); + { TAnalyzer } -procedure TAnalyzer.DoRun; +procedure tAnalyzer.doRun; var - ErrorMsg: String; + errorMsg: string; begin // quick check parameters - ErrorMsg:=CheckOptions('h', 'help'); - if ErrorMsg<>'' then begin - ShowException(Exception.Create(ErrorMsg)); - Terminate; - Exit; + errorMsg:=checkOptions('hi:', 'help input:'); + if errorMsg<>'' then begin + showException(Exception.Create(ErrorMsg)); + halt(1); end; // parse parameters - if HasOption('h', 'help') then begin - WriteHelp; - Terminate; - Exit; + if hasOption('h', 'help') then begin + writeHelp; + terminate; + exit; + end; + + if not hasOption('i','input') then begin + writeln('error: required option ''-i'' not given'); + writeHelp; + halt(1); end; { add your program here } + writeln(''''+getOptionValue('i','input')+''''); + // stop program loop - Terminate; + terminate; +end; + +procedure tAnalyzer.parseOptions; +var + i: longint; + s,t: string; + o: tOption; + v,gefunden: boolean; +begin + setlength(options,0); + setlength(oValues,0); + i:=1; + while i<=paramcount do begin + s:=paramstr(i); + if copy(s,1,1)<>'-' then + illegalOption(i); + delete(s,1,1); + gefunden:=false; + if copy(s,1,1)='-' then begin // long option + delete(s,1,1); + if pos('=',s)>0 then begin + t:=copy(s,pos('=',s)+1,length(s)); + s:=copy(s,1,pos('=',s)-1); + v:=true; + end + else begin + t:=''; + v:=false; + end; + for o:=low(longOptions) to high(longOptions) do + if (longOptions[o]<>'') and (longOptions[o]=s) and ((not v) or odd(optionValue[o])) then begin + gefunden:=true; + break; + end; + end; + + if not gefunden then + illegalOption(i); + + inc(i); + end; +end; + +procedure tAnalyzer.illegalOption(i: longint); +begin + writeln('Illegal option at position '+inttostr(i)+': '''+paramstr(i)+''''); + writeHelp; + halt(1); end; -constructor TAnalyzer.Create(TheOwner: TComponent); +constructor tAnalyzer.create(theOwner: tComponent); begin - inherited Create(TheOwner); - StopOnException:=True; + inherited create(theOwner); + parseOptions; + stopOnException:=true; end; -destructor TAnalyzer.Destroy; +destructor tAnalyzer.destroy; begin - inherited Destroy; + inherited destroy; end; -procedure TAnalyzer.WriteHelp; +procedure tAnalyzer.writeHelp; begin { add your help code here } - writeln('Usage: ', ExeName, ' -h'); + writeln('usage: ', exeName, ' -h'); end; var - Application: TAnalyzer; + application: tAnalyzer; begin - Application:=TAnalyzer.Create(nil); - Application.Title:='Analyzer'; - Application.Run; - Application.Free; + application:=tAnalyzer.create(nil); + application.title:='analyzer'; + application.run; + application.free; end. diff --git a/analyzer.lps b/analyzer.lps index be061c4..2f4a0df 100644 --- a/analyzer.lps +++ b/analyzer.lps @@ -3,15 +3,42 @@ - + + + + + + + + + + + - + + + + + + + + + + + + + + + + + + -- cgit v1.2.3-54-g00ecf