1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
|
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.
|