Skip to content

Commit

Permalink
Support Range in enum option
Browse files Browse the repository at this point in the history
  • Loading branch information
Geoffrey Hichborn authored and rafaelfranca committed May 12, 2023
1 parent 6d91576 commit 5d97d47
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 8 deletions.
2 changes: 1 addition & 1 deletion lib/thor/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -660,7 +660,7 @@ def print_options(shell, options, group_name = nil)

list << item
list << ["", "# Default: #{option.print_default}"] if option.show_default?
list << ["", "# Possible values: #{option.enum.join(', ')}"] if option.enum
list << ["", "# Possible values: #{option.enum_to_s}"] if option.enum
end

shell.say(group_name ? "#{group_name} options:" : "Options:")
Expand Down
10 changes: 9 additions & 1 deletion lib/thor/parser/argument.rb
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,19 @@ def show_default?
end
end

def enum_to_s
if enum.respond_to? :join
enum.join(", ")
else
"#{enum.first}..#{enum.last}"
end
end

protected

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

def valid_type?(type)
Expand Down
4 changes: 2 additions & 2 deletions lib/thor/parser/arguments.rb
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ def parse_numeric(name)
value = $&.index(".") ? shift.to_f : shift.to_i
if @switches.is_a?(Hash) && switch = @switches[name]
if switch.enum && !switch.enum.include?(value)
raise MalformattedArgumentError, "Expected '#{name}' to be one of #{switch.enum.join(', ')}; got #{value}"
raise MalformattedArgumentError, "Expected '#{name}' to be one of #{switch.enum_to_s}; got #{value}"
end
end
value
Expand All @@ -154,7 +154,7 @@ def parse_string(name)
value = shift
if @switches.is_a?(Hash) && switch = @switches[name]
if switch.enum && !switch.enum.include?(value)
raise MalformattedArgumentError, "Expected '#{name}' to be one of #{switch.enum.join(', ')}; got #{value}"
raise MalformattedArgumentError, "Expected '#{name}' to be one of #{switch.enum_to_s}; got #{value}"
end
end
value
Expand Down
4 changes: 2 additions & 2 deletions spec/parser/argument_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,10 @@ def argument(name, options = {})
end.to raise_error(ArgumentError, "An argument cannot be required and have default value.")
end

it "raises an error if enum isn't an array" do
it "raises an error if enum isn't enumerable" do
expect do
argument(:command, type: :string, enum: "bar")
end.to raise_error(ArgumentError, "An argument cannot have an enum other than an array.")
end.to raise_error(ArgumentError, "An argument cannot have an enum other than an enumerable.")
end
end

Expand Down
11 changes: 9 additions & 2 deletions spec/parser/options_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -529,11 +529,18 @@ def remaining
"Expected numeric value for '-n'; got \"foo\"")
end

it "raises error when value isn't in enum" do
it "raises error when value isn't in Array enum" do
enum = [1, 2]
create limit: Thor::Option.new("limit", type: :numeric, enum: enum)
expect { parse("--limit", "3") }.to raise_error(Thor::MalformattedArgumentError,
"Expected '--limit' to be one of #{enum.join(', ')}; got 3")
"Expected '--limit' to be one of 1, 2; got 3")
end

it "raises error when value isn't in Range enum" do
enum = 1..2
create limit: Thor::Option.new("limit", type: :numeric, enum: enum)
expect { parse("--limit", "3") }.to raise_error(Thor::MalformattedArgumentError,
"Expected '--limit' to be one of 1..2; got 3")
end

it "allows multiple values if repeatable is specified" do
Expand Down

0 comments on commit 5d97d47

Please sign in to comment.