Skip to content

Commit cb53d45

Browse files
authored
Merge pull request #775 from phene/support-range-in-enum-options
Support Range in enum option
2 parents 6d91576 + 5d97d47 commit cb53d45

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
@@ -660,7 +660,7 @@ def print_options(shell, options, group_name = nil)
660660

661661
list << item
662662
list << ["", "# Default: #{option.print_default}"] if option.show_default?
663-
list << ["", "# Possible values: #{option.enum.join(', ')}"] if option.enum
663+
list << ["", "# Possible values: #{option.enum_to_s}"] if option.enum
664664
end
665665

666666
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
@@ -52,11 +52,19 @@ def show_default?
5252
end
5353
end
5454

55+
def enum_to_s
56+
if enum.respond_to? :join
57+
enum.join(", ")
58+
else
59+
"#{enum.first}..#{enum.last}"
60+
end
61+
end
62+
5563
protected
5664

5765
def validate!
5866
raise ArgumentError, "An argument cannot be required and have default value." if required? && !default.nil?
59-
raise ArgumentError, "An argument cannot have an enum other than an array." if @enum && !@enum.is_a?(Array)
67+
raise ArgumentError, "An argument cannot have an enum other than an enumerable." if @enum && !@enum.is_a?(Enumerable)
6068
end
6169

6270
def valid_type?(type)

lib/thor/parser/arguments.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ def parse_numeric(name)
136136
value = $&.index(".") ? shift.to_f : shift.to_i
137137
if @switches.is_a?(Hash) && switch = @switches[name]
138138
if switch.enum && !switch.enum.include?(value)
139-
raise MalformattedArgumentError, "Expected '#{name}' to be one of #{switch.enum.join(', ')}; got #{value}"
139+
raise MalformattedArgumentError, "Expected '#{name}' to be one of #{switch.enum_to_s}; got #{value}"
140140
end
141141
end
142142
value
@@ -154,7 +154,7 @@ def parse_string(name)
154154
value = shift
155155
if @switches.is_a?(Hash) && switch = @switches[name]
156156
if switch.enum && !switch.enum.include?(value)
157-
raise MalformattedArgumentError, "Expected '#{name}' to be one of #{switch.enum.join(', ')}; got #{value}"
157+
raise MalformattedArgumentError, "Expected '#{name}' to be one of #{switch.enum_to_s}; got #{value}"
158158
end
159159
end
160160
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
@@ -529,11 +529,18 @@ def remaining
529529
"Expected numeric value for '-n'; got \"foo\"")
530530
end
531531

532-
it "raises error when value isn't in enum" do
532+
it "raises error when value isn't in Array enum" do
533533
enum = [1, 2]
534534
create limit: Thor::Option.new("limit", type: :numeric, enum: enum)
535535
expect { parse("--limit", "3") }.to raise_error(Thor::MalformattedArgumentError,
536-
"Expected '--limit' to be one of #{enum.join(', ')}; got 3")
536+
"Expected '--limit' to be one of 1, 2; got 3")
537+
end
538+
539+
it "raises error when value isn't in Range enum" do
540+
enum = 1..2
541+
create limit: Thor::Option.new("limit", type: :numeric, enum: enum)
542+
expect { parse("--limit", "3") }.to raise_error(Thor::MalformattedArgumentError,
543+
"Expected '--limit' to be one of 1..2; got 3")
537544
end
538545

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

0 commit comments

Comments
 (0)