Class: Clamp::Parameter

Inherits:
Attribute show all
Defined in:
lib/clamp/parameter.rb,
lib/clamp/parameter/parsing.rb,
lib/clamp/parameter/declaration.rb

Defined Under Namespace

Modules: Declaration, Parsing

Instance Attribute Summary collapse

Attributes inherited from Attribute

#description, #environment_variable

Instance Method Summary collapse

Methods inherited from Attribute

#default_method, #help, #help_rhs, #ivar_name, #read_method, #write_method

Constructor Details

#initialize(name, description, options = {}) ⇒ Parameter

Returns a new instance of Parameter.



7
8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/clamp/parameter.rb', line 7

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

Instance Attribute Details

#attribute_nameObject (readonly)

Returns the value of attribute attribute_name.



22
23
24
# File 'lib/clamp/parameter.rb', line 22

def attribute_name
  @attribute_name
end

#nameObject (readonly)

Returns the value of attribute name.



22
23
24
# File 'lib/clamp/parameter.rb', line 22

def name
  @name
end

Instance Method Details

#consume(arguments) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/clamp/parameter.rb', line 28

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

#default_valueObject



41
42
43
44
45
46
47
# File 'lib/clamp/parameter.rb', line 41

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

#help_lhsObject



24
25
26
# File 'lib/clamp/parameter.rb', line 24

def help_lhs
  name
end