Sha256: c8277fd74391f5294b08879adfd5b067bf2dde735fa73782a0171aac8764be74

Contents?: true

Size: 718 Bytes

Versions: 2

Compression:

Stored size: 718 Bytes

Contents

class SimpleOptionParser

  attr_accessor :defaults

  def self.parse(argv, defaults={})
    new(defaults).parse(argv)
  end

  def initialize(defaults={})
    @defaults = defaults
  end

  def parse(argv)
    options = @defaults.dup
    while argv.first =~ %r{^--([\w\-]+)(?:=(.*))?$}
      key, value = $1, $2
      key = key.gsub('-', '_').to_sym
      options[key] = parse_value(value)
      argv.shift
    end
    options
  end

  def parse_value(value)
    case value
    when %r{^[\d.]+$}
      value.to_f
    when %r{^(\d+)/(\d+)$}
      Rational($1.to_i, $2.to_i)
    when 'true', 'yes', 'on'
      true
    when 'false', 'no', 'off'
      false
    when nil
      true
    else
      value
    end
  end

end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
simple_option_parser-0.3 lib/simple_option_parser.rb
simple_option_parser-0.2 lib/simple_option_parser.rb