Class: ParseArgs::Parser::Argument

Inherits:
Object
  • Object
show all
Includes:
Util
Defined in:
lib/parseargs.rb,
lib/parseargs-0.3.0.rb

Direct Known Subclasses

Keyword

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Util

append_features

Constructor Details

#initialize(name, opts = {}) ⇒ Argument

Returns a new instance of Argument.



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/parseargs.rb', line 75

def initialize name, opts = {}
#--{{{
  @name = name 
  @required = get_kw 'required', opts
  @default = get_kw 'default', opts
  @types = get_kw('type', opts, get_kw('types', opts, []))
  @ducktypes = get_kw('ducktype', opts, get_kw('ducktypes', opts, []))
  @coerce = get_kw 'coerce', opts
  @convince = get_kw 'convince', opts
  @validate = get_kw 'validate', opts
  @types = [ @types ].flatten
  @ducktypes = [ @ducktypes ].flatten
  if @coerce and (String === @coerce or Symbol === @coerce)
    msg = @coerce.dup
    @coerce = lambda{|obj| obj.send msg}
  end
  if @convince and Module === @convince
    mod = @convince
    @convince = lambda{|obj| obj.extend mod}
  end
  if @validate and not Proc === @validate 
    equiv = @validate.dup
    @validate = lambda{|obj| equiv === obj}
  end
  @value_set = false
#--}}}
end

Instance Attribute Details

#coerceObject

Returns the value of attribute coerce.



71
72
73
# File 'lib/parseargs.rb', line 71

def coerce
  @coerce
end

#convinceObject

Returns the value of attribute convince.



72
73
74
# File 'lib/parseargs.rb', line 72

def convince
  @convince
end

#defaultObject

Returns the value of attribute default.



68
69
70
# File 'lib/parseargs.rb', line 68

def default
  @default
end

#ducktypeObject

Returns the value of attribute ducktype.



70
71
72
# File 'lib/parseargs.rb', line 70

def ducktype
  @ducktype
end

#nameObject

Returns the value of attribute name.



65
66
67
# File 'lib/parseargs.rb', line 65

def name
  @name
end

#requiredObject Also known as: required?

Returns the value of attribute required.



66
67
68
# File 'lib/parseargs.rb', line 66

def required
  @required
end

#typeObject

Returns the value of attribute type.



69
70
71
# File 'lib/parseargs.rb', line 69

def type
  @type
end

#validateObject

Returns the value of attribute validate.



73
74
75
# File 'lib/parseargs.rb', line 73

def validate
  @validate
end

Instance Method Details

#optionalObject Also known as: optional?

–}}}



151
152
153
154
155
# File 'lib/parseargs.rb', line 151

def optional
#--{{{
  not required
#--}}}
end

#valueObject

–}}}



142
143
144
145
146
147
148
149
150
# File 'lib/parseargs.rb', line 142

def value
#--{{{
  if @value_set
    @value
  else
    @default
  end
#--}}}
end

#value=(v) ⇒ Object

–}}}



102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/parseargs.rb', line 102

def value= v
#--{{{
  unless @types.empty?
    ok = false 
    @types.each{|t| if v.is_a? t; ok = true; break; end}
    if not ok and @coerce
      v = @coerce[v]
      ok = false 
      @types.each{|t| if v.is_a? t; ok = true; break; end}
    end
    raise TypeError, "value given not of type(#{ @types.join ',' }) in '#{ @name }='" unless ok
  end
  unless @ducktypes.empty?
    ok = true
    @ducktypes.each{|t| unless v.respond_to? t;ok = false; break; end}
    if not ok and @convince
      @convince[v]
      ok = true
      @ducktypes.each{|t| unless v.respond_to? t; ok = false; break; end}
    end
    if not ok and @coerce
      v = @coerce[v]
      ok = true
      @ducktypes.each{|t| unless v.respond_to? t; ok = false; break; end}
    end
    raise TypeError, "value given not of ducktype(#{ @ducktypes.join ',' }) in '#{ @name }='" unless ok
  end
  if @validate
    valid = @validate[v]
    unless valid
      vmsg = v.inspect
      vmsg = vmsg[0,16] << '...' if vmsg.size > 16
      raise ArgumentError, "invalid value(#{ vmsg }) given in '#{ @name }='"
    end
  end
  @value = v
  @value_set = true
  @value
#--}}}
end