Class: OptParseValidator::OptBase

Inherits:
Object
  • Object
show all
Defined in:
lib/opt_parse_validator/opts/base.rb

Overview

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(option, attrs = {}) ⇒ OptBase

Note:

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

Returns a new instance of OptBase.

Parameters:

  • option (Array)

    See OptionParser#on

  • attrs (Hash) (defaults to: {})

Options Hash (attrs):

  • :required (Boolean)
  • :default (Mixed)

    The default value to use if the option is not supplied

  • :value_if_empty (Mixed)

    The value to use if no argument has been supplied

  • :normalize (Array<Symbol>)

    See #normalize



19
20
21
22
23
24
25
26
# 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

Instance Attribute Details

#attrsObject (readonly)

Returns the value of attribute attrs.



8
9
10
# File 'lib/opt_parse_validator/opts/base.rb', line 8

def attrs
  @attrs
end

#optionObject (readonly)

Returns the value of attribute option.



8
9
10
# File 'lib/opt_parse_validator/opts/base.rb', line 8

def option
  @option
end

#required=(value) ⇒ Object (writeonly)

Sets the attribute required

Parameters:

  • value

    the value to set the attribute required to.



7
8
9
# File 'lib/opt_parse_validator/opts/base.rb', line 7

def required=(value)
  @required = value
end

Instance Method Details

#advanced?Boolean

Returns:

  • (Boolean)


72
73
74
# File 'lib/opt_parse_validator/opts/base.rb', line 72

def advanced?
  attrs[:advanced] ? true : false
end

#alias?Boolean

Returns:

  • (Boolean)


67
68
69
# File 'lib/opt_parse_validator/opts/base.rb', line 67

def alias?
  false
end

#append_help_messagesVoid

Returns:

  • (Void)


29
30
31
32
33
34
35
36
# 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?
  return if required_unless.empty?

  option << "This option is mandatory unless #{required_unless.join(' or ')} is/are supplied"
end

#choicesArray<Mixed>

Returns:

  • (Array<Mixed>)


57
58
59
# File 'lib/opt_parse_validator/opts/base.rb', line 57

def choices
  attrs[:choices]
end

#defaultMixed

Returns:

  • (Mixed)


52
53
54
# File 'lib/opt_parse_validator/opts/base.rb', line 52

def default
  attrs[:default]
end

#help_message_for_defaultObject



38
39
40
# File 'lib/opt_parse_validator/opts/base.rb', line 38

def help_message_for_default
  default.to_s
end

#help_messagesArray<String>

Returns:

  • (Array<String>)


134
135
136
137
138
139
140
# File 'lib/opt_parse_validator/opts/base.rb', line 134

def help_messages
  first_message_index = option.index { |e| e[0] != '-' }

  return [] unless first_message_index

  option[first_message_index..]
end

#normalize(value) ⇒ Mixed

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

Parameters:

  • value (Mixed)

Returns:

  • (Mixed)


95
96
97
98
99
100
101
102
103
# File 'lib/opt_parse_validator/opts/base.rb', line 95

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?Boolean

Returns:

  • (Boolean)


43
44
45
# File 'lib/opt_parse_validator/opts/base.rb', line 43

def required?
  @required ||= attrs[:required]
end

#required_unlessObject



47
48
49
# File 'lib/opt_parse_validator/opts/base.rb', line 47

def required_unless
  @required_unless ||= Array(attrs[:required_unless])
end

#to_longString

Returns The raw long option (e.g: –proxy).

Returns:

  • (String)

    The raw long option (e.g: –proxy)



118
119
120
121
122
123
124
125
126
# File 'lib/opt_parse_validator/opts/base.rb', line 118

def to_long
  option.each do |option_attr|
    if option_attr.start_with?('--')
      return option_attr.gsub(/ .*$/, '')
                        .gsub(/\[[^\]]+\]/, '')
    end
  end
  nil
end

#to_sString

Returns:

  • (String)


129
130
131
# File 'lib/opt_parse_validator/opts/base.rb', line 129

def to_s
  to_sym.to_s
end

#to_symSymbol

Returns:

  • (Symbol)


106
107
108
109
110
111
112
113
114
115
# File 'lib/opt_parse_validator/opts/base.rb', line 106

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) ⇒ Object

Parameters:

  • value (String)


77
78
79
80
81
82
83
84
# File 'lib/opt_parse_validator/opts/base.rb', line 77

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_emptyMixed

Returns:

  • (Mixed)


62
63
64
# File 'lib/opt_parse_validator/opts/base.rb', line 62

def value_if_empty
  attrs[:value_if_empty]
end