class OptParseValidator::OptBase

Base Option This Option should not be called, children should be used.

Attributes

attrs[R]
option[R]
required[W]

Public Class Methods

new(option, attrs = {}) click to toggle source

@param [ Array ] option See OptionParser#on @param [ Hash ] attrs @option attrs [ Boolean ] :required @options attrs [ Array<Symbol>, Symbol ] :required_unless @option attrs [ Mixed ] :default The default value to use if the option is not supplied @option attrs [ Mixed ] :value_if_empty The value to use if no argument has been supplied @option attrs [ Array<Symbol> ] :normalize See normalize

@note The :default and :normalize ‘logics’ are done in OptParseValidator::OptParser#add_option

# File lib/opt_parse_validator/opts/base.rb, line 19
def initialize(option, attrs = {})
  @option = option
  @attrs  = attrs

  # TODO: incompatible attributes, ie required and require_unless at the same time

  append_help_messages
end

Public Instance Methods

advanced?() click to toggle source

@return [ Boolean ]

# File lib/opt_parse_validator/opts/base.rb, line 70
def advanced?
  attrs[:advanced] ? true : false
end
alias?() click to toggle source

@return [ Boolean ]

# File lib/opt_parse_validator/opts/base.rb, line 65
def alias?
  false
end
append_help_messages() click to toggle source

@return [ Void ]

# File lib/opt_parse_validator/opts/base.rb, line 29
def append_help_messages
  option << "Default: #{help_message_for_default}" if default
  option << "Value if no argument supplied: #{value_if_empty}" if value_if_empty
  option << 'This option is mandatory' if required?
  option << "This option is mandatory unless #{required_unless.join(' or ')} is/are supplied" unless required_unless.empty?
end
choices() click to toggle source

@return [ Array<Mixed> ]

# File lib/opt_parse_validator/opts/base.rb, line 55
def choices
  attrs[:choices]
end
default() click to toggle source

@return [ Mixed ]

# File lib/opt_parse_validator/opts/base.rb, line 50
def default
  attrs[:default]
end
help_message_for_default() click to toggle source
# File lib/opt_parse_validator/opts/base.rb, line 36
def help_message_for_default
  default.to_s
end
help_messages() click to toggle source

@return [ Array<String> ]

# File lib/opt_parse_validator/opts/base.rb, line 132
def help_messages
  first_message_index = option.index { |e| e[0] != '-' }

  return [] unless first_message_index

  option[first_message_index..-1]
end
normalize(value) click to toggle source

Apply each methods from attrs to the value if possible User input should not be used in this attrs

e.g: normalize: :to_sym will return the symbol of the value

normalize: [:to_sym, :upcase] Will return the upercased symbol

@param [ Mixed ] value

@return [ Mixed ]

# File lib/opt_parse_validator/opts/base.rb, line 93
def normalize(value)
  Array(attrs[:normalize]).each do |method|
    next unless method.is_a?(Symbol)

    value = value.send(method) if value.respond_to?(method)
  end

  value
end
required?() click to toggle source

@return [ Boolean ]

# File lib/opt_parse_validator/opts/base.rb, line 41
def required?
  @required ||= attrs[:required]
end
required_unless() click to toggle source
# File lib/opt_parse_validator/opts/base.rb, line 45
def required_unless
  @required_unless ||= Array(attrs[:required_unless])
end
to_long() click to toggle source

@return [ String ] The raw long option (e.g: –proxy)

# File lib/opt_parse_validator/opts/base.rb, line 116
def to_long
  option.each do |option_attr|
    if option_attr.start_with?('--')
      return option_attr.gsub(/ .*$/, '')
                        .gsub(/\[[^\]]+\]/, '')
    end
  end
  nil
end
to_s() click to toggle source

@return [ String ]

# File lib/opt_parse_validator/opts/base.rb, line 127
def to_s
  to_sym.to_s
end
to_sym() click to toggle source

@return [ Symbol ]

# File lib/opt_parse_validator/opts/base.rb, line 104
def to_sym
  unless @symbol
    long_option = to_long

    raise Error, "Could not find option symbol for #{option}" unless long_option

    @symbol = long_option.delete_prefix('--').tr('-', '_').to_sym
  end
  @symbol
end
validate(value) click to toggle source

@param [ String ] value

# File lib/opt_parse_validator/opts/base.rb, line 75
def validate(value)
  if value.nil? || value.to_s.empty?
    raise Error, 'Empty option value supplied' if value_if_empty.nil?

    return value_if_empty
  end
  value
end
value_if_empty() click to toggle source

@return [ Mixed ]

# File lib/opt_parse_validator/opts/base.rb, line 60
def value_if_empty
  attrs[:value_if_empty]
end