Module: RubyExt

Defined in:
lib/ruby_ext/more/miscellaneous.rb,
lib/ruby_ext.rb

Overview

Parsing command line

Class Method Summary collapse

Class Method Details

.argv(input = ARGV) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/ruby_ext/more/miscellaneous.rb', line 14

def self.argv input = ARGV
  # don't want to modify original input and can't use :clone (it may be not an array)
  input = input.collect{|v| v}
  
  list, options = [], {}    
  until input.empty? do
    arg = input.shift.strip
    if arg =~ /.+:$/ and !input.empty?
      k = arg[0..-2].to_sym
      v = input.shift
      v = v.sub(/\s*,$/, '') if v
      options[k] = simple_cast(v)
    else
      v = arg.gsub(/^['"]|['"]$/, '')
      v = v.sub(/\s*,$/, '')
      list << simple_cast(v)
    end
  end
  list << options
  list
end