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

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from AbstractNormalizer

#complete, #completion_type, #description, inherited

Class Method Details

.common_descriptionObject



62
63
64
65
# File 'lib/hammer_cli/options/normalizers.rb', line 62

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

.completion_typeObject



58
59
60
# File 'lib/hammer_cli/options/normalizers.rb', line 58

def completion_type
  :key_value_list
end

Instance Method Details

#format(val) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/hammer_cli/options/normalizers.rb', line 68

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