lib/thor/parser/options.rb in josevalim-thor-0.10.21 vs lib/thor/parser/options.rb in josevalim-thor-0.10.22

- old
+ new

@@ -28,27 +28,26 @@ end.join(" ") end # Takes a hash of Thor::Option objects. # - def initialize(switches={}) - @shorts, @non_assigned_required = {}, [] + def initialize(options={}) + options = options.values + super(options) + @shorts, @switches = {}, {} - @switches = switches.values.inject({}) do |mem, option| - @non_assigned_required << option if option.required? + options.each do |option| + @switches[option.switch_name] = option option.aliases.each do |short| @shorts[short.to_s] ||= option.switch_name end - - mem[option.switch_name] = option - mem end end def parse(args) - @pile, options = args.dup, {} + @pile = args.dup while peek if current_is_switch? case shift when SHORT_SQ_RE @@ -62,23 +61,18 @@ end switch = normalize_switch(switch) next unless option = switch_option(switch) - if option.input_required? - raise RequiredArgumentMissingError, "no value provided for required option '#{switch}'" if peek.nil? - raise MalformattedArgumentError, "cannot pass switch '#{peek}' as an argument" unless current_is_value? - end - - options[option.human_name] = parse_peek(switch, option) + @assigns[option.human_name] = parse_peek(switch, option) else shift end end check_requirement! - options + @assigns end protected # Returns true if the current value in peek is a registered switch. @@ -95,17 +89,22 @@ def switch?(arg) switch_option(arg) || @shorts.key?(arg) end def switch_option(arg) - if arg =~ /^--(no|skip)-([-\w]+)$/ - @switches[arg] || @switches["--#{$2}"] + if match = no_or_skip?(arg) + @switches[arg] || @switches["--#{match}"] else @switches[arg] end end + def no_or_skip?(arg) + arg =~ /^--(no|skip)-([-\w]+)$/ + $2 + end + # Check if the given argument is actually a shortcut. # def normalize_switch(arg) @shorts.key?(arg) ? @shorts[arg] : arg end @@ -114,27 +113,23 @@ # def parse_boolean(switch) if current_is_value? ["true", "TRUE", "t", "T", true].include?(shift) else - @switches.key?(switch) || switch !~ /^--(no|skip)-([-\w]+)$/ + @switches.key?(switch) || !no_or_skip?(switch) end end - # Receives switch, option and the current values hash and assign the next - # value to it. Also removes the option from the array where non assigned - # required are kept. + # Parse the value at the peek analyzing if it requires an input or not. # def parse_peek(switch, option) - @non_assigned_required.delete(option) - - type = if option.type == :default - current_is_value? ? :string : :boolean - else - option.type + if option.input_required? + return nil if no_or_skip?(switch) + raise MalformattedArgumentError, "no value provided for option '#{switch}'" unless current_is_value? end - send(:"parse_#{type}", switch) + @non_assigned_required.delete(option) + send(:"parse_#{option.type}", switch) end end end