Sha256: 6a9b80093b23fc181f989d812ad2fc869e4c4a78ad325781023d4dec0f8c4746
Contents?: true
Size: 1.75 KB
Versions: 2
Compression:
Stored size: 1.75 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?(:environment_variable) @environment_variable = options[:environment_variable] 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
2 entries across 2 versions & 1 rubygems
Version | Path |
---|---|
clamp-0.5.0 | lib/clamp/parameter.rb |
clamp-0.4.0 | lib/clamp/parameter.rb |