Class: Geoptima::Options

Inherits:
Object
  • Object
show all
Defined in:
lib/geoptima/options.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(debug = nil) ⇒ Options

Returns a new instance of Options.



17
18
19
20
21
# File 'lib/geoptima/options.rb', line 17

def initialize(debug=nil)
  @debug = debug
  @args = []
  @options = {}
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(symbol, *args, &block) ⇒ Object



26
27
28
29
# File 'lib/geoptima/options.rb', line 26

def method_missing(symbol,*args,&block)
  puts "Adding option processing for: #{symbol}" if(debug)
  @options[symbol.to_s] = block
end

Instance Attribute Details

#argsObject (readonly)

Returns the value of attribute args.



15
16
17
# File 'lib/geoptima/options.rb', line 15

def args
  @args
end

#debugObject (readonly)

Returns the value of attribute debug.



15
16
17
# File 'lib/geoptima/options.rb', line 15

def debug
  @debug
end

#optionsObject (readonly)

Returns the value of attribute options.



15
16
17
# File 'lib/geoptima/options.rb', line 15

def options
  @options
end

Class Method Details

.process_args(debug = nil) {|options| ... } ⇒ Object

Yields:



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/geoptima/options.rb', line 46

def self.process_args(debug=nil)
  options = Options.new(debug)
  options.add('f') {$flush_stdout = true}
  options.add('v') {$print_version = true}
  options.add('w') {$verbose = true}
  options.add('d') {$debug = true}
  options.add('h') {$help = true}
  puts "Processing options: #{options}" if(debug)
  yield options if(block_given?)
  while arg = ARGV.shift do
    if arg =~ /^\-(\w+)/
      $1.split(//).each do |a|
        options.process a
      end
    else
      options.args << arg
    end
  end
  puts "Geoptima Gem Version: #{Geoptima::VERSION}" if($print_version)
  STDOUT.sync if($flush_stdout)
  options.args
end

Instance Method Details

#add(*args, &block) ⇒ Object



22
23
24
25
# File 'lib/geoptima/options.rb', line 22

def add(*args,&block)
  puts "Adding option processing for: #{args[0]}" if(debug)
  @options[args[0].to_s] = block
end

#process(a) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/geoptima/options.rb', line 30

def process(a)
  puts "Looking for match to option #{a}" if(debug)
  @options.each do |opt,block|
    puts "Comparing option #{a} to known option #{opt}" if(debug)
    if opt === a
      puts "Calling block for option #{a}: #{block.inspect}" if(debug)
      block.call
      return
    end
  end
  puts "Unknown option: -#{a}"
end

#to_sObject



42
43
44
# File 'lib/geoptima/options.rb', line 42

def to_s
  "Options[#{@options.keys.sort.join(', ')}]: #{args.join(', ')}"
end