Class: Bovem::Option

Inherits:
Object
  • Object
show all
Defined in:
lib/bovem/option.rb

Overview

This class represents an option for a command.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, forms = [], options = {}, &action) ⇒ Option

Creates a new option.

Parameters:

  • name (String)

    The name of this option. Must be unique.

  • forms (Array) (defaults to: [])

    An array of short and long forms for this option. Missing forms will be inferred by the name.

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

    The settings for this option.

  • action (Proc)

    The action of this option.



61
62
63
64
65
66
67
# File 'lib/bovem/option.rb', line 61

def initialize(name, forms = [], options = {}, &action)
  @name = name.ensure_string
  @provided = false
  setup_forms(forms)
  setup_options(options)
  setup_action(action)
end

Instance Attribute Details

#actionProc

Returns The action associated to this option.

Returns:

  • (Proc)

    The action associated to this option.



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
# File 'lib/bovem/option.rb', line 41

class Option
  attr_accessor :name
  attr_accessor :short
  attr_accessor :long
  attr_accessor :type
  attr_accessor :required
  attr_accessor :default
  attr_accessor :meta
  attr_accessor :help
  attr_accessor :value
  attr_accessor :action
  attr_accessor :validator
  attr_accessor :parent

  # Creates a new option.
  #
  # @param name [String] The name of this option. Must be unique.
  # @param forms [Array] An array of short and long forms for this option. Missing forms will be inferred by the name.
  # @param options [Hash] The settings for this option.
  # @param action [Proc] The action of this option.
  def initialize(name, forms = [], options = {}, &action)
    @name = name.ensure_string
    @provided = false
    setup_forms(forms)
    setup_options(options)
    setup_action(action)
  end

  # Sets the short form of this option.
  #
  # @param value [String] The short form of this option.
  def short=(value)
    value = @name[0, 1] unless value.present?

    # Clean value
    final_value = value.to_s.match(/^-{0,2}([a-z0-9])(.*)$/i)[1]

    @short = final_value if final_value.present?
  end

  # Sets the long form of this option.
  #
  # @param value [String] The short form of this option.
  def long=(value)
    value = @name unless value.present?

    # Clean value
    final_value = value.to_s.match(/^-{0,2}(.+)$/)[1]

    @long = final_value if final_value.present?
  end

  # Sets the long form of this option. Can be a Object, an Array, a Regexp or a Proc which takes one argument and returns a boolean.
  #
  # @param value [String] The validator of this option.
  def validator=(value)
    value = nil if value.blank? || (value.is_a?(Regexp) && value.source.blank?)
    value = value.ensure_array(no_duplicates: true, compact: true, flatten: true) if !value.nil? && !value.is_a?(Regexp) && !value.is_a?(Proc)
    @validator = value
  end

  # Returns the short form with a dash prepended.
  #
  # @return [String] The short form with a dash prepended.
  def complete_short
    "-#{@short}"
  end

  # Returns the long form with two dashes prepended.
  #
  # @return [String] The short form with two dashes prepended.
  def complete_long
    "--#{@long}"
  end

  # Returns a label for this option, combining short and long forms.
  #
  # @return [String] A label for this option.
  def label
    [complete_short, complete_long].compact.join("/")
  end

  # Returns the meta argument for this option.
  #
  # @return [String|NilClass] Returns the current meta argument for this option (the default value is the option name uppercased) or `nil`,
  #   if this option doesn't require a meta argument.
  def meta
    return nil unless requires_argument?
    @meta.present? ? @meta : @name.upcase
  end

  # Get the current default value for this option.
  #
  # @return [Object] The default value for this option.
  def default
    @default || Bovem::OPTION_TYPES[@type] || false
  end

  # Check if the current option has a default value.
  #
  # @return [Boolean] If the current option has a default value.
  def default?
    !@default.nil?
  end

  # Sets the value of this option and also make sure that it is validated.
  #
  # @param value [Object] The new value of this option.
  # @param raise_error [Boolean] If raise an ArgumentError in case of validation errors.
  # @return [Boolean] `true` if operation succeeded, `false` otherwise.
  def set(value, raise_error = true)
    vs = get_validator_method(@validator)
    rv = vs ? @validator.send(vs, value) : true

    if rv
      @value = value
      @provided = true
    else # Validation failed
      @value = nil
      @provided = false
      handle_set_failure(vs) if raise_error
      false
    end
  end

  # Executes the action associated to this option.
  def execute_action
    return nil unless @action.present?
    @provided = true
    @action.call(parent, self)
  end

  # Checks if this option requires an argument.
  #
  # @return [Boolean] `true` if this option requires an argument, `false` otherwise.
  def requires_argument?
    [String, Integer, Float, Array].include?(@type) && @action.blank?
  end

  # If this option was provided.
  #
  # @return [Boolean] `true` if this option was provided, `false` otherwise.
  def provided?
    @provided
  end

  # Check if this command has a help.
  #
  # @return [Boolean] `true` if this command has a help, `false` otherwise.
  def help?
    @help.present?
  end

  # Get the current value for this option.
  #
  # @return [Object] The current value of this option.
  def value
    provided? ? @value : default
  end

  private

  # :nodoc:
  def setup_forms(forms)
    self.short = !forms.empty? ? forms[0] : @name[0, 1]
    self.long = forms.length == 2 ? forms[1] : @name
  end

  # :nodoc:
  def setup_options(options)
    (options.is_a?(::Hash) ? options : {}).each_pair do |option, value|
      send("#{option}=", value) if respond_to?("#{option}=")
    end
  end

  # :nodoc:
  def setup_action(action)
    @action = action if action.present? && action.respond_to?(:call) && action.try(:arity) == 2
  end

  # :nodoc:
  def handle_set_failure(vs)
    locale = @parent.i18n

    message =
      case vs
      when "match" then locale.invalid_for_regexp(label, @validator.inspect)
      when "call" then locale.invalid_for_proc(label)
      else locale.invalid_value(label, Bovem::Parser.smart_join(@validator.ensure_array, separator: ", ", last_separator: locale.join_separator).html_safe)
      end

    raise Bovem::Errors::Error.new(self, :validation_failed, message)
  end

  # :nodoc:
  def get_validator_method(validator)
    case validator.class.to_s
    when "Array" then "include?"
    when "Regexp" then "match"
    when "Proc" then "call"
    else false
    end
  end
end

#defaultObject

Get the current default value for this option.

Returns:

  • (Object)

    The default value for this option.



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
# File 'lib/bovem/option.rb', line 41

class Option
  attr_accessor :name
  attr_accessor :short
  attr_accessor :long
  attr_accessor :type
  attr_accessor :required
  attr_accessor :default
  attr_accessor :meta
  attr_accessor :help
  attr_accessor :value
  attr_accessor :action
  attr_accessor :validator
  attr_accessor :parent

  # Creates a new option.
  #
  # @param name [String] The name of this option. Must be unique.
  # @param forms [Array] An array of short and long forms for this option. Missing forms will be inferred by the name.
  # @param options [Hash] The settings for this option.
  # @param action [Proc] The action of this option.
  def initialize(name, forms = [], options = {}, &action)
    @name = name.ensure_string
    @provided = false
    setup_forms(forms)
    setup_options(options)
    setup_action(action)
  end

  # Sets the short form of this option.
  #
  # @param value [String] The short form of this option.
  def short=(value)
    value = @name[0, 1] unless value.present?

    # Clean value
    final_value = value.to_s.match(/^-{0,2}([a-z0-9])(.*)$/i)[1]

    @short = final_value if final_value.present?
  end

  # Sets the long form of this option.
  #
  # @param value [String] The short form of this option.
  def long=(value)
    value = @name unless value.present?

    # Clean value
    final_value = value.to_s.match(/^-{0,2}(.+)$/)[1]

    @long = final_value if final_value.present?
  end

  # Sets the long form of this option. Can be a Object, an Array, a Regexp or a Proc which takes one argument and returns a boolean.
  #
  # @param value [String] The validator of this option.
  def validator=(value)
    value = nil if value.blank? || (value.is_a?(Regexp) && value.source.blank?)
    value = value.ensure_array(no_duplicates: true, compact: true, flatten: true) if !value.nil? && !value.is_a?(Regexp) && !value.is_a?(Proc)
    @validator = value
  end

  # Returns the short form with a dash prepended.
  #
  # @return [String] The short form with a dash prepended.
  def complete_short
    "-#{@short}"
  end

  # Returns the long form with two dashes prepended.
  #
  # @return [String] The short form with two dashes prepended.
  def complete_long
    "--#{@long}"
  end

  # Returns a label for this option, combining short and long forms.
  #
  # @return [String] A label for this option.
  def label
    [complete_short, complete_long].compact.join("/")
  end

  # Returns the meta argument for this option.
  #
  # @return [String|NilClass] Returns the current meta argument for this option (the default value is the option name uppercased) or `nil`,
  #   if this option doesn't require a meta argument.
  def meta
    return nil unless requires_argument?
    @meta.present? ? @meta : @name.upcase
  end

  # Get the current default value for this option.
  #
  # @return [Object] The default value for this option.
  def default
    @default || Bovem::OPTION_TYPES[@type] || false
  end

  # Check if the current option has a default value.
  #
  # @return [Boolean] If the current option has a default value.
  def default?
    !@default.nil?
  end

  # Sets the value of this option and also make sure that it is validated.
  #
  # @param value [Object] The new value of this option.
  # @param raise_error [Boolean] If raise an ArgumentError in case of validation errors.
  # @return [Boolean] `true` if operation succeeded, `false` otherwise.
  def set(value, raise_error = true)
    vs = get_validator_method(@validator)
    rv = vs ? @validator.send(vs, value) : true

    if rv
      @value = value
      @provided = true
    else # Validation failed
      @value = nil
      @provided = false
      handle_set_failure(vs) if raise_error
      false
    end
  end

  # Executes the action associated to this option.
  def execute_action
    return nil unless @action.present?
    @provided = true
    @action.call(parent, self)
  end

  # Checks if this option requires an argument.
  #
  # @return [Boolean] `true` if this option requires an argument, `false` otherwise.
  def requires_argument?
    [String, Integer, Float, Array].include?(@type) && @action.blank?
  end

  # If this option was provided.
  #
  # @return [Boolean] `true` if this option was provided, `false` otherwise.
  def provided?
    @provided
  end

  # Check if this command has a help.
  #
  # @return [Boolean] `true` if this command has a help, `false` otherwise.
  def help?
    @help.present?
  end

  # Get the current value for this option.
  #
  # @return [Object] The current value of this option.
  def value
    provided? ? @value : default
  end

  private

  # :nodoc:
  def setup_forms(forms)
    self.short = !forms.empty? ? forms[0] : @name[0, 1]
    self.long = forms.length == 2 ? forms[1] : @name
  end

  # :nodoc:
  def setup_options(options)
    (options.is_a?(::Hash) ? options : {}).each_pair do |option, value|
      send("#{option}=", value) if respond_to?("#{option}=")
    end
  end

  # :nodoc:
  def setup_action(action)
    @action = action if action.present? && action.respond_to?(:call) && action.try(:arity) == 2
  end

  # :nodoc:
  def handle_set_failure(vs)
    locale = @parent.i18n

    message =
      case vs
      when "match" then locale.invalid_for_regexp(label, @validator.inspect)
      when "call" then locale.invalid_for_proc(label)
      else locale.invalid_value(label, Bovem::Parser.smart_join(@validator.ensure_array, separator: ", ", last_separator: locale.join_separator).html_safe)
      end

    raise Bovem::Errors::Error.new(self, :validation_failed, message)
  end

  # :nodoc:
  def get_validator_method(validator)
    case validator.class.to_s
    when "Array" then "include?"
    when "Regexp" then "match"
    when "Proc" then "call"
    else false
    end
  end
end

#helpString

Returns An help message for this option.

Returns:

  • (String)

    An help message for this option.



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
# File 'lib/bovem/option.rb', line 41

class Option
  attr_accessor :name
  attr_accessor :short
  attr_accessor :long
  attr_accessor :type
  attr_accessor :required
  attr_accessor :default
  attr_accessor :meta
  attr_accessor :help
  attr_accessor :value
  attr_accessor :action
  attr_accessor :validator
  attr_accessor :parent

  # Creates a new option.
  #
  # @param name [String] The name of this option. Must be unique.
  # @param forms [Array] An array of short and long forms for this option. Missing forms will be inferred by the name.
  # @param options [Hash] The settings for this option.
  # @param action [Proc] The action of this option.
  def initialize(name, forms = [], options = {}, &action)
    @name = name.ensure_string
    @provided = false
    setup_forms(forms)
    setup_options(options)
    setup_action(action)
  end

  # Sets the short form of this option.
  #
  # @param value [String] The short form of this option.
  def short=(value)
    value = @name[0, 1] unless value.present?

    # Clean value
    final_value = value.to_s.match(/^-{0,2}([a-z0-9])(.*)$/i)[1]

    @short = final_value if final_value.present?
  end

  # Sets the long form of this option.
  #
  # @param value [String] The short form of this option.
  def long=(value)
    value = @name unless value.present?

    # Clean value
    final_value = value.to_s.match(/^-{0,2}(.+)$/)[1]

    @long = final_value if final_value.present?
  end

  # Sets the long form of this option. Can be a Object, an Array, a Regexp or a Proc which takes one argument and returns a boolean.
  #
  # @param value [String] The validator of this option.
  def validator=(value)
    value = nil if value.blank? || (value.is_a?(Regexp) && value.source.blank?)
    value = value.ensure_array(no_duplicates: true, compact: true, flatten: true) if !value.nil? && !value.is_a?(Regexp) && !value.is_a?(Proc)
    @validator = value
  end

  # Returns the short form with a dash prepended.
  #
  # @return [String] The short form with a dash prepended.
  def complete_short
    "-#{@short}"
  end

  # Returns the long form with two dashes prepended.
  #
  # @return [String] The short form with two dashes prepended.
  def complete_long
    "--#{@long}"
  end

  # Returns a label for this option, combining short and long forms.
  #
  # @return [String] A label for this option.
  def label
    [complete_short, complete_long].compact.join("/")
  end

  # Returns the meta argument for this option.
  #
  # @return [String|NilClass] Returns the current meta argument for this option (the default value is the option name uppercased) or `nil`,
  #   if this option doesn't require a meta argument.
  def meta
    return nil unless requires_argument?
    @meta.present? ? @meta : @name.upcase
  end

  # Get the current default value for this option.
  #
  # @return [Object] The default value for this option.
  def default
    @default || Bovem::OPTION_TYPES[@type] || false
  end

  # Check if the current option has a default value.
  #
  # @return [Boolean] If the current option has a default value.
  def default?
    !@default.nil?
  end

  # Sets the value of this option and also make sure that it is validated.
  #
  # @param value [Object] The new value of this option.
  # @param raise_error [Boolean] If raise an ArgumentError in case of validation errors.
  # @return [Boolean] `true` if operation succeeded, `false` otherwise.
  def set(value, raise_error = true)
    vs = get_validator_method(@validator)
    rv = vs ? @validator.send(vs, value) : true

    if rv
      @value = value
      @provided = true
    else # Validation failed
      @value = nil
      @provided = false
      handle_set_failure(vs) if raise_error
      false
    end
  end

  # Executes the action associated to this option.
  def execute_action
    return nil unless @action.present?
    @provided = true
    @action.call(parent, self)
  end

  # Checks if this option requires an argument.
  #
  # @return [Boolean] `true` if this option requires an argument, `false` otherwise.
  def requires_argument?
    [String, Integer, Float, Array].include?(@type) && @action.blank?
  end

  # If this option was provided.
  #
  # @return [Boolean] `true` if this option was provided, `false` otherwise.
  def provided?
    @provided
  end

  # Check if this command has a help.
  #
  # @return [Boolean] `true` if this command has a help, `false` otherwise.
  def help?
    @help.present?
  end

  # Get the current value for this option.
  #
  # @return [Object] The current value of this option.
  def value
    provided? ? @value : default
  end

  private

  # :nodoc:
  def setup_forms(forms)
    self.short = !forms.empty? ? forms[0] : @name[0, 1]
    self.long = forms.length == 2 ? forms[1] : @name
  end

  # :nodoc:
  def setup_options(options)
    (options.is_a?(::Hash) ? options : {}).each_pair do |option, value|
      send("#{option}=", value) if respond_to?("#{option}=")
    end
  end

  # :nodoc:
  def setup_action(action)
    @action = action if action.present? && action.respond_to?(:call) && action.try(:arity) == 2
  end

  # :nodoc:
  def handle_set_failure(vs)
    locale = @parent.i18n

    message =
      case vs
      when "match" then locale.invalid_for_regexp(label, @validator.inspect)
      when "call" then locale.invalid_for_proc(label)
      else locale.invalid_value(label, Bovem::Parser.smart_join(@validator.ensure_array, separator: ", ", last_separator: locale.join_separator).html_safe)
      end

    raise Bovem::Errors::Error.new(self, :validation_failed, message)
  end

  # :nodoc:
  def get_validator_method(validator)
    case validator.class.to_s
    when "Array" then "include?"
    when "Regexp" then "match"
    when "Proc" then "call"
    else false
    end
  end
end

#longString

Returns The long form (i.e.: --help) for this option.

Returns:

  • (String)

    The long form (i.e.: --help) for this option.



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
# File 'lib/bovem/option.rb', line 41

class Option
  attr_accessor :name
  attr_accessor :short
  attr_accessor :long
  attr_accessor :type
  attr_accessor :required
  attr_accessor :default
  attr_accessor :meta
  attr_accessor :help
  attr_accessor :value
  attr_accessor :action
  attr_accessor :validator
  attr_accessor :parent

  # Creates a new option.
  #
  # @param name [String] The name of this option. Must be unique.
  # @param forms [Array] An array of short and long forms for this option. Missing forms will be inferred by the name.
  # @param options [Hash] The settings for this option.
  # @param action [Proc] The action of this option.
  def initialize(name, forms = [], options = {}, &action)
    @name = name.ensure_string
    @provided = false
    setup_forms(forms)
    setup_options(options)
    setup_action(action)
  end

  # Sets the short form of this option.
  #
  # @param value [String] The short form of this option.
  def short=(value)
    value = @name[0, 1] unless value.present?

    # Clean value
    final_value = value.to_s.match(/^-{0,2}([a-z0-9])(.*)$/i)[1]

    @short = final_value if final_value.present?
  end

  # Sets the long form of this option.
  #
  # @param value [String] The short form of this option.
  def long=(value)
    value = @name unless value.present?

    # Clean value
    final_value = value.to_s.match(/^-{0,2}(.+)$/)[1]

    @long = final_value if final_value.present?
  end

  # Sets the long form of this option. Can be a Object, an Array, a Regexp or a Proc which takes one argument and returns a boolean.
  #
  # @param value [String] The validator of this option.
  def validator=(value)
    value = nil if value.blank? || (value.is_a?(Regexp) && value.source.blank?)
    value = value.ensure_array(no_duplicates: true, compact: true, flatten: true) if !value.nil? && !value.is_a?(Regexp) && !value.is_a?(Proc)
    @validator = value
  end

  # Returns the short form with a dash prepended.
  #
  # @return [String] The short form with a dash prepended.
  def complete_short
    "-#{@short}"
  end

  # Returns the long form with two dashes prepended.
  #
  # @return [String] The short form with two dashes prepended.
  def complete_long
    "--#{@long}"
  end

  # Returns a label for this option, combining short and long forms.
  #
  # @return [String] A label for this option.
  def label
    [complete_short, complete_long].compact.join("/")
  end

  # Returns the meta argument for this option.
  #
  # @return [String|NilClass] Returns the current meta argument for this option (the default value is the option name uppercased) or `nil`,
  #   if this option doesn't require a meta argument.
  def meta
    return nil unless requires_argument?
    @meta.present? ? @meta : @name.upcase
  end

  # Get the current default value for this option.
  #
  # @return [Object] The default value for this option.
  def default
    @default || Bovem::OPTION_TYPES[@type] || false
  end

  # Check if the current option has a default value.
  #
  # @return [Boolean] If the current option has a default value.
  def default?
    !@default.nil?
  end

  # Sets the value of this option and also make sure that it is validated.
  #
  # @param value [Object] The new value of this option.
  # @param raise_error [Boolean] If raise an ArgumentError in case of validation errors.
  # @return [Boolean] `true` if operation succeeded, `false` otherwise.
  def set(value, raise_error = true)
    vs = get_validator_method(@validator)
    rv = vs ? @validator.send(vs, value) : true

    if rv
      @value = value
      @provided = true
    else # Validation failed
      @value = nil
      @provided = false
      handle_set_failure(vs) if raise_error
      false
    end
  end

  # Executes the action associated to this option.
  def execute_action
    return nil unless @action.present?
    @provided = true
    @action.call(parent, self)
  end

  # Checks if this option requires an argument.
  #
  # @return [Boolean] `true` if this option requires an argument, `false` otherwise.
  def requires_argument?
    [String, Integer, Float, Array].include?(@type) && @action.blank?
  end

  # If this option was provided.
  #
  # @return [Boolean] `true` if this option was provided, `false` otherwise.
  def provided?
    @provided
  end

  # Check if this command has a help.
  #
  # @return [Boolean] `true` if this command has a help, `false` otherwise.
  def help?
    @help.present?
  end

  # Get the current value for this option.
  #
  # @return [Object] The current value of this option.
  def value
    provided? ? @value : default
  end

  private

  # :nodoc:
  def setup_forms(forms)
    self.short = !forms.empty? ? forms[0] : @name[0, 1]
    self.long = forms.length == 2 ? forms[1] : @name
  end

  # :nodoc:
  def setup_options(options)
    (options.is_a?(::Hash) ? options : {}).each_pair do |option, value|
      send("#{option}=", value) if respond_to?("#{option}=")
    end
  end

  # :nodoc:
  def setup_action(action)
    @action = action if action.present? && action.respond_to?(:call) && action.try(:arity) == 2
  end

  # :nodoc:
  def handle_set_failure(vs)
    locale = @parent.i18n

    message =
      case vs
      when "match" then locale.invalid_for_regexp(label, @validator.inspect)
      when "call" then locale.invalid_for_proc(label)
      else locale.invalid_value(label, Bovem::Parser.smart_join(@validator.ensure_array, separator: ", ", last_separator: locale.join_separator).html_safe)
      end

    raise Bovem::Errors::Error.new(self, :validation_failed, message)
  end

  # :nodoc:
  def get_validator_method(validator)
    case validator.class.to_s
    when "Array" then "include?"
    when "Regexp" then "match"
    when "Proc" then "call"
    else false
    end
  end
end

#metaString|NilClass

Returns the meta argument for this option.

Returns:

  • (String|NilClass)

    Returns the current meta argument for this option (the default value is the option name uppercased) or nil, if this option doesn't require a meta argument.



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
# File 'lib/bovem/option.rb', line 41

class Option
  attr_accessor :name
  attr_accessor :short
  attr_accessor :long
  attr_accessor :type
  attr_accessor :required
  attr_accessor :default
  attr_accessor :meta
  attr_accessor :help
  attr_accessor :value
  attr_accessor :action
  attr_accessor :validator
  attr_accessor :parent

  # Creates a new option.
  #
  # @param name [String] The name of this option. Must be unique.
  # @param forms [Array] An array of short and long forms for this option. Missing forms will be inferred by the name.
  # @param options [Hash] The settings for this option.
  # @param action [Proc] The action of this option.
  def initialize(name, forms = [], options = {}, &action)
    @name = name.ensure_string
    @provided = false
    setup_forms(forms)
    setup_options(options)
    setup_action(action)
  end

  # Sets the short form of this option.
  #
  # @param value [String] The short form of this option.
  def short=(value)
    value = @name[0, 1] unless value.present?

    # Clean value
    final_value = value.to_s.match(/^-{0,2}([a-z0-9])(.*)$/i)[1]

    @short = final_value if final_value.present?
  end

  # Sets the long form of this option.
  #
  # @param value [String] The short form of this option.
  def long=(value)
    value = @name unless value.present?

    # Clean value
    final_value = value.to_s.match(/^-{0,2}(.+)$/)[1]

    @long = final_value if final_value.present?
  end

  # Sets the long form of this option. Can be a Object, an Array, a Regexp or a Proc which takes one argument and returns a boolean.
  #
  # @param value [String] The validator of this option.
  def validator=(value)
    value = nil if value.blank? || (value.is_a?(Regexp) && value.source.blank?)
    value = value.ensure_array(no_duplicates: true, compact: true, flatten: true) if !value.nil? && !value.is_a?(Regexp) && !value.is_a?(Proc)
    @validator = value
  end

  # Returns the short form with a dash prepended.
  #
  # @return [String] The short form with a dash prepended.
  def complete_short
    "-#{@short}"
  end

  # Returns the long form with two dashes prepended.
  #
  # @return [String] The short form with two dashes prepended.
  def complete_long
    "--#{@long}"
  end

  # Returns a label for this option, combining short and long forms.
  #
  # @return [String] A label for this option.
  def label
    [complete_short, complete_long].compact.join("/")
  end

  # Returns the meta argument for this option.
  #
  # @return [String|NilClass] Returns the current meta argument for this option (the default value is the option name uppercased) or `nil`,
  #   if this option doesn't require a meta argument.
  def meta
    return nil unless requires_argument?
    @meta.present? ? @meta : @name.upcase
  end

  # Get the current default value for this option.
  #
  # @return [Object] The default value for this option.
  def default
    @default || Bovem::OPTION_TYPES[@type] || false
  end

  # Check if the current option has a default value.
  #
  # @return [Boolean] If the current option has a default value.
  def default?
    !@default.nil?
  end

  # Sets the value of this option and also make sure that it is validated.
  #
  # @param value [Object] The new value of this option.
  # @param raise_error [Boolean] If raise an ArgumentError in case of validation errors.
  # @return [Boolean] `true` if operation succeeded, `false` otherwise.
  def set(value, raise_error = true)
    vs = get_validator_method(@validator)
    rv = vs ? @validator.send(vs, value) : true

    if rv
      @value = value
      @provided = true
    else # Validation failed
      @value = nil
      @provided = false
      handle_set_failure(vs) if raise_error
      false
    end
  end

  # Executes the action associated to this option.
  def execute_action
    return nil unless @action.present?
    @provided = true
    @action.call(parent, self)
  end

  # Checks if this option requires an argument.
  #
  # @return [Boolean] `true` if this option requires an argument, `false` otherwise.
  def requires_argument?
    [String, Integer, Float, Array].include?(@type) && @action.blank?
  end

  # If this option was provided.
  #
  # @return [Boolean] `true` if this option was provided, `false` otherwise.
  def provided?
    @provided
  end

  # Check if this command has a help.
  #
  # @return [Boolean] `true` if this command has a help, `false` otherwise.
  def help?
    @help.present?
  end

  # Get the current value for this option.
  #
  # @return [Object] The current value of this option.
  def value
    provided? ? @value : default
  end

  private

  # :nodoc:
  def setup_forms(forms)
    self.short = !forms.empty? ? forms[0] : @name[0, 1]
    self.long = forms.length == 2 ? forms[1] : @name
  end

  # :nodoc:
  def setup_options(options)
    (options.is_a?(::Hash) ? options : {}).each_pair do |option, value|
      send("#{option}=", value) if respond_to?("#{option}=")
    end
  end

  # :nodoc:
  def setup_action(action)
    @action = action if action.present? && action.respond_to?(:call) && action.try(:arity) == 2
  end

  # :nodoc:
  def handle_set_failure(vs)
    locale = @parent.i18n

    message =
      case vs
      when "match" then locale.invalid_for_regexp(label, @validator.inspect)
      when "call" then locale.invalid_for_proc(label)
      else locale.invalid_value(label, Bovem::Parser.smart_join(@validator.ensure_array, separator: ", ", last_separator: locale.join_separator).html_safe)
      end

    raise Bovem::Errors::Error.new(self, :validation_failed, message)
  end

  # :nodoc:
  def get_validator_method(validator)
    case validator.class.to_s
    when "Array" then "include?"
    when "Regexp" then "match"
    when "Proc" then "call"
    else false
    end
  end
end

#nameString

Returns The name of this option.

Returns:

  • (String)

    The name of this option.



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
# File 'lib/bovem/option.rb', line 41

class Option
  attr_accessor :name
  attr_accessor :short
  attr_accessor :long
  attr_accessor :type
  attr_accessor :required
  attr_accessor :default
  attr_accessor :meta
  attr_accessor :help
  attr_accessor :value
  attr_accessor :action
  attr_accessor :validator
  attr_accessor :parent

  # Creates a new option.
  #
  # @param name [String] The name of this option. Must be unique.
  # @param forms [Array] An array of short and long forms for this option. Missing forms will be inferred by the name.
  # @param options [Hash] The settings for this option.
  # @param action [Proc] The action of this option.
  def initialize(name, forms = [], options = {}, &action)
    @name = name.ensure_string
    @provided = false
    setup_forms(forms)
    setup_options(options)
    setup_action(action)
  end

  # Sets the short form of this option.
  #
  # @param value [String] The short form of this option.
  def short=(value)
    value = @name[0, 1] unless value.present?

    # Clean value
    final_value = value.to_s.match(/^-{0,2}([a-z0-9])(.*)$/i)[1]

    @short = final_value if final_value.present?
  end

  # Sets the long form of this option.
  #
  # @param value [String] The short form of this option.
  def long=(value)
    value = @name unless value.present?

    # Clean value
    final_value = value.to_s.match(/^-{0,2}(.+)$/)[1]

    @long = final_value if final_value.present?
  end

  # Sets the long form of this option. Can be a Object, an Array, a Regexp or a Proc which takes one argument and returns a boolean.
  #
  # @param value [String] The validator of this option.
  def validator=(value)
    value = nil if value.blank? || (value.is_a?(Regexp) && value.source.blank?)
    value = value.ensure_array(no_duplicates: true, compact: true, flatten: true) if !value.nil? && !value.is_a?(Regexp) && !value.is_a?(Proc)
    @validator = value
  end

  # Returns the short form with a dash prepended.
  #
  # @return [String] The short form with a dash prepended.
  def complete_short
    "-#{@short}"
  end

  # Returns the long form with two dashes prepended.
  #
  # @return [String] The short form with two dashes prepended.
  def complete_long
    "--#{@long}"
  end

  # Returns a label for this option, combining short and long forms.
  #
  # @return [String] A label for this option.
  def label
    [complete_short, complete_long].compact.join("/")
  end

  # Returns the meta argument for this option.
  #
  # @return [String|NilClass] Returns the current meta argument for this option (the default value is the option name uppercased) or `nil`,
  #   if this option doesn't require a meta argument.
  def meta
    return nil unless requires_argument?
    @meta.present? ? @meta : @name.upcase
  end

  # Get the current default value for this option.
  #
  # @return [Object] The default value for this option.
  def default
    @default || Bovem::OPTION_TYPES[@type] || false
  end

  # Check if the current option has a default value.
  #
  # @return [Boolean] If the current option has a default value.
  def default?
    !@default.nil?
  end

  # Sets the value of this option and also make sure that it is validated.
  #
  # @param value [Object] The new value of this option.
  # @param raise_error [Boolean] If raise an ArgumentError in case of validation errors.
  # @return [Boolean] `true` if operation succeeded, `false` otherwise.
  def set(value, raise_error = true)
    vs = get_validator_method(@validator)
    rv = vs ? @validator.send(vs, value) : true

    if rv
      @value = value
      @provided = true
    else # Validation failed
      @value = nil
      @provided = false
      handle_set_failure(vs) if raise_error
      false
    end
  end

  # Executes the action associated to this option.
  def execute_action
    return nil unless @action.present?
    @provided = true
    @action.call(parent, self)
  end

  # Checks if this option requires an argument.
  #
  # @return [Boolean] `true` if this option requires an argument, `false` otherwise.
  def requires_argument?
    [String, Integer, Float, Array].include?(@type) && @action.blank?
  end

  # If this option was provided.
  #
  # @return [Boolean] `true` if this option was provided, `false` otherwise.
  def provided?
    @provided
  end

  # Check if this command has a help.
  #
  # @return [Boolean] `true` if this command has a help, `false` otherwise.
  def help?
    @help.present?
  end

  # Get the current value for this option.
  #
  # @return [Object] The current value of this option.
  def value
    provided? ? @value : default
  end

  private

  # :nodoc:
  def setup_forms(forms)
    self.short = !forms.empty? ? forms[0] : @name[0, 1]
    self.long = forms.length == 2 ? forms[1] : @name
  end

  # :nodoc:
  def setup_options(options)
    (options.is_a?(::Hash) ? options : {}).each_pair do |option, value|
      send("#{option}=", value) if respond_to?("#{option}=")
    end
  end

  # :nodoc:
  def setup_action(action)
    @action = action if action.present? && action.respond_to?(:call) && action.try(:arity) == 2
  end

  # :nodoc:
  def handle_set_failure(vs)
    locale = @parent.i18n

    message =
      case vs
      when "match" then locale.invalid_for_regexp(label, @validator.inspect)
      when "call" then locale.invalid_for_proc(label)
      else locale.invalid_value(label, Bovem::Parser.smart_join(@validator.ensure_array, separator: ", ", last_separator: locale.join_separator).html_safe)
      end

    raise Bovem::Errors::Error.new(self, :validation_failed, message)
  end

  # :nodoc:
  def get_validator_method(validator)
    case validator.class.to_s
    when "Array" then "include?"
    when "Regexp" then "match"
    when "Proc" then "call"
    else false
    end
  end
end

#parentCommand

Returns The parent of this option.

Returns:

  • (Command)

    The parent of this option.



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
# File 'lib/bovem/option.rb', line 41

class Option
  attr_accessor :name
  attr_accessor :short
  attr_accessor :long
  attr_accessor :type
  attr_accessor :required
  attr_accessor :default
  attr_accessor :meta
  attr_accessor :help
  attr_accessor :value
  attr_accessor :action
  attr_accessor :validator
  attr_accessor :parent

  # Creates a new option.
  #
  # @param name [String] The name of this option. Must be unique.
  # @param forms [Array] An array of short and long forms for this option. Missing forms will be inferred by the name.
  # @param options [Hash] The settings for this option.
  # @param action [Proc] The action of this option.
  def initialize(name, forms = [], options = {}, &action)
    @name = name.ensure_string
    @provided = false
    setup_forms(forms)
    setup_options(options)
    setup_action(action)
  end

  # Sets the short form of this option.
  #
  # @param value [String] The short form of this option.
  def short=(value)
    value = @name[0, 1] unless value.present?

    # Clean value
    final_value = value.to_s.match(/^-{0,2}([a-z0-9])(.*)$/i)[1]

    @short = final_value if final_value.present?
  end

  # Sets the long form of this option.
  #
  # @param value [String] The short form of this option.
  def long=(value)
    value = @name unless value.present?

    # Clean value
    final_value = value.to_s.match(/^-{0,2}(.+)$/)[1]

    @long = final_value if final_value.present?
  end

  # Sets the long form of this option. Can be a Object, an Array, a Regexp or a Proc which takes one argument and returns a boolean.
  #
  # @param value [String] The validator of this option.
  def validator=(value)
    value = nil if value.blank? || (value.is_a?(Regexp) && value.source.blank?)
    value = value.ensure_array(no_duplicates: true, compact: true, flatten: true) if !value.nil? && !value.is_a?(Regexp) && !value.is_a?(Proc)
    @validator = value
  end

  # Returns the short form with a dash prepended.
  #
  # @return [String] The short form with a dash prepended.
  def complete_short
    "-#{@short}"
  end

  # Returns the long form with two dashes prepended.
  #
  # @return [String] The short form with two dashes prepended.
  def complete_long
    "--#{@long}"
  end

  # Returns a label for this option, combining short and long forms.
  #
  # @return [String] A label for this option.
  def label
    [complete_short, complete_long].compact.join("/")
  end

  # Returns the meta argument for this option.
  #
  # @return [String|NilClass] Returns the current meta argument for this option (the default value is the option name uppercased) or `nil`,
  #   if this option doesn't require a meta argument.
  def meta
    return nil unless requires_argument?
    @meta.present? ? @meta : @name.upcase
  end

  # Get the current default value for this option.
  #
  # @return [Object] The default value for this option.
  def default
    @default || Bovem::OPTION_TYPES[@type] || false
  end

  # Check if the current option has a default value.
  #
  # @return [Boolean] If the current option has a default value.
  def default?
    !@default.nil?
  end

  # Sets the value of this option and also make sure that it is validated.
  #
  # @param value [Object] The new value of this option.
  # @param raise_error [Boolean] If raise an ArgumentError in case of validation errors.
  # @return [Boolean] `true` if operation succeeded, `false` otherwise.
  def set(value, raise_error = true)
    vs = get_validator_method(@validator)
    rv = vs ? @validator.send(vs, value) : true

    if rv
      @value = value
      @provided = true
    else # Validation failed
      @value = nil
      @provided = false
      handle_set_failure(vs) if raise_error
      false
    end
  end

  # Executes the action associated to this option.
  def execute_action
    return nil unless @action.present?
    @provided = true
    @action.call(parent, self)
  end

  # Checks if this option requires an argument.
  #
  # @return [Boolean] `true` if this option requires an argument, `false` otherwise.
  def requires_argument?
    [String, Integer, Float, Array].include?(@type) && @action.blank?
  end

  # If this option was provided.
  #
  # @return [Boolean] `true` if this option was provided, `false` otherwise.
  def provided?
    @provided
  end

  # Check if this command has a help.
  #
  # @return [Boolean] `true` if this command has a help, `false` otherwise.
  def help?
    @help.present?
  end

  # Get the current value for this option.
  #
  # @return [Object] The current value of this option.
  def value
    provided? ? @value : default
  end

  private

  # :nodoc:
  def setup_forms(forms)
    self.short = !forms.empty? ? forms[0] : @name[0, 1]
    self.long = forms.length == 2 ? forms[1] : @name
  end

  # :nodoc:
  def setup_options(options)
    (options.is_a?(::Hash) ? options : {}).each_pair do |option, value|
      send("#{option}=", value) if respond_to?("#{option}=")
    end
  end

  # :nodoc:
  def setup_action(action)
    @action = action if action.present? && action.respond_to?(:call) && action.try(:arity) == 2
  end

  # :nodoc:
  def handle_set_failure(vs)
    locale = @parent.i18n

    message =
      case vs
      when "match" then locale.invalid_for_regexp(label, @validator.inspect)
      when "call" then locale.invalid_for_proc(label)
      else locale.invalid_value(label, Bovem::Parser.smart_join(@validator.ensure_array, separator: ", ", last_separator: locale.join_separator).html_safe)
      end

    raise Bovem::Errors::Error.new(self, :validation_failed, message)
  end

  # :nodoc:
  def get_validator_method(validator)
    case validator.class.to_s
    when "Array" then "include?"
    when "Regexp" then "match"
    when "Proc" then "call"
    else false
    end
  end
end

#requiredBoolean

Returns If this option is required.

Returns:

  • (Boolean)

    If this option is required.



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
# File 'lib/bovem/option.rb', line 41

class Option
  attr_accessor :name
  attr_accessor :short
  attr_accessor :long
  attr_accessor :type
  attr_accessor :required
  attr_accessor :default
  attr_accessor :meta
  attr_accessor :help
  attr_accessor :value
  attr_accessor :action
  attr_accessor :validator
  attr_accessor :parent

  # Creates a new option.
  #
  # @param name [String] The name of this option. Must be unique.
  # @param forms [Array] An array of short and long forms for this option. Missing forms will be inferred by the name.
  # @param options [Hash] The settings for this option.
  # @param action [Proc] The action of this option.
  def initialize(name, forms = [], options = {}, &action)
    @name = name.ensure_string
    @provided = false
    setup_forms(forms)
    setup_options(options)
    setup_action(action)
  end

  # Sets the short form of this option.
  #
  # @param value [String] The short form of this option.
  def short=(value)
    value = @name[0, 1] unless value.present?

    # Clean value
    final_value = value.to_s.match(/^-{0,2}([a-z0-9])(.*)$/i)[1]

    @short = final_value if final_value.present?
  end

  # Sets the long form of this option.
  #
  # @param value [String] The short form of this option.
  def long=(value)
    value = @name unless value.present?

    # Clean value
    final_value = value.to_s.match(/^-{0,2}(.+)$/)[1]

    @long = final_value if final_value.present?
  end

  # Sets the long form of this option. Can be a Object, an Array, a Regexp or a Proc which takes one argument and returns a boolean.
  #
  # @param value [String] The validator of this option.
  def validator=(value)
    value = nil if value.blank? || (value.is_a?(Regexp) && value.source.blank?)
    value = value.ensure_array(no_duplicates: true, compact: true, flatten: true) if !value.nil? && !value.is_a?(Regexp) && !value.is_a?(Proc)
    @validator = value
  end

  # Returns the short form with a dash prepended.
  #
  # @return [String] The short form with a dash prepended.
  def complete_short
    "-#{@short}"
  end

  # Returns the long form with two dashes prepended.
  #
  # @return [String] The short form with two dashes prepended.
  def complete_long
    "--#{@long}"
  end

  # Returns a label for this option, combining short and long forms.
  #
  # @return [String] A label for this option.
  def label
    [complete_short, complete_long].compact.join("/")
  end

  # Returns the meta argument for this option.
  #
  # @return [String|NilClass] Returns the current meta argument for this option (the default value is the option name uppercased) or `nil`,
  #   if this option doesn't require a meta argument.
  def meta
    return nil unless requires_argument?
    @meta.present? ? @meta : @name.upcase
  end

  # Get the current default value for this option.
  #
  # @return [Object] The default value for this option.
  def default
    @default || Bovem::OPTION_TYPES[@type] || false
  end

  # Check if the current option has a default value.
  #
  # @return [Boolean] If the current option has a default value.
  def default?
    !@default.nil?
  end

  # Sets the value of this option and also make sure that it is validated.
  #
  # @param value [Object] The new value of this option.
  # @param raise_error [Boolean] If raise an ArgumentError in case of validation errors.
  # @return [Boolean] `true` if operation succeeded, `false` otherwise.
  def set(value, raise_error = true)
    vs = get_validator_method(@validator)
    rv = vs ? @validator.send(vs, value) : true

    if rv
      @value = value
      @provided = true
    else # Validation failed
      @value = nil
      @provided = false
      handle_set_failure(vs) if raise_error
      false
    end
  end

  # Executes the action associated to this option.
  def execute_action
    return nil unless @action.present?
    @provided = true
    @action.call(parent, self)
  end

  # Checks if this option requires an argument.
  #
  # @return [Boolean] `true` if this option requires an argument, `false` otherwise.
  def requires_argument?
    [String, Integer, Float, Array].include?(@type) && @action.blank?
  end

  # If this option was provided.
  #
  # @return [Boolean] `true` if this option was provided, `false` otherwise.
  def provided?
    @provided
  end

  # Check if this command has a help.
  #
  # @return [Boolean] `true` if this command has a help, `false` otherwise.
  def help?
    @help.present?
  end

  # Get the current value for this option.
  #
  # @return [Object] The current value of this option.
  def value
    provided? ? @value : default
  end

  private

  # :nodoc:
  def setup_forms(forms)
    self.short = !forms.empty? ? forms[0] : @name[0, 1]
    self.long = forms.length == 2 ? forms[1] : @name
  end

  # :nodoc:
  def setup_options(options)
    (options.is_a?(::Hash) ? options : {}).each_pair do |option, value|
      send("#{option}=", value) if respond_to?("#{option}=")
    end
  end

  # :nodoc:
  def setup_action(action)
    @action = action if action.present? && action.respond_to?(:call) && action.try(:arity) == 2
  end

  # :nodoc:
  def handle_set_failure(vs)
    locale = @parent.i18n

    message =
      case vs
      when "match" then locale.invalid_for_regexp(label, @validator.inspect)
      when "call" then locale.invalid_for_proc(label)
      else locale.invalid_value(label, Bovem::Parser.smart_join(@validator.ensure_array, separator: ", ", last_separator: locale.join_separator).html_safe)
      end

    raise Bovem::Errors::Error.new(self, :validation_failed, message)
  end

  # :nodoc:
  def get_validator_method(validator)
    case validator.class.to_s
    when "Array" then "include?"
    when "Regexp" then "match"
    when "Proc" then "call"
    else false
    end
  end
end

#shortString

Returns The short form (i.e.: -h) for this option.

Returns:

  • (String)

    The short form (i.e.: -h) for this option.



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
# File 'lib/bovem/option.rb', line 41

class Option
  attr_accessor :name
  attr_accessor :short
  attr_accessor :long
  attr_accessor :type
  attr_accessor :required
  attr_accessor :default
  attr_accessor :meta
  attr_accessor :help
  attr_accessor :value
  attr_accessor :action
  attr_accessor :validator
  attr_accessor :parent

  # Creates a new option.
  #
  # @param name [String] The name of this option. Must be unique.
  # @param forms [Array] An array of short and long forms for this option. Missing forms will be inferred by the name.
  # @param options [Hash] The settings for this option.
  # @param action [Proc] The action of this option.
  def initialize(name, forms = [], options = {}, &action)
    @name = name.ensure_string
    @provided = false
    setup_forms(forms)
    setup_options(options)
    setup_action(action)
  end

  # Sets the short form of this option.
  #
  # @param value [String] The short form of this option.
  def short=(value)
    value = @name[0, 1] unless value.present?

    # Clean value
    final_value = value.to_s.match(/^-{0,2}([a-z0-9])(.*)$/i)[1]

    @short = final_value if final_value.present?
  end

  # Sets the long form of this option.
  #
  # @param value [String] The short form of this option.
  def long=(value)
    value = @name unless value.present?

    # Clean value
    final_value = value.to_s.match(/^-{0,2}(.+)$/)[1]

    @long = final_value if final_value.present?
  end

  # Sets the long form of this option. Can be a Object, an Array, a Regexp or a Proc which takes one argument and returns a boolean.
  #
  # @param value [String] The validator of this option.
  def validator=(value)
    value = nil if value.blank? || (value.is_a?(Regexp) && value.source.blank?)
    value = value.ensure_array(no_duplicates: true, compact: true, flatten: true) if !value.nil? && !value.is_a?(Regexp) && !value.is_a?(Proc)
    @validator = value
  end

  # Returns the short form with a dash prepended.
  #
  # @return [String] The short form with a dash prepended.
  def complete_short
    "-#{@short}"
  end

  # Returns the long form with two dashes prepended.
  #
  # @return [String] The short form with two dashes prepended.
  def complete_long
    "--#{@long}"
  end

  # Returns a label for this option, combining short and long forms.
  #
  # @return [String] A label for this option.
  def label
    [complete_short, complete_long].compact.join("/")
  end

  # Returns the meta argument for this option.
  #
  # @return [String|NilClass] Returns the current meta argument for this option (the default value is the option name uppercased) or `nil`,
  #   if this option doesn't require a meta argument.
  def meta
    return nil unless requires_argument?
    @meta.present? ? @meta : @name.upcase
  end

  # Get the current default value for this option.
  #
  # @return [Object] The default value for this option.
  def default
    @default || Bovem::OPTION_TYPES[@type] || false
  end

  # Check if the current option has a default value.
  #
  # @return [Boolean] If the current option has a default value.
  def default?
    !@default.nil?
  end

  # Sets the value of this option and also make sure that it is validated.
  #
  # @param value [Object] The new value of this option.
  # @param raise_error [Boolean] If raise an ArgumentError in case of validation errors.
  # @return [Boolean] `true` if operation succeeded, `false` otherwise.
  def set(value, raise_error = true)
    vs = get_validator_method(@validator)
    rv = vs ? @validator.send(vs, value) : true

    if rv
      @value = value
      @provided = true
    else # Validation failed
      @value = nil
      @provided = false
      handle_set_failure(vs) if raise_error
      false
    end
  end

  # Executes the action associated to this option.
  def execute_action
    return nil unless @action.present?
    @provided = true
    @action.call(parent, self)
  end

  # Checks if this option requires an argument.
  #
  # @return [Boolean] `true` if this option requires an argument, `false` otherwise.
  def requires_argument?
    [String, Integer, Float, Array].include?(@type) && @action.blank?
  end

  # If this option was provided.
  #
  # @return [Boolean] `true` if this option was provided, `false` otherwise.
  def provided?
    @provided
  end

  # Check if this command has a help.
  #
  # @return [Boolean] `true` if this command has a help, `false` otherwise.
  def help?
    @help.present?
  end

  # Get the current value for this option.
  #
  # @return [Object] The current value of this option.
  def value
    provided? ? @value : default
  end

  private

  # :nodoc:
  def setup_forms(forms)
    self.short = !forms.empty? ? forms[0] : @name[0, 1]
    self.long = forms.length == 2 ? forms[1] : @name
  end

  # :nodoc:
  def setup_options(options)
    (options.is_a?(::Hash) ? options : {}).each_pair do |option, value|
      send("#{option}=", value) if respond_to?("#{option}=")
    end
  end

  # :nodoc:
  def setup_action(action)
    @action = action if action.present? && action.respond_to?(:call) && action.try(:arity) == 2
  end

  # :nodoc:
  def handle_set_failure(vs)
    locale = @parent.i18n

    message =
      case vs
      when "match" then locale.invalid_for_regexp(label, @validator.inspect)
      when "call" then locale.invalid_for_proc(label)
      else locale.invalid_value(label, Bovem::Parser.smart_join(@validator.ensure_array, separator: ", ", last_separator: locale.join_separator).html_safe)
      end

    raise Bovem::Errors::Error.new(self, :validation_failed, message)
  end

  # :nodoc:
  def get_validator_method(validator)
    case validator.class.to_s
    when "Array" then "include?"
    when "Regexp" then "match"
    when "Proc" then "call"
    else false
    end
  end
end

#typeClass

Returns The type of this option.

Returns:

  • (Class)

    The type of this option.



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
# File 'lib/bovem/option.rb', line 41

class Option
  attr_accessor :name
  attr_accessor :short
  attr_accessor :long
  attr_accessor :type
  attr_accessor :required
  attr_accessor :default
  attr_accessor :meta
  attr_accessor :help
  attr_accessor :value
  attr_accessor :action
  attr_accessor :validator
  attr_accessor :parent

  # Creates a new option.
  #
  # @param name [String] The name of this option. Must be unique.
  # @param forms [Array] An array of short and long forms for this option. Missing forms will be inferred by the name.
  # @param options [Hash] The settings for this option.
  # @param action [Proc] The action of this option.
  def initialize(name, forms = [], options = {}, &action)
    @name = name.ensure_string
    @provided = false
    setup_forms(forms)
    setup_options(options)
    setup_action(action)
  end

  # Sets the short form of this option.
  #
  # @param value [String] The short form of this option.
  def short=(value)
    value = @name[0, 1] unless value.present?

    # Clean value
    final_value = value.to_s.match(/^-{0,2}([a-z0-9])(.*)$/i)[1]

    @short = final_value if final_value.present?
  end

  # Sets the long form of this option.
  #
  # @param value [String] The short form of this option.
  def long=(value)
    value = @name unless value.present?

    # Clean value
    final_value = value.to_s.match(/^-{0,2}(.+)$/)[1]

    @long = final_value if final_value.present?
  end

  # Sets the long form of this option. Can be a Object, an Array, a Regexp or a Proc which takes one argument and returns a boolean.
  #
  # @param value [String] The validator of this option.
  def validator=(value)
    value = nil if value.blank? || (value.is_a?(Regexp) && value.source.blank?)
    value = value.ensure_array(no_duplicates: true, compact: true, flatten: true) if !value.nil? && !value.is_a?(Regexp) && !value.is_a?(Proc)
    @validator = value
  end

  # Returns the short form with a dash prepended.
  #
  # @return [String] The short form with a dash prepended.
  def complete_short
    "-#{@short}"
  end

  # Returns the long form with two dashes prepended.
  #
  # @return [String] The short form with two dashes prepended.
  def complete_long
    "--#{@long}"
  end

  # Returns a label for this option, combining short and long forms.
  #
  # @return [String] A label for this option.
  def label
    [complete_short, complete_long].compact.join("/")
  end

  # Returns the meta argument for this option.
  #
  # @return [String|NilClass] Returns the current meta argument for this option (the default value is the option name uppercased) or `nil`,
  #   if this option doesn't require a meta argument.
  def meta
    return nil unless requires_argument?
    @meta.present? ? @meta : @name.upcase
  end

  # Get the current default value for this option.
  #
  # @return [Object] The default value for this option.
  def default
    @default || Bovem::OPTION_TYPES[@type] || false
  end

  # Check if the current option has a default value.
  #
  # @return [Boolean] If the current option has a default value.
  def default?
    !@default.nil?
  end

  # Sets the value of this option and also make sure that it is validated.
  #
  # @param value [Object] The new value of this option.
  # @param raise_error [Boolean] If raise an ArgumentError in case of validation errors.
  # @return [Boolean] `true` if operation succeeded, `false` otherwise.
  def set(value, raise_error = true)
    vs = get_validator_method(@validator)
    rv = vs ? @validator.send(vs, value) : true

    if rv
      @value = value
      @provided = true
    else # Validation failed
      @value = nil
      @provided = false
      handle_set_failure(vs) if raise_error
      false
    end
  end

  # Executes the action associated to this option.
  def execute_action
    return nil unless @action.present?
    @provided = true
    @action.call(parent, self)
  end

  # Checks if this option requires an argument.
  #
  # @return [Boolean] `true` if this option requires an argument, `false` otherwise.
  def requires_argument?
    [String, Integer, Float, Array].include?(@type) && @action.blank?
  end

  # If this option was provided.
  #
  # @return [Boolean] `true` if this option was provided, `false` otherwise.
  def provided?
    @provided
  end

  # Check if this command has a help.
  #
  # @return [Boolean] `true` if this command has a help, `false` otherwise.
  def help?
    @help.present?
  end

  # Get the current value for this option.
  #
  # @return [Object] The current value of this option.
  def value
    provided? ? @value : default
  end

  private

  # :nodoc:
  def setup_forms(forms)
    self.short = !forms.empty? ? forms[0] : @name[0, 1]
    self.long = forms.length == 2 ? forms[1] : @name
  end

  # :nodoc:
  def setup_options(options)
    (options.is_a?(::Hash) ? options : {}).each_pair do |option, value|
      send("#{option}=", value) if respond_to?("#{option}=")
    end
  end

  # :nodoc:
  def setup_action(action)
    @action = action if action.present? && action.respond_to?(:call) && action.try(:arity) == 2
  end

  # :nodoc:
  def handle_set_failure(vs)
    locale = @parent.i18n

    message =
      case vs
      when "match" then locale.invalid_for_regexp(label, @validator.inspect)
      when "call" then locale.invalid_for_proc(label)
      else locale.invalid_value(label, Bovem::Parser.smart_join(@validator.ensure_array, separator: ", ", last_separator: locale.join_separator).html_safe)
      end

    raise Bovem::Errors::Error.new(self, :validation_failed, message)
  end

  # :nodoc:
  def get_validator_method(validator)
    case validator.class.to_s
    when "Array" then "include?"
    when "Regexp" then "match"
    when "Proc" then "call"
    else false
    end
  end
end

#validatorArray|Regexp

Returns or A constraint for valid values. Can be an Array of valid values or a Regexp.

Returns:

  • (Array|Regexp)

    or A constraint for valid values. Can be an Array of valid values or a Regexp.



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
# File 'lib/bovem/option.rb', line 41

class Option
  attr_accessor :name
  attr_accessor :short
  attr_accessor :long
  attr_accessor :type
  attr_accessor :required
  attr_accessor :default
  attr_accessor :meta
  attr_accessor :help
  attr_accessor :value
  attr_accessor :action
  attr_accessor :validator
  attr_accessor :parent

  # Creates a new option.
  #
  # @param name [String] The name of this option. Must be unique.
  # @param forms [Array] An array of short and long forms for this option. Missing forms will be inferred by the name.
  # @param options [Hash] The settings for this option.
  # @param action [Proc] The action of this option.
  def initialize(name, forms = [], options = {}, &action)
    @name = name.ensure_string
    @provided = false
    setup_forms(forms)
    setup_options(options)
    setup_action(action)
  end

  # Sets the short form of this option.
  #
  # @param value [String] The short form of this option.
  def short=(value)
    value = @name[0, 1] unless value.present?

    # Clean value
    final_value = value.to_s.match(/^-{0,2}([a-z0-9])(.*)$/i)[1]

    @short = final_value if final_value.present?
  end

  # Sets the long form of this option.
  #
  # @param value [String] The short form of this option.
  def long=(value)
    value = @name unless value.present?

    # Clean value
    final_value = value.to_s.match(/^-{0,2}(.+)$/)[1]

    @long = final_value if final_value.present?
  end

  # Sets the long form of this option. Can be a Object, an Array, a Regexp or a Proc which takes one argument and returns a boolean.
  #
  # @param value [String] The validator of this option.
  def validator=(value)
    value = nil if value.blank? || (value.is_a?(Regexp) && value.source.blank?)
    value = value.ensure_array(no_duplicates: true, compact: true, flatten: true) if !value.nil? && !value.is_a?(Regexp) && !value.is_a?(Proc)
    @validator = value
  end

  # Returns the short form with a dash prepended.
  #
  # @return [String] The short form with a dash prepended.
  def complete_short
    "-#{@short}"
  end

  # Returns the long form with two dashes prepended.
  #
  # @return [String] The short form with two dashes prepended.
  def complete_long
    "--#{@long}"
  end

  # Returns a label for this option, combining short and long forms.
  #
  # @return [String] A label for this option.
  def label
    [complete_short, complete_long].compact.join("/")
  end

  # Returns the meta argument for this option.
  #
  # @return [String|NilClass] Returns the current meta argument for this option (the default value is the option name uppercased) or `nil`,
  #   if this option doesn't require a meta argument.
  def meta
    return nil unless requires_argument?
    @meta.present? ? @meta : @name.upcase
  end

  # Get the current default value for this option.
  #
  # @return [Object] The default value for this option.
  def default
    @default || Bovem::OPTION_TYPES[@type] || false
  end

  # Check if the current option has a default value.
  #
  # @return [Boolean] If the current option has a default value.
  def default?
    !@default.nil?
  end

  # Sets the value of this option and also make sure that it is validated.
  #
  # @param value [Object] The new value of this option.
  # @param raise_error [Boolean] If raise an ArgumentError in case of validation errors.
  # @return [Boolean] `true` if operation succeeded, `false` otherwise.
  def set(value, raise_error = true)
    vs = get_validator_method(@validator)
    rv = vs ? @validator.send(vs, value) : true

    if rv
      @value = value
      @provided = true
    else # Validation failed
      @value = nil
      @provided = false
      handle_set_failure(vs) if raise_error
      false
    end
  end

  # Executes the action associated to this option.
  def execute_action
    return nil unless @action.present?
    @provided = true
    @action.call(parent, self)
  end

  # Checks if this option requires an argument.
  #
  # @return [Boolean] `true` if this option requires an argument, `false` otherwise.
  def requires_argument?
    [String, Integer, Float, Array].include?(@type) && @action.blank?
  end

  # If this option was provided.
  #
  # @return [Boolean] `true` if this option was provided, `false` otherwise.
  def provided?
    @provided
  end

  # Check if this command has a help.
  #
  # @return [Boolean] `true` if this command has a help, `false` otherwise.
  def help?
    @help.present?
  end

  # Get the current value for this option.
  #
  # @return [Object] The current value of this option.
  def value
    provided? ? @value : default
  end

  private

  # :nodoc:
  def setup_forms(forms)
    self.short = !forms.empty? ? forms[0] : @name[0, 1]
    self.long = forms.length == 2 ? forms[1] : @name
  end

  # :nodoc:
  def setup_options(options)
    (options.is_a?(::Hash) ? options : {}).each_pair do |option, value|
      send("#{option}=", value) if respond_to?("#{option}=")
    end
  end

  # :nodoc:
  def setup_action(action)
    @action = action if action.present? && action.respond_to?(:call) && action.try(:arity) == 2
  end

  # :nodoc:
  def handle_set_failure(vs)
    locale = @parent.i18n

    message =
      case vs
      when "match" then locale.invalid_for_regexp(label, @validator.inspect)
      when "call" then locale.invalid_for_proc(label)
      else locale.invalid_value(label, Bovem::Parser.smart_join(@validator.ensure_array, separator: ", ", last_separator: locale.join_separator).html_safe)
      end

    raise Bovem::Errors::Error.new(self, :validation_failed, message)
  end

  # :nodoc:
  def get_validator_method(validator)
    case validator.class.to_s
    when "Array" then "include?"
    when "Regexp" then "match"
    when "Proc" then "call"
    else false
    end
  end
end

#valueObject

Get the current value for this option.

Returns:

  • (Object)

    The current value of this option.



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
# File 'lib/bovem/option.rb', line 41

class Option
  attr_accessor :name
  attr_accessor :short
  attr_accessor :long
  attr_accessor :type
  attr_accessor :required
  attr_accessor :default
  attr_accessor :meta
  attr_accessor :help
  attr_accessor :value
  attr_accessor :action
  attr_accessor :validator
  attr_accessor :parent

  # Creates a new option.
  #
  # @param name [String] The name of this option. Must be unique.
  # @param forms [Array] An array of short and long forms for this option. Missing forms will be inferred by the name.
  # @param options [Hash] The settings for this option.
  # @param action [Proc] The action of this option.
  def initialize(name, forms = [], options = {}, &action)
    @name = name.ensure_string
    @provided = false
    setup_forms(forms)
    setup_options(options)
    setup_action(action)
  end

  # Sets the short form of this option.
  #
  # @param value [String] The short form of this option.
  def short=(value)
    value = @name[0, 1] unless value.present?

    # Clean value
    final_value = value.to_s.match(/^-{0,2}([a-z0-9])(.*)$/i)[1]

    @short = final_value if final_value.present?
  end

  # Sets the long form of this option.
  #
  # @param value [String] The short form of this option.
  def long=(value)
    value = @name unless value.present?

    # Clean value
    final_value = value.to_s.match(/^-{0,2}(.+)$/)[1]

    @long = final_value if final_value.present?
  end

  # Sets the long form of this option. Can be a Object, an Array, a Regexp or a Proc which takes one argument and returns a boolean.
  #
  # @param value [String] The validator of this option.
  def validator=(value)
    value = nil if value.blank? || (value.is_a?(Regexp) && value.source.blank?)
    value = value.ensure_array(no_duplicates: true, compact: true, flatten: true) if !value.nil? && !value.is_a?(Regexp) && !value.is_a?(Proc)
    @validator = value
  end

  # Returns the short form with a dash prepended.
  #
  # @return [String] The short form with a dash prepended.
  def complete_short
    "-#{@short}"
  end

  # Returns the long form with two dashes prepended.
  #
  # @return [String] The short form with two dashes prepended.
  def complete_long
    "--#{@long}"
  end

  # Returns a label for this option, combining short and long forms.
  #
  # @return [String] A label for this option.
  def label
    [complete_short, complete_long].compact.join("/")
  end

  # Returns the meta argument for this option.
  #
  # @return [String|NilClass] Returns the current meta argument for this option (the default value is the option name uppercased) or `nil`,
  #   if this option doesn't require a meta argument.
  def meta
    return nil unless requires_argument?
    @meta.present? ? @meta : @name.upcase
  end

  # Get the current default value for this option.
  #
  # @return [Object] The default value for this option.
  def default
    @default || Bovem::OPTION_TYPES[@type] || false
  end

  # Check if the current option has a default value.
  #
  # @return [Boolean] If the current option has a default value.
  def default?
    !@default.nil?
  end

  # Sets the value of this option and also make sure that it is validated.
  #
  # @param value [Object] The new value of this option.
  # @param raise_error [Boolean] If raise an ArgumentError in case of validation errors.
  # @return [Boolean] `true` if operation succeeded, `false` otherwise.
  def set(value, raise_error = true)
    vs = get_validator_method(@validator)
    rv = vs ? @validator.send(vs, value) : true

    if rv
      @value = value
      @provided = true
    else # Validation failed
      @value = nil
      @provided = false
      handle_set_failure(vs) if raise_error
      false
    end
  end

  # Executes the action associated to this option.
  def execute_action
    return nil unless @action.present?
    @provided = true
    @action.call(parent, self)
  end

  # Checks if this option requires an argument.
  #
  # @return [Boolean] `true` if this option requires an argument, `false` otherwise.
  def requires_argument?
    [String, Integer, Float, Array].include?(@type) && @action.blank?
  end

  # If this option was provided.
  #
  # @return [Boolean] `true` if this option was provided, `false` otherwise.
  def provided?
    @provided
  end

  # Check if this command has a help.
  #
  # @return [Boolean] `true` if this command has a help, `false` otherwise.
  def help?
    @help.present?
  end

  # Get the current value for this option.
  #
  # @return [Object] The current value of this option.
  def value
    provided? ? @value : default
  end

  private

  # :nodoc:
  def setup_forms(forms)
    self.short = !forms.empty? ? forms[0] : @name[0, 1]
    self.long = forms.length == 2 ? forms[1] : @name
  end

  # :nodoc:
  def setup_options(options)
    (options.is_a?(::Hash) ? options : {}).each_pair do |option, value|
      send("#{option}=", value) if respond_to?("#{option}=")
    end
  end

  # :nodoc:
  def setup_action(action)
    @action = action if action.present? && action.respond_to?(:call) && action.try(:arity) == 2
  end

  # :nodoc:
  def handle_set_failure(vs)
    locale = @parent.i18n

    message =
      case vs
      when "match" then locale.invalid_for_regexp(label, @validator.inspect)
      when "call" then locale.invalid_for_proc(label)
      else locale.invalid_value(label, Bovem::Parser.smart_join(@validator.ensure_array, separator: ", ", last_separator: locale.join_separator).html_safe)
      end

    raise Bovem::Errors::Error.new(self, :validation_failed, message)
  end

  # :nodoc:
  def get_validator_method(validator)
    case validator.class.to_s
    when "Array" then "include?"
    when "Regexp" then "match"
    when "Proc" then "call"
    else false
    end
  end
end

Instance Method Details

#complete_longString

Returns the long form with two dashes prepended.

Returns:

  • (String)

    The short form with two dashes prepended.



112
113
114
# File 'lib/bovem/option.rb', line 112

def complete_long
  "--#{@long}"
end

#complete_shortString

Returns the short form with a dash prepended.

Returns:

  • (String)

    The short form with a dash prepended.



105
106
107
# File 'lib/bovem/option.rb', line 105

def complete_short
  "-#{@short}"
end

#default?Boolean

Check if the current option has a default value.

Returns:

  • (Boolean)

    If the current option has a default value.



142
143
144
# File 'lib/bovem/option.rb', line 142

def default?
  !@default.nil?
end

#execute_actionObject

Executes the action associated to this option.



167
168
169
170
171
# File 'lib/bovem/option.rb', line 167

def execute_action
  return nil unless @action.present?
  @provided = true
  @action.call(parent, self)
end

#help?Boolean

Check if this command has a help.

Returns:

  • (Boolean)

    true if this command has a help, false otherwise.



190
191
192
# File 'lib/bovem/option.rb', line 190

def help?
  @help.present?
end

#labelString

Returns a label for this option, combining short and long forms.

Returns:

  • (String)

    A label for this option.



119
120
121
# File 'lib/bovem/option.rb', line 119

def label
  [complete_short, complete_long].compact.join("/")
end

#provided?Boolean

If this option was provided.

Returns:

  • (Boolean)

    true if this option was provided, false otherwise.



183
184
185
# File 'lib/bovem/option.rb', line 183

def provided?
  @provided
end

#requires_argument?Boolean

Checks if this option requires an argument.

Returns:

  • (Boolean)

    true if this option requires an argument, false otherwise.



176
177
178
# File 'lib/bovem/option.rb', line 176

def requires_argument?
  [String, Integer, Float, Array].include?(@type) && @action.blank?
end

#set(value, raise_error = true) ⇒ Boolean

Sets the value of this option and also make sure that it is validated.

Parameters:

  • value (Object)

    The new value of this option.

  • raise_error (Boolean) (defaults to: true)

    If raise an ArgumentError in case of validation errors.

Returns:

  • (Boolean)

    true if operation succeeded, false otherwise.



151
152
153
154
155
156
157
158
159
160
161
162
163
164
# File 'lib/bovem/option.rb', line 151

def set(value, raise_error = true)
  vs = get_validator_method(@validator)
  rv = vs ? @validator.send(vs, value) : true

  if rv
    @value = value
    @provided = true
  else # Validation failed
    @value = nil
    @provided = false
    handle_set_failure(vs) if raise_error
    false
  end
end