Class: Hieracles::Optparse

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(array) ⇒ Optparse

Returns a new instance of Optparse.



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/hieracles/optparse.rb', line 11

def initialize(array)
  @options = {}
  @payload = []
  ok = optionkeys
  while x = array.shift
    if x[0] == '-'
      found = ok[x[/[a-z][-_a-z]*$/]]
      if found
        if found[:has_args]
          @options[found[:var]] = array.shift
        else
          @options[found[:var]] = true
        end
      else
        array.shift
      end
    else
      @payload << x
    end
  end
end

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options.



5
6
7
# File 'lib/hieracles/optparse.rb', line 5

def options
  @options
end

#payloadObject (readonly)

Returns the value of attribute payload.



5
6
7
# File 'lib/hieracles/optparse.rb', line 5

def payload
  @payload
end

Instance Method Details

#available_optionsObject



7
8
9
# File 'lib/hieracles/optparse.rb', line 7

def available_options
  {}
end

#optionkeysObject



33
34
35
36
37
38
39
40
41
# File 'lib/hieracles/optparse.rb', line 33

def optionkeys
  back = {}
  available_options.each do |k, v|
    v[:aliases].each do |a|
      back[a] = { var: k, has_args: v[:has_arg] }
    end
  end
  back
end