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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289 | with Ada.Command_Line;
with Ada.Containers.Indefinite_Vectors;
with Ada.Directories;
with Ada.Strings.Fixed;
with Ada.Text_IO;
with WL.String_Maps;
package body WL.Command_Line is
package Option_Value_Maps is
new WL.String_Maps (String);
package Argument_Vectors is
new Ada.Containers.Indefinite_Vectors (Positive, String);
Default_Values : Option_Value_Maps.Map;
Cached_Values : Option_Value_Maps.Map;
Argument_Vector : Argument_Vectors.Vector;
Loaded_Arguments : Boolean := False;
procedure Check_Arguments;
--------------
-- Argument --
--------------
function Argument (Index : Positive) return String is
begin
Check_Arguments;
return Argument_Vector.Element (Index);
end Argument;
--------------------
-- Argument_Count --
--------------------
function Argument_Count return Natural is
begin
Check_Arguments;
return Argument_Vector.Last_Index;
end Argument_Count;
---------------------
-- Check_Arguments --
---------------------
procedure Check_Arguments is
Skip_Next : Boolean := False;
Keep_Rest : Boolean := False;
begin
if Loaded_Arguments then
return;
end if;
for I in 1 .. Ada.Command_Line.Argument_Count loop
if Keep_Rest then
Argument_Vector.Append (Ada.Command_Line.Argument (I));
elsif Skip_Next then
Skip_Next := False;
else
declare
Text : constant String :=
Ada.Command_Line.Argument (I);
begin
if Text = "--" then
Keep_Rest := True;
elsif Text'Length > 2
and then Text (Text'First .. Text'First + 1) = "--"
then
null;
else
Argument_Vector.Append (Ada.Command_Line.Argument (I));
end if;
end;
end if;
end loop;
Loaded_Arguments := True;
end Check_Arguments;
-----------------
-- Find_Option --
-----------------
function Find_Option
(Long_Name : String;
Short_Name : Character)
return String
is
begin
if not Cached_Values.Contains (Long_Name) then
for I in 1 .. Ada.Command_Line.Argument_Count loop
declare
Argument : constant String :=
Ada.Command_Line.Argument (I);
Separator_Index : constant Natural :=
Ada.Strings.Fixed.Index (Argument, "=");
begin
if Short_Name /= ' ' and then Argument = ('-', Short_Name) then
if I < Ada.Command_Line.Argument_Count then
Cached_Values.Insert
(Long_Name, Ada.Command_Line.Argument (I + 1));
else
Cached_Values.Insert (Long_Name, "");
end if;
exit;
elsif Argument'Length > 3 and then Separator_Index > 0
and then Argument (1 .. 2) = "--"
and then Argument (3 .. Separator_Index - 1) = Long_Name
then
Cached_Values.Insert
(Long_Name,
Argument (Separator_Index + 1 .. Argument'Last));
exit;
end if;
end;
end loop;
if not Cached_Values.Contains (Long_Name) then
if Default_Values.Contains (Long_Name) then
Cached_Values.Insert
(Long_Name, Default_Values.Element (Long_Name));
else
Cached_Values.Insert
(Long_Name, "");
end if;
end if;
end if;
return Cached_Values.Element (Long_Name);
end Find_Option;
-----------------
-- Find_Option --
-----------------
function Find_Option
(Long_Name : String;
Short_Name : Character)
return Boolean
is
begin
if not Cached_Values.Contains (Long_Name) then
Scan_Command_Line :
for I in 1 .. Ada.Command_Line.Argument_Count loop
declare
Argument : constant String :=
Ada.Command_Line.Argument (I);
begin
if Argument = "--" & Long_Name then
Cached_Values.Insert (Long_Name, "true");
exit Scan_Command_Line;
elsif Argument = "--no-" & Long_Name then
Cached_Values.Insert (Long_Name, "");
exit Scan_Command_Line;
elsif Argument'Length >= 2
and then Short_Name /= ' '
and then Argument (Argument'First) = '-'
and then Argument (Argument'First + 1) /= '-'
then
for Item of Argument loop
if Item /= '-' and then Item = Short_Name then
Cached_Values.Insert (Long_Name, "true");
exit Scan_Command_Line;
end if;
end loop;
end if;
end;
end loop Scan_Command_Line;
if not Cached_Values.Contains (Long_Name) then
if Default_Values.Contains (Long_Name) then
declare
Value : constant String :=
Default_Values.Element (Long_Name);
begin
if Value = "true"
or else Value = "yes"
or else Value = "1"
then
Cached_Values.Insert
(Long_Name, "true");
else
Cached_Values.Insert (Long_Name, "");
end if;
end;
else
Cached_Values.Insert (Long_Name, "");
end if;
end if;
end if;
return Cached_Values.Element (Long_Name) = "true";
end Find_Option;
-----------------
-- Find_Option --
-----------------
function Find_Option
(Long_Name : String;
Short_Name : Character;
Default : Integer := 0)
return Integer
is
Value : constant String := Find_Option (Long_Name, Short_Name);
begin
if Value = "" then
return Default;
else
return Integer'Value (Value);
end if;
exception
when Constraint_Error =>
return Default;
end Find_Option;
-------------------
-- Load_Defaults --
-------------------
procedure Load_Defaults
(File_Path : String)
is
use Ada.Text_IO;
File : File_Type;
Line_Number : Natural := 0;
begin
if not Ada.Directories.Exists (File_Path) then
return;
end if;
Open (File, In_File, File_Path);
while not End_Of_File (File) loop
Line_Number := Line_Number + 1;
declare
use Ada.Strings, Ada.Strings.Fixed;
Full_Line : constant String := Get_Line (File);
Comment_Index : constant Natural :=
Index (Full_Line, "#");
Active_Line : constant String :=
(if Comment_Index = 0
then Full_Line
else Full_Line (1 .. Comment_Index - 1));
Trimmed_Line : constant String :=
Trim (Active_Line, Both);
Equal_Index : constant Natural :=
Index (Trimmed_Line, "=");
Name : constant String :=
Trim (Trimmed_Line (1 .. Equal_Index - 1),
Right);
Value : constant String :=
Trim (Trimmed_Line (Equal_Index + 1 ..
Trimmed_Line'Last),
Left);
begin
if Trimmed_Line'Length > 0 then
if Name = "" then
raise Constraint_Error with
"error in " & Ada.Directories.Simple_Name (File_Path)
& " line" & Natural'Image (Line_Number)
& ": invalid setting";
end if;
if Default_Values.Contains (Name) then
Ada.Text_IO.Put_Line
(Ada.Text_IO.Standard_Error,
"warning: option '" & Name & "' appears twice with "
& (if Value = Default_Values.Element (Name)
then "the same value"
else "different values"));
else
Default_Values.Insert (Name, Value);
end if;
end if;
end;
end loop;
Close (File);
exception
when Name_Error =>
null;
end Load_Defaults;
end WL.Command_Line;
|