program analyzer; {$mode objfpc}{$H+} uses {$IFDEF UNIX}{$IFDEF UseCThreads} cthreads, {$ENDIF}{$ENDIF} classes, sysUtils, custApp { you can add units after this }; type tOption = (oHelp, oInput); { TAnalyzer } tAnalyzer = class(tCustomApplication) protected 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; 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; var errorMsg: string; begin // quick check parameters 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; 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; 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); begin inherited create(theOwner); parseOptions; stopOnException:=true; end; destructor tAnalyzer.destroy; begin inherited destroy; end; procedure tAnalyzer.writeHelp; begin { add your help code here } writeln('usage: ', exeName, ' -h'); end; var application: tAnalyzer; begin application:=tAnalyzer.create(nil); application.title:='analyzer'; application.run; application.free; end.