Sha256: 5b1b431ce5b0a0331cf5fb8b12074f8aa15539ad6c43a1d6e67b08d970777056

Contents?: true

Size: 1.59 KB

Versions: 4

Compression:

Stored size: 1.59 KB

Contents

module Hieracles

  class Optparse
    
    attr_reader :options, :payload

    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']
      }
    }

    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

    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

  end

end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
hieracles-0.2.0 lib/hieracles/optparse.rb
hieracles-0.1.7 lib/hieracles/optparse.rb
hieracles-0.1.6 lib/hieracles/optparse.rb
hieracles-0.1.5 lib/hieracles/optparse.rb