Skip to content

Commit 4a511fe

Browse files
author
Geoffrey Hichborn
committed
Support Range in enum option
1 parent ab3b5be commit 4a511fe

File tree

5 files changed

+23
-8
lines changed

5 files changed

+23
-8
lines changed

lib/thor/base.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -567,7 +567,7 @@ def print_options(shell, options, group_name = nil)
567567

568568
list << item
569569
list << ["", "# Default: #{option.default}"] if option.show_default?
570-
list << ["", "# Possible values: #{option.enum.join(', ')}"] if option.enum
570+
list << ["", "# Possible values: #{option.enum_to_s}"] if option.enum
571571
end
572572

573573
shell.say(group_name ? "#{group_name} options:" : "Options:")

lib/thor/parser/argument.rb

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,11 +41,19 @@ def show_default?
4141
end
4242
end
4343

44+
def enum_to_s
45+
if enum.respond_to? :join
46+
enum.join(', ')
47+
else
48+
"#{enum.first}..#{enum.last}"
49+
end
50+
end
51+
4452
protected
4553

4654
def validate!
4755
raise ArgumentError, "An argument cannot be required and have default value." if required? && !default.nil?
48-
raise ArgumentError, "An argument cannot have an enum other than an array." if @enum && !@enum.is_a?(Array)
56+
raise ArgumentError, "An argument cannot have an enum other than an enumerable." if @enum && !@enum.is_a?(Enumerable)
4957
end
5058

5159
def valid_type?(type)

lib/thor/parser/arguments.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ def parse_numeric(name)
140140
value = $&.index(".") ? shift.to_f : shift.to_i
141141
if @switches.is_a?(Hash) && switch = @switches[name]
142142
if switch.enum && !switch.enum.include?(value)
143-
raise MalformattedArgumentError, "Expected '#{name}' to be one of #{switch.enum.join(', ')}; got #{value}"
143+
raise MalformattedArgumentError, "Expected '#{name}' to be one of #{switch.enum_to_s}; got #{value}"
144144
end
145145
end
146146
value
@@ -158,7 +158,7 @@ def parse_string(name)
158158
value = shift
159159
if @switches.is_a?(Hash) && switch = @switches[name]
160160
if switch.enum && !switch.enum.include?(value)
161-
raise MalformattedArgumentError, "Expected '#{name}' to be one of #{switch.enum.join(', ')}; got #{value}"
161+
raise MalformattedArgumentError, "Expected '#{name}' to be one of #{switch.enum_to_s}; got #{value}"
162162
end
163163
end
164164
value

spec/parser/argument_spec.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,10 @@ def argument(name, options = {})
2525
end.to raise_error(ArgumentError, "An argument cannot be required and have default value.")
2626
end
2727

28-
it "raises an error if enum isn't an array" do
28+
it "raises an error if enum isn't enumerable" do
2929
expect do
3030
argument(:command, :type => :string, :enum => "bar")
31-
end.to raise_error(ArgumentError, "An argument cannot have an enum other than an array.")
31+
end.to raise_error(ArgumentError, "An argument cannot have an enum other than an enumerable.")
3232
end
3333
end
3434

spec/parser/options_spec.rb

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -463,11 +463,18 @@ def remaining
463463
"Expected numeric value for '-n'; got \"foo\"")
464464
end
465465

466-
it "raises error when value isn't in enum" do
466+
it "raises error when value isn't in Array enum" do
467467
enum = [1, 2]
468468
create :limit => Thor::Option.new("limit", :type => :numeric, :enum => enum)
469469
expect { parse("--limit", "3") }.to raise_error(Thor::MalformattedArgumentError,
470-
"Expected '--limit' to be one of #{enum.join(', ')}; got 3")
470+
"Expected '--limit' to be one of 1, 2; got 3")
471+
end
472+
473+
it "raises error when value isn't in Range enum" do
474+
enum = 1..2
475+
create :limit => Thor::Option.new("limit", :type => :numeric, :enum => enum)
476+
expect { parse("--limit", "3") }.to raise_error(Thor::MalformattedArgumentError,
477+
"Expected '--limit' to be one of 1..2; got 3")
471478
end
472479

473480
it "allows multiple values if repeatable is specified" do

0 commit comments

Comments
 (0)