Class: OptionParser

Inherits:
Object
  • Object
show all
Defined in:
lib/option_parser.rb

Class Method Summary collapse

Class Method Details

.parse(argv) ⇒ Object



2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/option_parser.rb', line 2

def self.parse(argv)
  return [{},[]] if argv.empty?
  
  options  = {}
  rest     = []
  switch   = nil
  
  for value in argv
    # values is a switch
    if value[0] == 45
      switch = value.slice((value[1] == 45 ? 2 : 1)..-1)
      options[switch] = nil
    else
      if switch
        # we encountered a switch so this
        # value belongs  to  that  switch
        options[switch] = value
        switch = nil
      else
        rest << value
      end
    end
  end
  
  [options, rest]
end