Sha256: 5c3946439b2738427952dd8242e3b9047aa39186e52e60b7344bf18cb8b8976f

Contents?: true

Size: 1.16 KB

Versions: 1

Compression:

Stored size: 1.16 KB

Contents

# Convienience module to put into Hash to allow options
# parsed to be a bit more accessible
class Brut::CLI::Options
  def initialize(parsed_options)
    @parsed_options = parsed_options
    @defaults = {}
  end

  def to_h = @parsed_options

  def [](key) = @parsed_options[key]
  def key?(key) = @parsed_options.key?(key)
  def set_default(sym,default_value)
    @defaults[sym] = default_value
  end

  def method_missing(sym,*args,&block)
    boolean = false
    if sym.to_s =~ /\?$/
      sym = sym.to_s[0..-2].to_sym
      boolean = true
    end

    sym_underscore = sym.to_s.gsub(/\-/,"_").to_sym
    sym_dash       = sym.to_s.gsub(/_/,"-").to_sym

    value = if self.key?(sym_underscore)
              self[sym_underscore]
            elsif self.key?(sym_dash)
              self[sym_dash]
            elsif args[0].kind_of?(Hash) && args[0].key?(:default)
              return args[0][:default]
            elsif @defaults.key?(sym_underscore)
              @defaults[sym_underscore]
            elsif @defaults.key?(sym_dash)
              @defaults[sym_dash]
            else
              nil
            end
    if boolean
      !!value
    else
      value
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
brut-0.0.1 lib/brut/cli/options.rb