/[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 91477 - (show annotations)
Sun Jan 14 09:47:13 2007 UTC (13 years ago) by ericb
File size: 9561 byte(s)
Synchronized with ISE 6.0.65740
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 library: "Free implementation of ELKS library"
9 copyright: "Copyright (c) 1986-2006, Eiffel Software and others"
10 license: "Eiffel Forum License v2 (see forum.txt)"
11 date: "$Date$"
12 revision: "$Revision$"
13
14 class
15 ARGUMENTS
16
17 feature -- Access
18
19 argument (i: INTEGER): STRING is
20 -- `i'-th argument of command that started system execution
21 -- (the command name if `i' = 0)
22 require
23 index_large_enough: i >= 0
24 index_small_enough: i <= argument_count
25 external
26 "built_in"
27 ensure
28 argument_not_void: Result /= Void
29 end
30
31 argument_array: ARRAY [STRING] is
32 -- Array containing command name (position 0) and arguments
33 once
34 Result := internal_argument_array
35 ensure
36 argument_array_not_void: Result /= Void
37 end
38
39 Command_line: STRING is
40 -- Total command line
41 local
42 i: INTEGER
43 once
44 Result := Command_name.twin
45 from
46 i := 1
47 until
48 i > argument_count
49 loop
50 Result.append (" ")
51 Result.append (argument (i))
52 i := i + 1
53 end
54 ensure
55 Result.count >= command_name.count
56 end
57
58 Command_name: STRING is
59 -- Name of command that started system execution
60 once
61 Result := argument (0)
62 ensure
63 definition: Result.is_equal (argument (0))
64 end
65
66 feature -- Status report
67
68 has_word_option (opt: STRING): INTEGER is
69 obsolete "Use index_of_word_option instead."
70 do
71 Result := index_of_word_option (opt)
72 end
73
74 index_of_word_option (opt: STRING): INTEGER is
75 -- Does command line specify word option `opt' and, if so,
76 -- at what position?
77 -- If one of the arguments in list of space-separated arguments
78 -- is `Xopt', where `X' is the current `option_sign',
79 -- then index of this argument in list;
80 -- else 0.
81 require
82 opt_non_void: opt /= Void
83 opt_meaningful: not opt.is_empty
84 local
85 i: INTEGER
86 do
87 from
88 i := 1
89 until
90 i > argument_count or else
91 option_word_equal (argument_array.item (i), opt)
92 loop
93 i := i + 1
94 end
95 if i <= argument_count then
96 Result := i
97 end
98 end
99
100 index_of_beginning_with_word_option (opt: STRING): INTEGER is
101 -- Does command line specify argument beginning with word
102 -- option `opt' and, if so, at what position?
103 -- If one of the arguments in list of space-separated arguments
104 -- is `Xoptxx', where `X' is the current `option_sign', 'xx'
105 -- is arbitrary, possibly empty sequence of characters,
106 -- then index of this argument in list;
107 -- else 0.
108 require
109 opt_non_void: opt /= Void
110 opt_meaningful: not opt.is_empty
111 local
112 i: INTEGER
113 do
114 from
115 i := 1
116 until
117 i > argument_count or else
118 option_word_begins_with (argument_array.item (i), opt)
119 loop
120 i := i + 1
121 end
122 if i <= argument_count then
123 Result := i
124 end
125 end
126
127 has_character_option (o: CHARACTER): INTEGER is
128 obsolete "Use index_of_character_option instead."
129 do
130 Result := index_of_character_option (o)
131 end
132
133 index_of_character_option (o: CHARACTER): INTEGER is
134 -- Does command line specify character option `o' and, if so,
135 -- at what position?
136 -- If one of the space-separated arguments is of the form `Xxxoyy',
137 -- where `X' is the current `option_sign', `xx' and `yy'
138 -- are arbitrary, possibly empty sequences of characters,
139 -- then index of this argument in list of arguments;
140 -- else 0.
141 require
142 o_non_null: o /= '%U'
143 local
144 i: INTEGER
145 do
146 from
147 i := 1
148 until
149 i > argument_count or else
150 option_character_equal (argument_array.item (i), o)
151 loop
152 i := i + 1
153 end
154 if i <= argument_count then Result := i end
155 end
156
157 separate_character_option_value (o: CHARACTER): STRING is
158 -- The value, if any, specified after character option `o' on
159 -- the command line.
160 -- This is one of the following (where `X' is the current
161 -- `option_sign', `xx' and 'yy' are arbitrary, possibly empty
162 -- sequences of characters):
163 -- `val' if command line includes two consecutive arguments
164 -- of the form `Xxxoyy' and `val' respectively.
165 -- Empty string if command line includes argument `Xxxoyy', which is
166 -- either last argument or followed by argument starting with `X'.
167 -- Void if there is no argument of the form `Xxxoyy'.
168 require
169 o_non_null: o /= '%U'
170 local
171 p: INTEGER
172 do
173 p := index_of_character_option (o)
174 if p /= 0 then
175 if p = argument_count or else
176 argument_array.item (p + 1).item (1) = option_sign.item
177 then
178 Result := ""
179 else
180 Result := argument_array.item (p + 1)
181 end
182 end
183 end
184
185 separate_word_option_value (opt: STRING): STRING is
186 -- The value, if any, specified after word option `opt' on the
187 -- command line.
188 -- This is one of the following (where `X' is the current `option_sign'):
189 -- `val' if command line includes two consecutive arguments
190 -- of the form `Xopt' and `val' respectively.
191 -- Empty string if command line includes argument `Xopt', which is
192 -- either last argument or followed by argument starting with `X'.
193 -- Void if no `Xopt' argument.
194 require
195 opt_non_void: opt /= Void
196 opt_meaningful: not opt.is_empty
197 local
198 p: INTEGER
199 do
200 p := index_of_word_option (opt)
201 if p /= 0 then
202 if p = argument_count or else
203 argument_array.item (p + 1).item (1) = option_sign.item
204 then
205 Result := ""
206 else
207 Result := argument_array.item (p + 1)
208 end
209 end
210 end
211
212 coalesced_option_character_value (o: CHARACTER): STRING is
213 obsolete "Use coalesced_character_option_value instead."
214 do
215 Result := coalesced_character_option_value (o)
216 end
217
218 coalesced_character_option_value (o: CHARACTER): STRING is
219 -- The value, if any, specified for character option `o' on
220 -- the command line.
221 -- Defined as follows (where 'X' is the current 'option_sign' and
222 -- 'xx' is an arbitrary, possibly empty sequence of characters):
223 -- `val' if command line includes an argument of the form `Xxxoval'
224 -- (this may be an empty string if argument is just `Xxxo').
225 -- Void otherwise.
226 require
227 o_non_null: o /= '%U'
228 local
229 p: INTEGER
230 l: STRING
231 do
232 p := index_of_character_option (o)
233 if p /= 0 then
234 l := argument_array.item (p).twin
235 if option_sign.item /= '%U' then
236 l.remove (1)
237 end
238 Result := l.substring (l.index_of (o, 1) + 1, l.count)
239 end
240 end
241
242 coalesced_option_word_value (opt: STRING): STRING is
243 obsolete "Use coalesced_word_option_value instead."
244 do
245 Result := coalesced_word_option_value (opt)
246 end
247
248 coalesced_word_option_value (opt: STRING): STRING is
249 -- The value, if any, specified for word option `opt' on the
250 -- command line.
251 -- Defined as follows (where X is the current `option_sign'):
252 -- `val' if command line includes an argument of the form `Xoptval'
253 -- (this may be an empty string if argument is just `Xopt').
254 -- Void otherwise.
255 require
256 opt_non_void: opt /= Void
257 opt_meaningful: not opt.is_empty
258 local
259 p: INTEGER
260 l: STRING
261 do
262 p := index_of_beginning_with_word_option (opt)
263 if p /= 0 then
264 l := argument_array.item (p).twin
265 if option_sign.item /= '%U' then
266 l.remove (1)
267 end
268 Result := l.substring (opt.count + 1, l.count)
269 end
270 end
271
272 option_sign: CHARACTER_REF is
273 -- The character used to signal options on the command line.
274 -- This can be '%U' if no sign is necesary for the argument
275 -- to be an option
276 -- Default is '-'
277 once
278 create Result
279 Result.set_item ('-')
280 end
281
282 feature -- Status setting
283
284 set_option_sign (c: CHARACTER) is
285 -- Make `c' the option sign.
286 -- Use'%U' if no sign is necesary for the argument to
287 -- be an option
288 do
289 option_sign.set_item (c)
290 end
291
292 feature -- Measurement
293
294 argument_count: INTEGER is
295 -- Number of arguments given to command that started
296 -- system execution (command name does not count)
297 external
298 "built_in"
299 ensure
300 argument_count_positive: Result >= 0
301 end
302
303 feature {NONE} -- Implementation
304
305 option_word_equal (arg, w: STRING): BOOLEAN is
306 -- Is `arg' equal to the word option `w'?
307 require
308 arg_not_void: arg /= Void
309 w_not_void: w /= Void
310 do
311 if option_sign.item = '%U' then
312 Result := arg.is_equal (w)
313 elseif not arg.is_empty and then arg.item (1) = option_sign.item then
314 Result := arg.substring (2, arg.count).is_equal (w)
315 end
316 end
317
318 option_word_begins_with (arg, w: STRING): BOOLEAN is
319 -- Does `arg' begin with the word option `w'?
320 require
321 arg_not_void: arg /= Void
322 w_not_void: w /= Void
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 require
334 arg_not_void: arg /= Void
335 do
336 if option_sign.item = '%U' then
337 Result := arg.has (c)
338 elseif arg.item (1) = option_sign.item then
339 Result := arg.substring (2, arg.count).has (c)
340 end
341 end
342
343 internal_argument_array: ARRAY [STRING] is
344 -- Array containing command name (position 0) and arguments
345 local
346 i: INTEGER
347 do
348 create Result.make (0, argument_count)
349 from
350 until
351 i > argument_count
352 loop
353 Result.put (argument (i), i)
354 i := i + 1
355 end
356 ensure
357 internal_argument_array_not_void: Result /= Void
358 end
359
360 end

Properties

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

  ViewVC Help
Powered by ViewVC 1.1.23