Sha256: ad99ee32fe314d7140987f35aaf5f825baf8cbf2e8292aaf90749e1c4977603a

Contents?: true

Size: 1.75 KB

Versions: 17

Compression:

Stored size: 1.75 KB

Contents

module ApplixHash

  module ClassMethods
    # #from_argv builds hash from ARGV like argument vector according to
    # following examples: 
    #
    #   '-f'                  --> { :f      => true }
    #   '--flag'              --> { :flag   => true }
    #   '--flag:false'        --> { :flag   => false }
    #   '--flag=false'        --> { :flag   => 'false' }
    #   '--option=value'      --> { :option => "value" }
    #   '--int=1'             --> { :int    => "1" }
    #   '--float=2.3'         --> { :float  => "2.3" }
    #   '--float:2.3'         --> { :float  => 2.3 }
    #   '--txt="foo bar"'     --> { :txt    => "foo bar" }
    #   '--txt:\'"foo bar"\'' --> { :txt    => "foo bar" }
    #   '--txt:%w{foo bar}'   --> { :txt    => ["foo", "bar"] }
    #   '--now:Time.now'      --> { :now    => #<Date: 3588595/2,0,2299161> }
    #
    # remaining arguments(non flag/options) are inserted as [:args]. eg:
    #     Hash.from_argv %w(--foo --bar=loo 123 now)
    # becomes  
    #     { :foo => true, :bar => 'loo', :args => ["123", "now"] }
    #
    def from_argv argv, opts = {}
      args, h = argv.clone, {}
      while arg = args.first
        key, val = ApplixHash.parse(arg)
        break unless key
        h[key] = val
        args.shift
      end 
      #[args, h]
      h[:args] = args
      h
    end
  end # ClassMethods

  # parse single flag/option into a [key, value] tuple. returns nil on non
  # option/flag arguments.
  def self.parse(arg)
    m = /^(-(\w)|--(\w\w+))(([=:])(.+))?$/.match(arg)
    return [nil, arg] unless m # neither option nor flag -> straight arg
    key = (m[2] || m[3]).to_sym
    value = m[6][/(['"]?)(.*)\1$/,2] rescue true
    value = eval(value) if m[5] == ':'
    [key, value]
  end
end

Hash.extend ApplixHash::ClassMethods

Version data entries

17 entries across 17 versions & 1 rubygems

Version Path
applix-0.4.9 lib/applix/hash.rb
applix-0.4.8 lib/applix/hash.rb
applix-0.4.7 lib/applix/hash.rb
applix-0.4.6 lib/applix/hash.rb
applix-0.4.5 lib/applix/hash.rb
applix-0.4.4 lib/applix/hash.rb
applix-0.4.3 lib/applix/hash.rb
applix-0.4.2 lib/applix/hash.rb
applix-0.3.8 lib/applix/hash.rb
applix-0.3.7 lib/applix/hash.rb
applix-0.3.6 lib/applix/hash.rb
applix-0.3.5 lib/applix/hash.rb
applix-0.3.4 lib/applix/hash.rb
applix-0.3.0 lib/applix/hash.rb
applix-0.2.2 lib/applix.rb
applix-0.2.1 lib/applix.rb
applix-0.2.0 lib/applix.rb