Sha256: 71d28e9429911c68246c74bc58af710c84ec1c0b66625228fc0bcfcd5c902fa1
Contents?: true
Size: 1.16 KB
Versions: 12
Compression:
Stored size: 1.16 KB
Contents
# frozen_string_literal: true module OptParseValidator # Implementation of the Choice Option class OptChoice < OptBase # @param [ Array ] option See OptBase#new # @param [ Hash ] attrs # :choices [ Array ] The available choices (mandatory) # :case_sensitive [ Boolean ] Default: false def initialize(option, attrs = {}) raise Error, 'The :choices attribute is mandatory' unless attrs.key?(:choices) raise Error, 'The :choices attribute must be an array' unless attrs[:choices].is_a?(Array) super(option, attrs) end # @return [ Void ] def append_help_messages super option << "Available choices: #{choices.join(', ')}" end # @return [ String ] # If :case_sensitive if false (or nil), the downcased value of the choice # will be returned def validate(value) value = +value.to_s unless attrs[:case_sensitive] value.downcase! choices.map!(&:downcase) end unless choices.include?(value) raise Error, "'#{value}' is not a valid choice, expected one " \ "of the followings: #{choices.join(',')}" end value end end end
Version data entries
12 entries across 12 versions & 1 rubygems