Class: ParseArgs::Parser::Argument
- Includes:
- Util
- Defined in:
- lib/parseargs.rb,
lib/parseargs-0.3.0.rb
Direct Known Subclasses
Instance Attribute Summary collapse
-
#coerce ⇒ Object
Returns the value of attribute coerce.
-
#convince ⇒ Object
Returns the value of attribute convince.
-
#default ⇒ Object
Returns the value of attribute default.
-
#ducktype ⇒ Object
Returns the value of attribute ducktype.
-
#name ⇒ Object
Returns the value of attribute name.
-
#required ⇒ Object
(also: #required?)
Returns the value of attribute required.
-
#type ⇒ Object
Returns the value of attribute type.
-
#validate ⇒ Object
Returns the value of attribute validate.
Instance Method Summary collapse
-
#initialize(name, opts = {}) ⇒ Argument
constructor
A new instance of Argument.
-
#optional ⇒ Object
(also: #optional?)
–}}}.
-
#value ⇒ Object
–}}}.
-
#value=(v) ⇒ Object
–}}}.
Methods included from Util
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
#coerce ⇒ Object
Returns the value of attribute coerce.
71 72 73 |
# File 'lib/parseargs.rb', line 71 def coerce @coerce end |
#convince ⇒ Object
Returns the value of attribute convince.
72 73 74 |
# File 'lib/parseargs.rb', line 72 def convince @convince end |
#default ⇒ Object
Returns the value of attribute default.
68 69 70 |
# File 'lib/parseargs.rb', line 68 def default @default end |
#ducktype ⇒ Object
Returns the value of attribute ducktype.
70 71 72 |
# File 'lib/parseargs.rb', line 70 def ducktype @ducktype end |
#name ⇒ Object
Returns the value of attribute name.
65 66 67 |
# File 'lib/parseargs.rb', line 65 def name @name end |
#required ⇒ Object Also known as: required?
Returns the value of attribute required.
66 67 68 |
# File 'lib/parseargs.rb', line 66 def required @required end |
#type ⇒ Object
Returns the value of attribute type.
69 70 71 |
# File 'lib/parseargs.rb', line 69 def type @type end |
#validate ⇒ Object
Returns the value of attribute validate.
73 74 75 |
# File 'lib/parseargs.rb', line 73 def validate @validate end |
Instance Method Details
#optional ⇒ Object Also known as: optional?
–}}}
151 152 153 154 155 |
# File 'lib/parseargs.rb', line 151 def optional #--{{{ not required #--}}} end |
#value ⇒ Object
–}}}
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 |