class HammerCLI::Options::Normalizers::KeyValueList

Public Instance Methods

description() click to toggle source
# File lib/hammer_cli/options/normalizers.rb, line 25
def description
  _("Comma-separated list of key=value.")
end
format(val) click to toggle source
# File lib/hammer_cli/options/normalizers.rb, line 29
def format(val)
  return {} unless val.is_a?(String)
  return {} if val.empty?

  result = {}

  pair_re = '([^,=]+)=([^,\[]+|\[[^\[\]]*\])'
  full_re = "^((%s)[,]?)+$" % pair_re

  unless Regexp.new(full_re).match(val)
    raise ArgumentError, _("value must be defined as a comma-separated list of key=value")
  end

  val.scan(Regexp.new(pair_re)) do |key, value|
    value = value.strip
    value = value.scan(/[^,\[\]]+/) if value.start_with?('[')

    result[key.strip] = strip_value(value)
  end
  return result
end

Private Instance Methods

strip_chars(string, chars) click to toggle source
# File lib/hammer_cli/options/normalizers.rb, line 63
def strip_chars(string, chars)
  chars = Regexp.escape(chars)
  string.gsub(/\A[#{chars}]+|[#{chars}]+\z/, '')
end
strip_value(value) click to toggle source
# File lib/hammer_cli/options/normalizers.rb, line 53
def strip_value(value)
  if value.is_a? Array
    value.map do |item|
      strip_chars(item.strip, '"\')
    end
  else
    strip_chars(value.strip, '"\')
  end
end