Class: Hieracles::Optparse

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

Constant Summary collapse

OPTIONS =
{
  config: {
    has_arg: true,
    aliases: ['c', 'conf', 'config']
  },
  format: {
    has_arg: true,
    aliases: ['f', 'format']
  },
  params: {
    has_arg: true,
    aliases: ['p', 'params']
  },
  hierafile: {
    has_arg: true,
    aliases: ['h', 'hierafile']
  },
  basepath: {
    has_arg: true,
    aliases: ['b', 'basepath']
  },
  encpath: {
    has_arg: true,
    aliases: ['e', 'encpath']
  },
  version: {
    has_arg: false,
    aliases: ['v', 'version']
  },
  yaml_facts: {
    has_arg: true,
    aliases: ['y', 'yaml']
  },
  json_facts: {
    has_arg: true,
    aliases: ['j', 'json']
  },
  interactive: {
    has_arg: false,
    aliases: ['i', 'interactive']
  }
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(array) ⇒ Optparse

Returns a new instance of Optparse.



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/hieracles/optparse.rb', line 50

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

#optionkeysObject



72
73
74
75
76
77
78
79
80
# File 'lib/hieracles/optparse.rb', line 72

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