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

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

Constant Summary collapse

PAIR_RE =
'([^,=]+)=([^,\{\[]+|[\{\[][^\{\}\[\]]*[\}\]])'
FULL_RE =
"^((%s)[,]?)+$" % PAIR_RE

Instance Method Summary collapse

Methods inherited from AbstractNormalizer

#complete

Instance Method Details

#descriptionObject



33
34
35
36
# File 'lib/hammer_cli/options/normalizers.rb', line 33

def description
  _("Comma-separated list of key=value.") + "\n" +
  _("JSON is acceptable and preferred way for complex parameters")
end

#format(val) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/hammer_cli/options/normalizers.rb', line 38

def format(val)
  return {} unless val.is_a?(String)
  return {} if val.empty?

  if valid_key_value?(val)
    parse_key_value(val)
  else
    begin
      formatter = JSONInput.new
      formatter.format(val)
    rescue ArgumentError
      raise ArgumentError, _("Value must be defined as a comma-separated list of key=value or valid JSON.")
    end
  end
end