lib/rprogram/option.rb in rprogram-0.3.1 vs lib/rprogram/option.rb in rprogram-0.3.2

- old
+ new

@@ -1,7 +1,9 @@ +require 'rprogram/argument' + module RProgram - class Option + class Option < Argument # Flag of the option attr_reader :flag # Is the option in equals format @@ -52,30 +54,28 @@ # values are allowed with the option. # def initialize(options={},&block) @flag = options[:flag] - @equals = (options[:equals] || false) - @multiple = (options[:multiple] || false) - @separator = if options[:separator] - options[:separator] - elsif options[:equals] - ' ' + @equals = (options[:equals] || false) + @multiple = (options[:multiple] || false) + @separator = if options[:separator] then options[:separator] + elsif options[:equals] then ' ' end @sub_options = (options[:sub_options] || false) @formatter = if block - block - else - Proc.new do |opt,value| - if opt.equals - ["#{opt.flag}=#{value.first}"] - else - [opt.flag] + value - end - end - end + block + else + Proc.new do |opt,value| + if opt.equals + ["#{opt.flag}=#{value.first}"] + else + [opt.flag] + value + end + end + end end # # Formats the arguments for the option. # @@ -84,41 +84,30 @@ # # @return [Array] # The formatted arguments of the option. # def arguments(value) - return [@flag] if value == true - return [] unless value + case value + when true + [@flag] + when false, nil + [] + else + value = super(value) - value = value.arguments if value.respond_to?(:arguments) + if @multiple + args = [] - if value.kind_of?(Hash) - value = value.map { |key,sub_value| - if sub_value == true - key.to_s - elsif sub_value - "#{key}=#{sub_value}" + value.each do |arg| + args += Array(@formatter.call(self,[arg])) end - } - elsif value.kind_of?(Array) - value.flatten! - else - value = [value] - end - value.compact! + return args + else + value = [value.join(@separator)] if @separator - if @multiple - return value.inject([]) do |args,value| - arg = @formatter.call(self,[value]) - - args += arg if arg - args + return Array(@formatter.call(self,value)) end - else - value = [value.join(@separator)] if @separator - - return (@formatter.call(self,value) || []) end end end end