Class: Main::Parameter::List

Inherits:
Array
  • Object
show all
Defined in:
lib/main/parameter.rb

Instance Method Summary collapse

Instance Method Details

#<<(*a) ⇒ Object



550
551
552
553
# File 'lib/main/parameter.rb', line 550

def <<(*a)
  delete(*a)
  super
end

#[](*index) ⇒ Object



555
556
557
558
559
560
561
562
# File 'lib/main/parameter.rb', line 555

def [](*index)
  first = index.first
  if(index.size == 1 and (first.is_a?(String) or first.is_a?(Symbol)))
    first = first.to_s
    return detect{|param| param.name == first}
  end
  return super
end

#defaults!Object



504
505
506
507
508
509
510
511
512
# File 'lib/main/parameter.rb', line 504

def defaults!
  each do |p|
    if(p.defaults? and (not p.given?)) 
      p.defaults.each do |default|
        p.values << (default.respond_to?('to_proc') ? main.instance_eval(&default) : default) # so as NOT to set 'given?'
      end
    end
  end
end

#delete(name, *names) ⇒ Object



539
540
541
542
543
544
545
546
547
548
# File 'lib/main/parameter.rb', line 539

def delete name, *names
  name, *names = name.names if Parameter === name
  names = Cast.list_of_string name, *names
  keep = []
  each do |param|
    common = Cast.list_of_string(param.names) & names 
    keep << param if common.empty?
  end
  replace keep
end

#parse(main) ⇒ Object



337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
# File 'lib/main/parameter.rb', line 337

def parse main
  @main, @argv, @env = main, main.argv, main.env

  ignore, stop = [], argv.index('--')

  if stop
    ignore = argv[stop .. -1]
    (argv.size - stop).times{ argv.pop }
  end

  argv.push "--#{ argv.shift }" if argv.first == 'help'

  parse_options argv

  return 'help' if detect{|p| p.name.to_s == 'help' and p.given?}

  parse_keywords argv
  parse_arguments argv
  parse_environment env

  defaults!
  validate!

  argv.push(*ignore) unless ignore.empty? 

  return self
ensure
  @main, @argv, @env = nil
end

#parse_arguments(argv, params = nil) ⇒ Object



417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
# File 'lib/main/parameter.rb', line 417

def parse_arguments argv, params=nil
  params ||= select{|p| p.type == :argument}

  params.each do |p|
    if p.arity >= 0
      p.arity.times do
        break if argv.empty?
        value = argv.shift
        p.add_value value
      end
    else
      arity = p.arity.abs - 1
      arity.times do
        break if argv.empty?
        value = argv.shift
        p.add_value value
      end
      argv.size.times do
        value = argv.shift
        p.add_value value
      end
    end
  end

=begin
  params.each do |p|
    p.setup!
  end
=end
end

#parse_environment(env, params = nil) ⇒ Object



486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
# File 'lib/main/parameter.rb', line 486

def parse_environment env, params=nil
  params ||= select{|p| p.type == :environment}

  params.each do |p|
    names = p.names
    name = names.first
    value = env[name]
    next unless value
    p.add_value value
  end

=begin
  params.each do |p|
    p.setup!
  end
=end
end

#parse_keywords(argv, params = nil) ⇒ Object



448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
# File 'lib/main/parameter.rb', line 448

def parse_keywords argv, params=nil
  params ||= select{|p| p.type == :keyword}

  replacements = {}

  params.each do |p|
    names = p.names
    name = names.sort_by{|n| [n.size,n]}.last

    kre = %r/^\s*(#{ names.join '|' })\s*=/
    opt = "--#{ name }"
    i = -1 

    argv.each_with_index do |a, idx|
      i += 1
      b = argv[idx + 1]
      s = "#{ a }#{ b }"
      m, key, *ignored = kre.match(s).to_a
      if m
        replacements[i] ||= a.gsub %r/^\s*#{ key }/, opt
        next
      end
=begin
      abbrev = name.index(key) == 0
      if abbrev
        replacements[i] ||= a.gsub %r/^\s*#{ key }/, opt
      end
=end
    end
  end

  replacements.each do |i, r|
    argv[i] = r
  end

  parse_options argv, params
end

#parse_options(argv, params = nil) ⇒ Object



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
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
# File 'lib/main/parameter.rb', line 367

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

=begin
  params.each do |p|
    p.setup!
  end
=end
end

#validate!Object



514
515
516
517
518
519
520
521
522
# File 'lib/main/parameter.rb', line 514

def validate!
  each do |p|
    #p.adding_handlers do
      #next if p.arity == -1
      #raise NotGiven, "#{ p.typename } not given" if(p.required? and (not p.given?))
    #end
    p.setup!
  end
end