lib/hammer_cli/options/normalizers.rb in hammer_cli-3.0.2 vs lib/hammer_cli/options/normalizers.rb in hammer_cli-3.1.0

- old
+ new

@@ -2,12 +2,32 @@ require 'hammer_cli/csv_parser' module HammerCLI module Options module Normalizers + def self.available + AbstractNormalizer.available + end class AbstractNormalizer + class << self + attr_reader :available + + def inherited(subclass) + @available ||= [] + @available << subclass + end + + def completion_type + :value + end + + def common_description + _("Value described in the option's description. Mostly simple string") + end + end + def description "" end def format(val) @@ -15,10 +35,14 @@ end def complete(val) [] end + + def completion_type + { type: self.class.completion_type } + end end class Default < AbstractNormalizer def format(value) value @@ -28,13 +52,19 @@ class KeyValueList < AbstractNormalizer PAIR_RE = '([^,=]+)=([^,\{\[]+|[\{\[][^\{\}\[\]]*[\}\]])' FULL_RE = "^((%s)[,]?)+$" % PAIR_RE - def description - _("Comma-separated list of key=value.") + "\n" + - _("JSON is acceptable and preferred way for complex parameters") + class << self + def completion_type + :key_value_list + end + + def common_description + _('Comma-separated list of key=value.') + "\n" + + _('JSON is acceptable and preferred way for such parameters') + end end def format(val) return {} unless val.is_a?(String) return {} if val.empty? @@ -92,13 +122,20 @@ end end class List < AbstractNormalizer - def description - _("Comma separated list of values. Values containing comma should be quoted or escaped with backslash.") + "\n" + - _("JSON is acceptable and preferred way for complex parameters") + class << self + def completion_type + :list + end + + def common_description + _('Comma separated list of values. Values containing comma should be quoted or escaped with backslash.') + + "\n" + + _('JSON is acceptable and preferred way for such parameters') + end end def format(val) return [] unless val.is_a?(String) && !val.empty? begin @@ -108,10 +145,22 @@ end end end class ListNested < AbstractNormalizer + class << self + def completion_type + :schema + end + + def common_description + _('Comma separated list of values defined by a schema.') + + "\n" + + _('JSON is acceptable and preferred way for such parameters') + end + end + class Schema < Array def description(richtext: true) '"' + reduce([]) do |schema, nested_param| name = nested_param.name name = HighLine.color(name, :bold) if nested_param.required? && richtext @@ -133,15 +182,10 @@ def initialize(schema) @schema = Schema.new(schema) end - def description - _("Comma separated list of values defined by a schema. See Option details section below.") + "\n" + - _("JSON is acceptable and preferred way for complex parameters") - end - def format(val) return [] unless val.is_a?(String) && !val.empty? begin JSON.parse(val) rescue JSON::ParserError @@ -150,14 +194,27 @@ results << KeyValueList.new.format(item) end end end + + def completion_type + super.merge({ schema: schema.description(richtext: false) }) + end end class Number < AbstractNormalizer + class << self + def completion_type + :number + end + def common_description + _('Numeric value. Integer') + end + end + def format(val) if numeric?(val) val.to_i else raise ArgumentError, _("Numeric value is required.") @@ -165,23 +222,28 @@ end def numeric?(val) Integer(val) != nil rescue false end - end class Bool < AbstractNormalizer + class << self + def completion_type + :boolean + end + + def common_description + _('One of %s') % ['true/false', 'yes/no', '1/0'].join(', ') + end + end + def allowed_values ['yes', 'no', 'true', 'false', '1', '0'] end - def description - _('One of %s.') % ['true/false', 'yes/no', '1/0'].join(', ') - end - def format(bool) bool = bool.to_s if bool.downcase.match(/^(true|t|yes|y|1)$/i) return true elsif bool.downcase.match(/^(false|f|no|n|0)$/i) @@ -192,15 +254,28 @@ end def complete(value) allowed_values.map { |v| v + ' ' } end + + def completion_type + super.merge({ values: allowed_values }) + end end class File < AbstractNormalizer + class << self + def completion_type + :file + end + def common_description + _('Path to a file') + end + end + def format(path) ::File.read(::File.expand_path(path)) end def complete(value) @@ -231,10 +306,20 @@ end class Enum < AbstractNormalizer + class << self + def completion_type + :enum + end + + def common_description + _("Possible values are described in the option's description") + end + end + attr_reader :allowed_values def initialize(allowed_values) @allowed_values = allowed_values end @@ -258,22 +343,31 @@ def complete(value) Completer::finalize_completions(@allowed_values) end + def completion_type + super.merge({ values: allowed_values }) + end + private def quoted_values @allowed_values.map { |v| "'#{v}'" }.join(', ') end end class DateTime < AbstractNormalizer + class << self + def completion_type + :datetime + end - def description - _("Date and time in YYYY-MM-DD HH:MM:SS or ISO 8601 format") + def common_description + _('Date and time in YYYY-MM-DD HH:MM:SS or ISO 8601 format') + end end def format(date) raise ArgumentError unless date ::DateTime.parse(date).to_s @@ -281,10 +375,20 @@ raise ArgumentError, _("'%s' is not a valid date.") % date end end class EnumList < AbstractNormalizer + class << self + def completion_type + :multienum + end + + def common_description + _("Any combination of possible values described in the option's description") + end + end + attr_reader :allowed_values def initialize(allowed_values) @allowed_values = allowed_values end @@ -297,9 +401,13 @@ value.is_a?(String) ? parse(value) : [] end def complete(value) Completer::finalize_completions(@allowed_values) + end + + def completion_type + super.merge({ values: allowed_values }) end private def quoted_values