Sha256: bc321f0d1fbd7e20d8d0040b4cfa2f8992f13e74b23c73a6980b856e7e33c14a

Contents?: true

Size: 1.72 KB

Versions: 1

Compression:

Stored size: 1.72 KB

Contents

require 'clamp/attribute'

module Clamp

  class Parameter < Attribute

    def initialize(name, description, options = {})
      @name = name
      @description = description
      infer_attribute_name_and_multiplicity
      if options.has_key?(:attribute_name)
        @attribute_name = options[:attribute_name].to_s
      end
      if options.has_key?(:default)
        @default_value = options[:default]
      end
      if options.has_key?(:env)
        @environment_variable = options[:env]
      end
    end

    attr_reader :name, :attribute_name

    def help_lhs
      name
    end

    def consume(arguments)
      if required? && arguments.empty?
        raise ArgumentError, "no value provided"
      end
      if multivalued?
        if arguments.length > 0
          arguments.shift(arguments.length)
        end
      else
        arguments.shift
      end
    end

    def default_value
      if defined?(@default_value)
        @default_value
      elsif multivalued?
        []
      end
    end

    private

    NAME_PATTERN = "([A-Za-z0-9_-]+)"

    def infer_attribute_name_and_multiplicity
      case @name
      when /^\[#{NAME_PATTERN}\]$/
        @attribute_name = $1
      when /^\[#{NAME_PATTERN}\] ...$/
        @attribute_name = "#{$1}_list"
        @multivalued = true
      when /^#{NAME_PATTERN} ...$/
        @attribute_name = "#{$1}_list"
        @multivalued = true
        @required = true
      when /^#{NAME_PATTERN}$/
        @attribute_name = @name
        @required = true
      else
        raise "invalid parameter name: '#{name}'"
      end
      @attribute_name = @attribute_name.downcase.tr('-', '_')
    end

    def multivalued?
      @multivalued
    end

    def required?
      @required
    end

  end

end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
jls-clamp-0.3.1.2 lib/clamp/parameter.rb