Class: HammerCLI::Options::Normalizers::KeyValueList

Inherits:
AbstractNormalizer show all
Defined in:
lib/hammer_cli/options/normalizers.rb

Instance Method Summary collapse

Methods inherited from AbstractNormalizer

#complete

Instance Method Details

#descriptionObject



25
26
27
# File 'lib/hammer_cli/options/normalizers.rb', line 25

def description
  _("Comma-separated list of key=value.")
end

#format(val) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# 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