class OptParseValidator::OptBase
Base Option This Option should not be called, children should be used.
Attributes
Public Class Methods
@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
@return [ Boolean ]
# File lib/opt_parse_validator/opts/base.rb, line 70 def advanced? attrs[:advanced] ? true : false end
@return [ Boolean ]
# File lib/opt_parse_validator/opts/base.rb, line 65 def alias? false end
@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
@return [ Array<Mixed> ]
# File lib/opt_parse_validator/opts/base.rb, line 55 def choices attrs[:choices] end
@return [ Mixed ]
# File lib/opt_parse_validator/opts/base.rb, line 50 def default attrs[:default] end
# File lib/opt_parse_validator/opts/base.rb, line 36 def help_message_for_default default.to_s end
@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
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
@return [ Boolean ]
# File lib/opt_parse_validator/opts/base.rb, line 41 def required? @required ||= attrs[:required] end
# File lib/opt_parse_validator/opts/base.rb, line 45 def required_unless @required_unless ||= Array(attrs[:required_unless]) end
@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
@return [ String ]
# File lib/opt_parse_validator/opts/base.rb, line 127 def to_s to_sym.to_s end
@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
@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
@return [ Mixed ]
# File lib/opt_parse_validator/opts/base.rb, line 60 def value_if_empty attrs[:value_if_empty] end