Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support Range in enum option #775

Merged
merged 1 commit into from
May 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 && [email protected]_a?(Array)
raise ArgumentError, "An argument cannot have an enum other than an enumerable." if @enum && [email protected]_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