@@ -117,14 +117,16 @@ def parse_hash(name)
117
117
#
118
118
def parse_array ( name )
119
119
return shift if peek . is_a? ( Array )
120
+
120
121
array = [ ]
122
+
121
123
while current_is_value?
122
124
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" )
127
128
end
129
+
128
130
array << value
129
131
end
130
132
array
@@ -142,11 +144,9 @@ def parse_numeric(name)
142
144
end
143
145
144
146
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
+
150
150
value
151
151
end
152
152
@@ -160,15 +160,27 @@ def parse_string(name)
160
160
nil
161
161
else
162
162
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
+
168
166
value
169
167
end
170
168
end
171
169
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
+
172
184
# Raises an error if @non_assigned_required array is not empty.
173
185
#
174
186
def check_requirement!
0 commit comments