lib/thor/options.rb in josevalim-thor-0.10.17 vs lib/thor/options.rb in josevalim-thor-0.10.18

- old
+ new

@@ -135,10 +135,16 @@ when SHORT_SQ_RE $1.split('').any? { |f| switch?("-#{f}") } end end + # Returns true if the next value exists and is not a switch. + # + def current_is_value? + peek && peek !~ /^-/ + end + # Check if the given argument matches with a switch. # def switch?(arg) switch_option(arg) || @shorts.key?(arg) end @@ -165,18 +171,22 @@ # def parse_option(switch, option) @non_assigned_required.delete(option) type = if option.type == :default - peek.nil? || peek.to_s =~ /^-/ ? :boolean : :string + current_is_value? ? :string : :boolean else option.type end case type when :boolean - @switches.key?(switch) || switch !~ /^--(no|skip)-([-\w]+)$/ + if current_is_value? + shift == "true" + else + @switches.key?(switch) || switch !~ /^--(no|skip)-([-\w]+)$/ + end when :string shift when :numeric parse_numeric(switch) when :hash @@ -196,11 +206,11 @@ # { "name" => "string", "age" => "integer" } # def parse_hash hash = {} - while peek && peek !~ /^\-/ + while current_is_value? && peek.include?(?:) key, value = shift.split(':') hash[key] = value end hash @@ -216,11 +226,11 @@ # ["a", "b", "c"] # def parse_array array = [] - while peek && peek !~ /^\-/ + while current_is_value? array << shift end array end @@ -238,10 +248,10 @@ # Raises an error if the option requires an input but it's not present. # def check_requirement!(switch, option) if option.input_required? raise RequiredArgumentMissingError, "no value provided for required argument '#{switch}'" if peek.nil? - raise MalformattedArgumentError, "cannot pass switch '#{peek}' as an argument" if switch?(peek) + raise MalformattedArgumentError, "cannot pass switch '#{peek}' as an argument" unless current_is_value? end end # Raises an error if @required array is not empty after parsing. #