358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
|
# File 'lib/main/parameter.rb', line 358
def parse_options argv, params = nil
params ||= options
spec, h, s = [], {}, {}
params.each do |p|
head, *tail = p.names
long = "--#{ head }"
shorts = tail.map{|t| "-#{ t }"}
type =
if p.argument_required? then GetoptLong::REQUIRED_ARGUMENT
elsif p.argument_optional? then GetoptLong::OPTIONAL_ARGUMENT
else GetoptLong::NO_ARGUMENT
end
a = [ long, shorts, type ].flatten
spec << a
h[long] = p
s[long] = a
end
begin
GetoptLong.new(argv, *spec).each do |long, value|
value =
case s[long].last
when GetoptLong::NO_ARGUMENT
value.empty? ? true : value
when GetoptLong::OPTIONAL_ARGUMENT
value.empty? ? true : value
when GetoptLong::REQUIRED_ARGUMENT
value
end
p = h[long]
p.add_value value
end
rescue GetoptLong::AmbigousOption, GetoptLong::NeedlessArgument,
GetoptLong::MissingArgument, GetoptLong::InvalidOption => e
c = Parameter.const_get e.class.name.split(/::/).last
ex = c.new e.message
ex.set_backtrace e.backtrace
ex.extend Softspoken
raise ex
end
end
|