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

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

Constant Summary collapse

FULL_RE =
'([^=,]+)=([\{\[][^\{\}\[\]]*[\}\]]|[^\0]+?)(?=,[^,]+=|$)'

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from AbstractNormalizer

#complete, #completion_type, #description, inherited

Class Method Details

.common_descriptionObject



60
61
62
63
# File 'lib/hammer_cli/options/normalizers.rb', line 60

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

.completion_typeObject



56
57
58
# File 'lib/hammer_cli/options/normalizers.rb', line 56

def completion_type
  :key_value_list
end

Instance Method Details

#format(val) ⇒ Object



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

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