Skip to content

Commit fb14b99

Browse files
committed
Refactor enum validation to avoid code duplication
1 parent 0e05616 commit fb14b99

File tree

2 files changed

+27
-15
lines changed

2 files changed

+27
-15
lines changed

lib/thor/parser/arguments.rb

Lines changed: 26 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -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!

spec/parser/options_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -512,7 +512,7 @@ def remaining
512512

513513
it "raises error when value isn't in enum" do
514514
enum = %w(apple banana)
515-
create :fruit => Thor::Option.new("fruits", :type => :array, :enum => enum)
515+
create fruit: Thor::Option.new("fruits", type: :array, enum: enum)
516516
expect { parse("--fruits=", "apple", "banana", "strawberry") }.to raise_error(Thor::MalformattedArgumentError,
517517
"Expected all values of '--fruits' to be one of #{enum.join(', ')}; got strawberry")
518518
end

0 commit comments

Comments
 (0)