Sha256: ede2600f3e7c34304913908927abbc51f2c2227cfa2f1255ed494f5ff3673285

Contents?: true

Size: 1.61 KB

Versions: 6

Compression:

Stored size: 1.61 KB

Contents

#!/usr/bin/ruby

def cw(val)
  val.nil? ? '' : "(#{val})"
end

def aw(val)
  val.nil? ? '' : "#{val.inspect}"
end

module Geoptima

  class Options

    attr_reader :args, :options, :debug

    def initialize(debug=nil)
      @debug = debug
      @args = []
      @options = {}
    end
    def add(*args,&block)
      puts "Adding option processing for: #{args[0]}" if(debug)
      @options[args[0].to_s] = block
    end
    def method_missing(symbol,*args,&block)
      puts "Adding option processing for: #{symbol}" if(debug)
      @options[symbol.to_s] = block
    end
    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
    def to_s
      "Options[#{@options.keys.sort.join(', ')}]: #{args.join(', ')}"
    end

    def self.process_args(debug=nil)
      options = Options.new(debug)
      options.add('v') {$print_version = 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)
      options.args
    end

  end

end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
geoptima-0.1.4 lib/geoptima/options.rb
geoptima-0.1.3 lib/geoptima/options.rb
geoptima-0.1.2 lib/geoptima/options.rb
geoptima-0.1.1 lib/geoptima/options.rb
geoptima-0.1.0 lib/geoptima/options.rb
geoptima-0.0.9 lib/geoptima/options.rb