@@ -117,14 +117,16 @@ def parse_hash(name)
117117 #
118118 def parse_array ( name )
119119 return shift if peek . is_a? ( Array )
120+
120121 array = [ ]
122+
121123 while current_is_value?
122124 value = shift
123- if !value . empty? && @switches . is_a? ( Hash ) && switch = @switches [ name ]
124- if switch . enum && !switch . enum . include? ( value )
125- raise MalformattedArgumentError , "Expected all values of '#{ name } ' to be one of #{ switch . enum_to_s } ; got #{ value } "
126- end
125+
126+ if !value . empty?
127+ validate_enum_value! ( name , value , "Expected all values of '%s' to be one of %s; got %s" )
127128 end
129+
128130 array << value
129131 end
130132 array
@@ -142,11 +144,9 @@ def parse_numeric(name)
142144 end
143145
144146 value = $&. index ( "." ) ? shift . to_f : shift . to_i
145- if @switches . is_a? ( Hash ) && switch = @switches [ name ]
146- if switch . enum && !switch . enum . include? ( value )
147- raise MalformattedArgumentError , "Expected '#{ name } ' to be one of #{ switch . enum_to_s } ; got #{ value } "
148- end
149- end
147+
148+ validate_enum_value! ( name , value , "Expected '%s' to be one of %s; got %s" )
149+
150150 value
151151 end
152152
@@ -160,15 +160,27 @@ def parse_string(name)
160160 nil
161161 else
162162 value = shift
163- if @switches . is_a? ( Hash ) && switch = @switches [ name ]
164- if switch . enum && !switch . enum . include? ( value )
165- raise MalformattedArgumentError , "Expected '#{ name } ' to be one of #{ switch . enum_to_s } ; got #{ value } "
166- end
167- end
163+
164+ validate_enum_value! ( name , value , "Expected '%s' to be one of %s; got %s" )
165+
168166 value
169167 end
170168 end
171169
170+ # Raises an error if the switch is an enum and the values aren't included on it.
171+ #
172+ def validate_enum_value! ( name , value , message )
173+ return unless @switches . is_a? ( Hash )
174+
175+ switch = @switches [ name ]
176+
177+ return unless switch
178+
179+ if switch . enum && !switch . enum . include? ( value )
180+ raise MalformattedArgumentError , message % [ name , switch . enum_to_s , value ]
181+ end
182+ end
183+
172184 # Raises an error if @non_assigned_required array is not empty.
173185 #
174186 def check_requirement!
0 commit comments