lib/thor/parser/arguments.rb in thor-0.19.1 vs lib/thor/parser/arguments.rb in thor-0.19.2
- old
+ new
@@ -1,8 +1,8 @@
class Thor
class Arguments #:nodoc: # rubocop:disable ClassLength
- NUMERIC = /(\d*\.\d+|\d+)/
+ NUMERIC = /[-+]?(\d*\.\d+|\d+)/
# Receives an array of args and returns two arrays, one with arguments
# and one with switches.
#
def self.split(args)
@@ -22,11 +22,12 @@
end
# Takes an array of Thor::Argument objects.
#
def initialize(arguments = [])
- @assigns, @non_assigned_required = {}, []
+ @assigns = {}
+ @non_assigned_required = []
@switches = arguments
arguments.each do |argument|
if !argument.default.nil?
@assigns[argument.human_name] = argument.default
@@ -47,11 +48,11 @@
check_requirement!
@assigns
end
- def remaining # rubocop:disable TrivialAccessors
+ def remaining
@pile
end
private
@@ -71,11 +72,11 @@
def shift
@pile.shift
end
def unshift(arg)
- if arg.kind_of?(Array)
+ if arg.is_a?(Array)
@pile = arg + @pile
else
@pile.unshift(arg)
end
end
@@ -97,10 +98,11 @@
return shift if peek.is_a?(Hash)
hash = {}
while current_is_value? && peek.include?(":")
key, value = shift.split(":", 2)
+ raise MalformattedArgumentError, "You can't specify '#{key}' more than once in option '#{name}'; got #{key}:#{hash[key]} and #{key}:#{value}" if hash.include? key
hash[key] = value
end
hash
end
@@ -126,17 +128,17 @@
#
def parse_numeric(name)
return shift if peek.is_a?(Numeric)
unless peek =~ NUMERIC && $& == peek
- fail MalformattedArgumentError, "Expected numeric value for '#{name}'; got #{peek.inspect}"
+ raise MalformattedArgumentError, "Expected numeric value for '#{name}'; got #{peek.inspect}"
end
value = $&.index(".") ? shift.to_f : shift.to_i
if @switches.is_a?(Hash) && switch = @switches[name]
if switch.enum && !switch.enum.include?(value)
- fail MalformattedArgumentError, "Expected '#{name}' to be one of #{switch.enum.join(', ')}; got #{value}"
+ raise MalformattedArgumentError, "Expected '#{name}' to be one of #{switch.enum.join(', ')}; got #{value}"
end
end
value
end
@@ -148,28 +150,26 @@
def parse_string(name)
if no_or_skip?(name)
nil
else
value = shift
- if @switches.is_a?(Hash) && switch = @switches[name] # rubocop:disable AssignmentInCondition
+ if @switches.is_a?(Hash) && switch = @switches[name]
if switch.enum && !switch.enum.include?(value)
- fail MalformattedArgumentError, "Expected '#{name}' to be one of #{switch.enum.join(', ')}; got #{value}"
+ raise MalformattedArgumentError, "Expected '#{name}' to be one of #{switch.enum.join(', ')}; got #{value}"
end
end
value
end
end
# Raises an error if @non_assigned_required array is not empty.
#
def check_requirement!
- unless @non_assigned_required.empty?
- names = @non_assigned_required.map do |o|
- o.respond_to?(:switch_name) ? o.switch_name : o.human_name
- end.join("', '")
-
- class_name = self.class.name.split("::").last.downcase
- fail RequiredArgumentMissingError, "No value provided for required #{class_name} '#{names}'"
- end
+ return if @non_assigned_required.empty?
+ names = @non_assigned_required.map do |o|
+ o.respond_to?(:switch_name) ? o.switch_name : o.human_name
+ end.join("', '")
+ class_name = self.class.name.split("::").last.downcase
+ raise RequiredArgumentMissingError, "No value provided for required #{class_name} '#{names}'"
end
end
end