lib/hammer_cli/options/normalizers.rb in hammer_cli-0.8.0 vs lib/hammer_cli/options/normalizers.rb in hammer_cli-0.9.0

- old
+ new

@@ -1,6 +1,7 @@ require 'json' +require 'csv' module HammerCLI module Options module Normalizers @@ -76,18 +77,28 @@ chars = Regexp.escape(chars) string.gsub(/\A[#{chars}]+|[#{chars}]+\z/, '') end end + CSV_ERROR_MESSAGES = { + /Missing or stray quote/ => _('Missing or stray quote.'), + /Unquoted fields do not allow/ => _('Unquoted fields do not allow \r or \n.'), + /Illegal quoting/ => _('Illegal quoting.'), + /Unclosed quoted field/ => _('Unclosed quoted field.'), + /Field size exceeded/ => _('Field size exceeded.') + } class List < AbstractNormalizer def description - _("Comma separated list of values.") + _("Comma separated list of values. Values containing comma should be double quoted") end def format(val) - val.is_a?(String) ? val.split(",") : [] + (val.is_a?(String) && !val.empty?) ? CSV.parse_line(val) : [] + rescue CSV::MalformedCSVError => e + message = CSV_ERROR_MESSAGES.find { |pattern,| pattern.match e.message } || [e.message] + raise ArgumentError.new(message.last) end end class Number < AbstractNormalizer