Permalink
Browse files

support casting operations that replace the entire set of values

  • Loading branch information...
1 parent 758eb61 commit a181fe354c19c96d81099eefabb8684ff716f135 @ahoward committed Jul 29, 2010
Showing with 20 additions and 16 deletions.
  1. +1 −1 lib/main.rb
  2. +8 −10 lib/main/parameter.rb
  3. +11 −5 test/main.rb
View
@@ -2,7 +2,7 @@ module Main
#
# top level constants
#
- Main::VERSION = '4.2.0' unless
+ Main::VERSION = '4.3.0' unless
defined? Main::VERSION
def self.version() Main::VERSION end
View
@@ -184,14 +184,6 @@ def check_arity
arity = min
-=begin
-puts
-p :values => values
-p :arity => arity
-p :argument_required => argument_required?
-p :argument_none => argument_none?
-puts
-=end
if values.size < arity
if argument_optional?
raise ex, "#{ typename }) #{ values.size }/#{ sign }#{ arity }" if(values.size < arity and values.size > 0)
@@ -204,8 +196,14 @@ def check_arity
def apply_casting
if cast?
op = cast.respond_to?('call') ? cast : Cast[cast]
- values.map! do |val|
- Parameter.wrap_errors{ op.call val }
+ case op.arity
+ when -1
+ replacement = Parameter.wrap_errors{ op.call(*values) }
+ values.replace(replacement)
+ else
+ values.map! do |val|
+ Parameter.wrap_errors{ op.call val }
+ end
end
end
end
View
@@ -230,7 +230,7 @@ def test_0140
def test_0150
param = nil
assert_nothing_raised{
- main(%w[--foo=42 --bar=42.0 --foobar=true --barfoo=false --uri=http://foo --x=s]){
+ main(%w[--foo=42 --bar=42.0 --foobar=true --barfoo=false --uri=http://foo --x=s --y=a,b,c]){
option('foo'){
required
argument_required
@@ -256,15 +256,21 @@ def test_0150
argument_required
cast{|x| x.to_s.upcase}
}
+ option('y'){
+ argument_required
+ process{|*values| values.join.split(',').map{|value| value.upcase}}
+ }
define_method('run'){ param = params }
}
}
assert param['foo'].value == 42
assert param['bar'].value == 42.0
- assert param['foobar'].value == true
- assert param['barfoo'].value == 'false'
- assert param['uri'].value == URI.parse('http://foo')
- assert param['x'].value == 'S'
+ assert param['foobar'].value == true
+ assert param['barfoo'].value == 'false'
+ assert param['uri'].value == URI.parse('http://foo')
+ assert param['x'].value == 'S'
+ assert param['y'].value == 'A'
+ assert param['y'].values == %w( A B C )
end
def test_0160
p = nil

0 comments on commit a181fe3

Please sign in to comment.