/[eiffelstudio]/FreeELKS/trunk/library/kernel/arguments.e
ViewVC logotype

Contents of /FreeELKS/trunk/library/kernel/arguments.e

Parent Directory Parent Directory | Revision Log Revision Log


Revision 91424 - (show annotations)
Tue Oct 26 18:39:32 2004 UTC (15 years, 3 months ago) by manus_eiffel
File size: 10263 byte(s)
Initial revision

1 indexing
2
3 description: "[
4 Access to command-line arguments. This class
5 may be used as ancestor by classes needing its facilities.
6 ]"
7
8 status: "See notice at end of class"
9 date: "$Date$"
10 revision: "$Revision$"
11
12 class
13 ARGUMENTS
14
15 feature -- Access
16
17 argument (i: INTEGER): STRING is
18 -- `i'-th argument of command that started system execution
19 -- (the command name if `i' = 0)
20 require
21 index_large_enough: i >= 0
22 index_small_enough: i <= argument_count
23 do
24 Result := arg_option (i)
25 end
26
27 argument_array: ARRAY [STRING] is
28 -- Array containing command name (position 0) and arguments
29 once
30 Result := internal_argument_array
31 ensure
32 argument_array_not_void: Result /= Void
33 end
34
35 Command_line: STRING is
36 -- Total command line
37 local
38 i: INTEGER
39 once
40 Result := Command_name.twin
41 from
42 i := 1
43 until
44 i > argument_count
45 loop
46 Result.append (" ")
47 Result.append (arg_option (i))
48 i := i + 1
49 end
50 ensure
51 Result.count >= command_name.count
52 end
53
54 Command_name: STRING is
55 -- Name of command that started system execution
56 once
57 Result := arg_option (0)
58 ensure
59 definition: Result.is_equal (argument (0))
60 end
61
62 feature -- Status report
63
64 has_word_option (opt: STRING): INTEGER is
65 obsolete "Use index_of_word_option instead."
66 do
67 Result := index_of_word_option (opt)
68 end
69
70 index_of_word_option (opt: STRING): INTEGER is
71 -- Does command line specify word option `opt' and, if so,
72 -- at what position?
73 -- If one of the arguments in list of space-separated arguments
74 -- is `Xopt', where `X' is the current `option_sign',
75 -- then index of this argument in list;
76 -- else 0.
77 require
78 opt_non_void: opt /= Void
79 opt_meaningful: not opt.is_empty
80 local
81 i: INTEGER
82 do
83 from
84 i := 1
85 until
86 i > argument_count or else
87 option_word_equal (argument_array.item (i), opt)
88 loop
89 i := i + 1
90 end
91 if i <= argument_count then
92 Result := i
93 end
94 end
95
96 index_of_beginning_with_word_option (opt: STRING): INTEGER is
97 -- Does command line specify argument beginning with word
98 -- option `opt' and, if so, at what position?
99 -- If one of the arguments in list of space-separated arguments
100 -- is `Xoptxx', where `X' is the current `option_sign', 'xx'
101 -- is arbitrary, possibly empty sequence of characters,
102 -- then index of this argument in list;
103 -- else 0.
104 require
105 opt_non_void: opt /= Void
106 opt_meaningful: not opt.is_empty
107 local
108 i: INTEGER
109 do
110 from
111 i := 1
112 until
113 i > argument_count or else
114 option_word_begins_with (argument_array.item (i), opt)
115 loop
116 i := i + 1
117 end
118 if i <= argument_count then
119 Result := i
120 end
121 end
122
123 has_character_option (o: CHARACTER): INTEGER is
124 obsolete "Use index_of_character_option instead."
125 do
126 Result := index_of_character_option (o)
127 end
128
129 index_of_character_option (o: CHARACTER): INTEGER is
130 -- Does command line specify character option `o' and, if so,
131 -- at what position?
132 -- If one of the space-separated arguments is of the form `Xxxoyy',
133 -- where `X' is the current `option_sign', `xx' and `yy'
134 -- are arbitrary, possibly empty sequences of characters,
135 -- then index of this argument in list of arguments;
136 -- else 0.
137 require
138 o_non_null: o /= '%U'
139 local
140 i: INTEGER
141 do
142 from
143 i := 1
144 until
145 i > argument_count or else
146 option_character_equal (argument_array.item (i), o)
147 loop
148 i := i + 1
149 end
150 if i <= argument_count then Result := i end
151 end
152
153 separate_character_option_value (o: CHARACTER): STRING is
154 -- The value, if any, specified after character option `o' on
155 -- the command line.
156 -- This is one of the following (where `X' is the current
157 -- `option_sign', `xx' and 'yy' are arbitrary, possibly empty
158 -- sequences of characters):
159 -- `val' if command line includes two consecutive arguments
160 -- of the form `Xxxoyy' and `val' respectively.
161 -- Empty string if command line includes argument `Xxxoyy', which is
162 -- either last argument or followed by argument starting with `X'.
163 -- Void if there is no argument of the form `Xxxoyy'.
164 require
165 o_non_null: o /= '%U'
166 local
167 p: INTEGER
168 do
169 p := index_of_character_option (o)
170 if p /= 0 then
171 if p = argument_count or else
172 argument_array.item (p + 1).item (1) = option_sign.item
173 then
174 Result := ""
175 else
176 Result := argument_array.item (p + 1)
177 end
178 end
179 end
180
181 separate_word_option_value (opt: STRING): STRING is
182 -- The value, if any, specified after word option `opt' on the
183 -- command line.
184 -- This is one of the following (where `X' is the current `option_sign'):
185 -- `val' if command line includes two consecutive arguments
186 -- of the form `Xopt' and `val' respectively.
187 -- Empty string if command line includes argument `Xopt', which is
188 -- either last argument or followed by argument starting with `X'.
189 -- Void if no `Xopt' argument.
190 require
191 opt_non_void: opt /= Void
192 opt_meaningful: not opt.is_empty
193 local
194 p: INTEGER
195 do
196 p := index_of_word_option (opt)
197 if p /= 0 then
198 if p = argument_count or else
199 argument_array.item (p + 1).item (1) = option_sign.item
200 then
201 Result := ""
202 else
203 Result := argument_array.item (p + 1)
204 end
205 end
206 end
207
208 coalesced_option_character_value (o: CHARACTER): STRING is
209 obsolete "Use coalesced_character_option_value instead."
210 do
211 Result := coalesced_character_option_value (o)
212 end
213
214 coalesced_character_option_value (o: CHARACTER): STRING is
215 -- The value, if any, specified for character option `o' on
216 -- the command line.
217 -- Defined as follows (where 'X' is the current 'option_sign' and
218 -- 'xx' is an arbitrary, possibly empty sequence of characters):
219 -- `val' if command line includes an argument of the form `Xxxoval'
220 -- (this may be an empty string if argument is just `Xxxo').
221 -- Void otherwise.
222 require
223 o_non_null: o /= '%U'
224 local
225 p: INTEGER
226 l: STRING
227 do
228 p := index_of_character_option (o)
229 if p /= 0 then
230 l := argument_array.item (p).twin
231 if option_sign.item /= '%U' then
232 l.remove (1)
233 end
234 Result := l.substring (l.index_of (o, 1) + 1, l.count)
235 end
236 end
237
238 coalesced_option_word_value (opt: STRING): STRING is
239 obsolete "Use coalesced_word_option_value instead."
240 do
241 Result := coalesced_word_option_value (opt)
242 end
243
244 coalesced_word_option_value (opt: STRING): STRING is
245 -- The value, if any, specified for word option `opt' on the
246 -- command line.
247 -- Defined as follows (where X is the current `option_sign'):
248 -- `val' if command line includes an argument of the form `Xoptval'
249 -- (this may be an empty string if argument is just `Xopt').
250 -- Void otherwise.
251 require
252 opt_non_void: opt /= Void
253 opt_meaningful: not opt.is_empty
254 local
255 p: INTEGER
256 l: STRING
257 do
258 p := index_of_beginning_with_word_option (opt)
259 if p /= 0 then
260 l := argument_array.item (p).twin
261 if option_sign.item /= '%U' then
262 l.remove (1)
263 end
264 Result := l.substring (opt.count + 1, l.count)
265 end
266 end
267
268 option_sign: CHARACTER_REF is
269 -- The character used to signal options on the command line.
270 -- This can be '%U' if no sign is necesary for the argument
271 -- to be an option
272 -- Default is '-'
273 once
274 create Result
275 Result.set_item ('-')
276 end
277
278 feature -- Status setting
279
280 set_option_sign (c: CHARACTER) is
281 -- Make `c' the option sign.
282 -- Use'%U' if no sign is necesary for the argument to
283 -- be an option
284 do
285 option_sign.set_item (c)
286 end
287
288 feature -- Measurement
289
290 argument_count: INTEGER is
291 -- Number of arguments given to command that started
292 -- system execution (command name does not count)
293 once
294 Result := arg_number - 1
295 ensure
296 Result >= 0
297 end
298
299 feature {NONE} -- Implementation
300
301 arg_number: INTEGER is
302 external
303 "C | %"eif_argv.h%""
304 end
305
306 arg_option (i: INTEGER): STRING is
307 external
308 "C | %"eif_argv.h%""
309 end
310
311 option_word_equal (arg, w: STRING): BOOLEAN is
312 -- Is `arg' equal to the word option `w'?
313 do
314 if option_sign.item = '%U' then
315 Result := arg.is_equal (w)
316 elseif arg.item (1) = option_sign.item then
317 Result := arg.substring (2, arg.count).is_equal (w)
318 end
319 end
320
321 option_word_begins_with (arg, w: STRING): BOOLEAN is
322 -- Does `arg' begin with the word option `w'?
323 do
324 if option_sign.item = '%U' and then arg.count >= w.count then
325 Result := arg.substring (1, w.count).is_equal (w)
326 elseif arg.item (1) = option_sign.item and then arg.count > w.count then
327 Result := arg.substring (2, w.count + 1).is_equal (w)
328 end
329 end
330
331 option_character_equal (arg: STRING; c: CHARACTER): BOOLEAN is
332 -- Does `arg' contain the character option `c'?
333 do
334 if option_sign.item = '%U' then
335 Result := arg.has (c)
336 elseif arg.item (1) = option_sign.item then
337 Result := arg.substring (2, arg.count).has (c)
338 end
339 end
340
341 internal_argument_array: ARRAY [STRING] is
342 -- Array containing command name (position 0) and arguments
343 local
344 i: INTEGER
345 do
346 create Result.make (0, argument_count)
347 from
348 until
349 i > argument_count
350 loop
351 Result.put (arg_option (i), i)
352 i := i + 1
353 end
354 ensure
355 internal_argument_array_not_void: Result /= Void
356 end
357
358 indexing
359
360 library: "[
361 EiffelBase: Library of reusable components for Eiffel.
362 ]"
363
364 status: "[
365 Copyright 1986-2001 Interactive Software Engineering (ISE).
366 For ISE customers the original versions are an ISE product
367 covered by the ISE Eiffel license and support agreements.
368 ]"
369
370 license: "[
371 EiffelBase may now be used by anyone as FREE SOFTWARE to
372 develop any product, public-domain or commercial, without
373 payment to ISE, under the terms of the ISE Free Eiffel Library
374 License (IFELL) at http://eiffel.com/products/base/license.html.
375 ]"
376
377 source: "[
378 Interactive Software Engineering Inc.
379 ISE Building
380 360 Storke Road, Goleta, CA 93117 USA
381 Telephone 805-685-1006, Fax 805-685-6869
382 Electronic mail <info@eiffel.com>
383 Customer support http://support.eiffel.com
384 ]"
385
386 info: "[
387 For latest info see award-winning pages: http://eiffel.com
388 ]"
389
390 end -- class ARGUMENTS
391
392
393

Properties

Name Value
svn:eol-style native
svn:keywords Author Date Id Revision

  ViewVC Help
Powered by ViewVC 1.1.23