Class: Mamertes::Option

Inherits:
Object
  • Object
show all
Defined in:
lib/mamertes/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.



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

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.



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
# File 'lib/mamertes/option.rb', line 42

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] if !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 if !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 or a Regexp.
  #
  # @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(nil, true, true, true, :ensure_string) if !value.nil? && !value.is_a?(Regexp)
    @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
    requires_argument? ? (@meta.present? ? @meta : @name.upcase) : nil
  end

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

  # Check if the current option has a default value.
  #
  # @return [Boolean] If the current option has a default value.
  def has_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 = @validator.present? ? (@validator.is_a?(Array) ? :array : :regexp) : false # Check we have a validator
    rv = vs ? @validator.send(vs == :array ? "include?" : "match", value) : true

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

  # Executes the action associated to this option.
  def execute_action
    if @action.present? then
      @provided = true
      @action.call(parent, self)
    end
  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 has_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
    # Setups the forms of the this option.
    #
    # @param forms [Array] An array of short and long forms for this option. Missing forms will be inferred by the name.
    def setup_forms(forms)
      self.short = forms.length > 0 ? forms[0] : @name[0, 1]
      self.long = forms.length == 2 ? forms[1] : @name
    end

    # Setups the settings of the this option.
    #
    # @param options [Hash] The settings for this option.
    def setup_options(options)
      (options.is_a?(::Hash) ? options : {}).each_pair do |option, value|
        send("#{option}=", value) if respond_to?("#{option}=")
      end
    end

    # Setups the action of the this option.
    #
    # @param action [Proc] The action of this option.
    def setup_action(action)
      @action = action if action.present? && action.respond_to?(:call) && action.try(:arity) == 2
    end

    # Handle failure in setting an option.
    #
    # @param vs [Symbol] The type of validator.
    def handle_set_failure(vs)
      if vs == :array then
        raise ::Mamertes::Error.new(self, :validation_failed, @parent.i18n.invalid_value(label, ::Mamertes::Parser.smart_join(@validator)))
      else
        raise ::Mamertes::Error.new(self, :validation_failed, @parent.i18n.invalid_for_regexp(label, @validator.inspect))
      end
    end
end

#defaultObject

Get the current default value for this option.

Returns:

  • (Object)

    The default value for this option.



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
# File 'lib/mamertes/option.rb', line 42

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] if !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 if !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 or a Regexp.
  #
  # @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(nil, true, true, true, :ensure_string) if !value.nil? && !value.is_a?(Regexp)
    @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
    requires_argument? ? (@meta.present? ? @meta : @name.upcase) : nil
  end

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

  # Check if the current option has a default value.
  #
  # @return [Boolean] If the current option has a default value.
  def has_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 = @validator.present? ? (@validator.is_a?(Array) ? :array : :regexp) : false # Check we have a validator
    rv = vs ? @validator.send(vs == :array ? "include?" : "match", value) : true

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

  # Executes the action associated to this option.
  def execute_action
    if @action.present? then
      @provided = true
      @action.call(parent, self)
    end
  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 has_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
    # Setups the forms of the this option.
    #
    # @param forms [Array] An array of short and long forms for this option. Missing forms will be inferred by the name.
    def setup_forms(forms)
      self.short = forms.length > 0 ? forms[0] : @name[0, 1]
      self.long = forms.length == 2 ? forms[1] : @name
    end

    # Setups the settings of the this option.
    #
    # @param options [Hash] The settings for this option.
    def setup_options(options)
      (options.is_a?(::Hash) ? options : {}).each_pair do |option, value|
        send("#{option}=", value) if respond_to?("#{option}=")
      end
    end

    # Setups the action of the this option.
    #
    # @param action [Proc] The action of this option.
    def setup_action(action)
      @action = action if action.present? && action.respond_to?(:call) && action.try(:arity) == 2
    end

    # Handle failure in setting an option.
    #
    # @param vs [Symbol] The type of validator.
    def handle_set_failure(vs)
      if vs == :array then
        raise ::Mamertes::Error.new(self, :validation_failed, @parent.i18n.invalid_value(label, ::Mamertes::Parser.smart_join(@validator)))
      else
        raise ::Mamertes::Error.new(self, :validation_failed, @parent.i18n.invalid_for_regexp(label, @validator.inspect))
      end
    end
end

#helpString

Returns An help message for this option.

Returns:

  • (String)

    An help message for this option.



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
# File 'lib/mamertes/option.rb', line 42

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] if !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 if !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 or a Regexp.
  #
  # @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(nil, true, true, true, :ensure_string) if !value.nil? && !value.is_a?(Regexp)
    @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
    requires_argument? ? (@meta.present? ? @meta : @name.upcase) : nil
  end

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

  # Check if the current option has a default value.
  #
  # @return [Boolean] If the current option has a default value.
  def has_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 = @validator.present? ? (@validator.is_a?(Array) ? :array : :regexp) : false # Check we have a validator
    rv = vs ? @validator.send(vs == :array ? "include?" : "match", value) : true

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

  # Executes the action associated to this option.
  def execute_action
    if @action.present? then
      @provided = true
      @action.call(parent, self)
    end
  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 has_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
    # Setups the forms of the this option.
    #
    # @param forms [Array] An array of short and long forms for this option. Missing forms will be inferred by the name.
    def setup_forms(forms)
      self.short = forms.length > 0 ? forms[0] : @name[0, 1]
      self.long = forms.length == 2 ? forms[1] : @name
    end

    # Setups the settings of the this option.
    #
    # @param options [Hash] The settings for this option.
    def setup_options(options)
      (options.is_a?(::Hash) ? options : {}).each_pair do |option, value|
        send("#{option}=", value) if respond_to?("#{option}=")
      end
    end

    # Setups the action of the this option.
    #
    # @param action [Proc] The action of this option.
    def setup_action(action)
      @action = action if action.present? && action.respond_to?(:call) && action.try(:arity) == 2
    end

    # Handle failure in setting an option.
    #
    # @param vs [Symbol] The type of validator.
    def handle_set_failure(vs)
      if vs == :array then
        raise ::Mamertes::Error.new(self, :validation_failed, @parent.i18n.invalid_value(label, ::Mamertes::Parser.smart_join(@validator)))
      else
        raise ::Mamertes::Error.new(self, :validation_failed, @parent.i18n.invalid_for_regexp(label, @validator.inspect))
      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.



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
# File 'lib/mamertes/option.rb', line 42

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] if !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 if !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 or a Regexp.
  #
  # @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(nil, true, true, true, :ensure_string) if !value.nil? && !value.is_a?(Regexp)
    @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
    requires_argument? ? (@meta.present? ? @meta : @name.upcase) : nil
  end

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

  # Check if the current option has a default value.
  #
  # @return [Boolean] If the current option has a default value.
  def has_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 = @validator.present? ? (@validator.is_a?(Array) ? :array : :regexp) : false # Check we have a validator
    rv = vs ? @validator.send(vs == :array ? "include?" : "match", value) : true

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

  # Executes the action associated to this option.
  def execute_action
    if @action.present? then
      @provided = true
      @action.call(parent, self)
    end
  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 has_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
    # Setups the forms of the this option.
    #
    # @param forms [Array] An array of short and long forms for this option. Missing forms will be inferred by the name.
    def setup_forms(forms)
      self.short = forms.length > 0 ? forms[0] : @name[0, 1]
      self.long = forms.length == 2 ? forms[1] : @name
    end

    # Setups the settings of the this option.
    #
    # @param options [Hash] The settings for this option.
    def setup_options(options)
      (options.is_a?(::Hash) ? options : {}).each_pair do |option, value|
        send("#{option}=", value) if respond_to?("#{option}=")
      end
    end

    # Setups the action of the this option.
    #
    # @param action [Proc] The action of this option.
    def setup_action(action)
      @action = action if action.present? && action.respond_to?(:call) && action.try(:arity) == 2
    end

    # Handle failure in setting an option.
    #
    # @param vs [Symbol] The type of validator.
    def handle_set_failure(vs)
      if vs == :array then
        raise ::Mamertes::Error.new(self, :validation_failed, @parent.i18n.invalid_value(label, ::Mamertes::Parser.smart_join(@validator)))
      else
        raise ::Mamertes::Error.new(self, :validation_failed, @parent.i18n.invalid_for_regexp(label, @validator.inspect))
      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.



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
# File 'lib/mamertes/option.rb', line 42

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] if !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 if !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 or a Regexp.
  #
  # @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(nil, true, true, true, :ensure_string) if !value.nil? && !value.is_a?(Regexp)
    @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
    requires_argument? ? (@meta.present? ? @meta : @name.upcase) : nil
  end

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

  # Check if the current option has a default value.
  #
  # @return [Boolean] If the current option has a default value.
  def has_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 = @validator.present? ? (@validator.is_a?(Array) ? :array : :regexp) : false # Check we have a validator
    rv = vs ? @validator.send(vs == :array ? "include?" : "match", value) : true

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

  # Executes the action associated to this option.
  def execute_action
    if @action.present? then
      @provided = true
      @action.call(parent, self)
    end
  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 has_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
    # Setups the forms of the this option.
    #
    # @param forms [Array] An array of short and long forms for this option. Missing forms will be inferred by the name.
    def setup_forms(forms)
      self.short = forms.length > 0 ? forms[0] : @name[0, 1]
      self.long = forms.length == 2 ? forms[1] : @name
    end

    # Setups the settings of the this option.
    #
    # @param options [Hash] The settings for this option.
    def setup_options(options)
      (options.is_a?(::Hash) ? options : {}).each_pair do |option, value|
        send("#{option}=", value) if respond_to?("#{option}=")
      end
    end

    # Setups the action of the this option.
    #
    # @param action [Proc] The action of this option.
    def setup_action(action)
      @action = action if action.present? && action.respond_to?(:call) && action.try(:arity) == 2
    end

    # Handle failure in setting an option.
    #
    # @param vs [Symbol] The type of validator.
    def handle_set_failure(vs)
      if vs == :array then
        raise ::Mamertes::Error.new(self, :validation_failed, @parent.i18n.invalid_value(label, ::Mamertes::Parser.smart_join(@validator)))
      else
        raise ::Mamertes::Error.new(self, :validation_failed, @parent.i18n.invalid_for_regexp(label, @validator.inspect))
      end
    end
end

#nameString

Returns The name of this option.

Returns:

  • (String)

    The name of this option.



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
# File 'lib/mamertes/option.rb', line 42

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] if !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 if !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 or a Regexp.
  #
  # @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(nil, true, true, true, :ensure_string) if !value.nil? && !value.is_a?(Regexp)
    @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
    requires_argument? ? (@meta.present? ? @meta : @name.upcase) : nil
  end

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

  # Check if the current option has a default value.
  #
  # @return [Boolean] If the current option has a default value.
  def has_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 = @validator.present? ? (@validator.is_a?(Array) ? :array : :regexp) : false # Check we have a validator
    rv = vs ? @validator.send(vs == :array ? "include?" : "match", value) : true

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

  # Executes the action associated to this option.
  def execute_action
    if @action.present? then
      @provided = true
      @action.call(parent, self)
    end
  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 has_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
    # Setups the forms of the this option.
    #
    # @param forms [Array] An array of short and long forms for this option. Missing forms will be inferred by the name.
    def setup_forms(forms)
      self.short = forms.length > 0 ? forms[0] : @name[0, 1]
      self.long = forms.length == 2 ? forms[1] : @name
    end

    # Setups the settings of the this option.
    #
    # @param options [Hash] The settings for this option.
    def setup_options(options)
      (options.is_a?(::Hash) ? options : {}).each_pair do |option, value|
        send("#{option}=", value) if respond_to?("#{option}=")
      end
    end

    # Setups the action of the this option.
    #
    # @param action [Proc] The action of this option.
    def setup_action(action)
      @action = action if action.present? && action.respond_to?(:call) && action.try(:arity) == 2
    end

    # Handle failure in setting an option.
    #
    # @param vs [Symbol] The type of validator.
    def handle_set_failure(vs)
      if vs == :array then
        raise ::Mamertes::Error.new(self, :validation_failed, @parent.i18n.invalid_value(label, ::Mamertes::Parser.smart_join(@validator)))
      else
        raise ::Mamertes::Error.new(self, :validation_failed, @parent.i18n.invalid_for_regexp(label, @validator.inspect))
      end
    end
end

#parentCommand

Returns The parent of this option.

Returns:

  • (Command)

    The parent of this option.



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
# File 'lib/mamertes/option.rb', line 42

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] if !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 if !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 or a Regexp.
  #
  # @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(nil, true, true, true, :ensure_string) if !value.nil? && !value.is_a?(Regexp)
    @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
    requires_argument? ? (@meta.present? ? @meta : @name.upcase) : nil
  end

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

  # Check if the current option has a default value.
  #
  # @return [Boolean] If the current option has a default value.
  def has_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 = @validator.present? ? (@validator.is_a?(Array) ? :array : :regexp) : false # Check we have a validator
    rv = vs ? @validator.send(vs == :array ? "include?" : "match", value) : true

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

  # Executes the action associated to this option.
  def execute_action
    if @action.present? then
      @provided = true
      @action.call(parent, self)
    end
  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 has_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
    # Setups the forms of the this option.
    #
    # @param forms [Array] An array of short and long forms for this option. Missing forms will be inferred by the name.
    def setup_forms(forms)
      self.short = forms.length > 0 ? forms[0] : @name[0, 1]
      self.long = forms.length == 2 ? forms[1] : @name
    end

    # Setups the settings of the this option.
    #
    # @param options [Hash] The settings for this option.
    def setup_options(options)
      (options.is_a?(::Hash) ? options : {}).each_pair do |option, value|
        send("#{option}=", value) if respond_to?("#{option}=")
      end
    end

    # Setups the action of the this option.
    #
    # @param action [Proc] The action of this option.
    def setup_action(action)
      @action = action if action.present? && action.respond_to?(:call) && action.try(:arity) == 2
    end

    # Handle failure in setting an option.
    #
    # @param vs [Symbol] The type of validator.
    def handle_set_failure(vs)
      if vs == :array then
        raise ::Mamertes::Error.new(self, :validation_failed, @parent.i18n.invalid_value(label, ::Mamertes::Parser.smart_join(@validator)))
      else
        raise ::Mamertes::Error.new(self, :validation_failed, @parent.i18n.invalid_for_regexp(label, @validator.inspect))
      end
    end
end

#requiredBoolean

Returns If this option is required.

Returns:

  • (Boolean)

    If this option is required.



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
# File 'lib/mamertes/option.rb', line 42

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] if !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 if !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 or a Regexp.
  #
  # @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(nil, true, true, true, :ensure_string) if !value.nil? && !value.is_a?(Regexp)
    @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
    requires_argument? ? (@meta.present? ? @meta : @name.upcase) : nil
  end

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

  # Check if the current option has a default value.
  #
  # @return [Boolean] If the current option has a default value.
  def has_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 = @validator.present? ? (@validator.is_a?(Array) ? :array : :regexp) : false # Check we have a validator
    rv = vs ? @validator.send(vs == :array ? "include?" : "match", value) : true

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

  # Executes the action associated to this option.
  def execute_action
    if @action.present? then
      @provided = true
      @action.call(parent, self)
    end
  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 has_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
    # Setups the forms of the this option.
    #
    # @param forms [Array] An array of short and long forms for this option. Missing forms will be inferred by the name.
    def setup_forms(forms)
      self.short = forms.length > 0 ? forms[0] : @name[0, 1]
      self.long = forms.length == 2 ? forms[1] : @name
    end

    # Setups the settings of the this option.
    #
    # @param options [Hash] The settings for this option.
    def setup_options(options)
      (options.is_a?(::Hash) ? options : {}).each_pair do |option, value|
        send("#{option}=", value) if respond_to?("#{option}=")
      end
    end

    # Setups the action of the this option.
    #
    # @param action [Proc] The action of this option.
    def setup_action(action)
      @action = action if action.present? && action.respond_to?(:call) && action.try(:arity) == 2
    end

    # Handle failure in setting an option.
    #
    # @param vs [Symbol] The type of validator.
    def handle_set_failure(vs)
      if vs == :array then
        raise ::Mamertes::Error.new(self, :validation_failed, @parent.i18n.invalid_value(label, ::Mamertes::Parser.smart_join(@validator)))
      else
        raise ::Mamertes::Error.new(self, :validation_failed, @parent.i18n.invalid_for_regexp(label, @validator.inspect))
      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.



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
# File 'lib/mamertes/option.rb', line 42

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] if !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 if !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 or a Regexp.
  #
  # @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(nil, true, true, true, :ensure_string) if !value.nil? && !value.is_a?(Regexp)
    @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
    requires_argument? ? (@meta.present? ? @meta : @name.upcase) : nil
  end

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

  # Check if the current option has a default value.
  #
  # @return [Boolean] If the current option has a default value.
  def has_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 = @validator.present? ? (@validator.is_a?(Array) ? :array : :regexp) : false # Check we have a validator
    rv = vs ? @validator.send(vs == :array ? "include?" : "match", value) : true

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

  # Executes the action associated to this option.
  def execute_action
    if @action.present? then
      @provided = true
      @action.call(parent, self)
    end
  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 has_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
    # Setups the forms of the this option.
    #
    # @param forms [Array] An array of short and long forms for this option. Missing forms will be inferred by the name.
    def setup_forms(forms)
      self.short = forms.length > 0 ? forms[0] : @name[0, 1]
      self.long = forms.length == 2 ? forms[1] : @name
    end

    # Setups the settings of the this option.
    #
    # @param options [Hash] The settings for this option.
    def setup_options(options)
      (options.is_a?(::Hash) ? options : {}).each_pair do |option, value|
        send("#{option}=", value) if respond_to?("#{option}=")
      end
    end

    # Setups the action of the this option.
    #
    # @param action [Proc] The action of this option.
    def setup_action(action)
      @action = action if action.present? && action.respond_to?(:call) && action.try(:arity) == 2
    end

    # Handle failure in setting an option.
    #
    # @param vs [Symbol] The type of validator.
    def handle_set_failure(vs)
      if vs == :array then
        raise ::Mamertes::Error.new(self, :validation_failed, @parent.i18n.invalid_value(label, ::Mamertes::Parser.smart_join(@validator)))
      else
        raise ::Mamertes::Error.new(self, :validation_failed, @parent.i18n.invalid_for_regexp(label, @validator.inspect))
      end
    end
end

#typeClass

Returns The type of this option.

Returns:

  • (Class)

    The type of this option.



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
# File 'lib/mamertes/option.rb', line 42

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] if !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 if !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 or a Regexp.
  #
  # @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(nil, true, true, true, :ensure_string) if !value.nil? && !value.is_a?(Regexp)
    @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
    requires_argument? ? (@meta.present? ? @meta : @name.upcase) : nil
  end

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

  # Check if the current option has a default value.
  #
  # @return [Boolean] If the current option has a default value.
  def has_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 = @validator.present? ? (@validator.is_a?(Array) ? :array : :regexp) : false # Check we have a validator
    rv = vs ? @validator.send(vs == :array ? "include?" : "match", value) : true

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

  # Executes the action associated to this option.
  def execute_action
    if @action.present? then
      @provided = true
      @action.call(parent, self)
    end
  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 has_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
    # Setups the forms of the this option.
    #
    # @param forms [Array] An array of short and long forms for this option. Missing forms will be inferred by the name.
    def setup_forms(forms)
      self.short = forms.length > 0 ? forms[0] : @name[0, 1]
      self.long = forms.length == 2 ? forms[1] : @name
    end

    # Setups the settings of the this option.
    #
    # @param options [Hash] The settings for this option.
    def setup_options(options)
      (options.is_a?(::Hash) ? options : {}).each_pair do |option, value|
        send("#{option}=", value) if respond_to?("#{option}=")
      end
    end

    # Setups the action of the this option.
    #
    # @param action [Proc] The action of this option.
    def setup_action(action)
      @action = action if action.present? && action.respond_to?(:call) && action.try(:arity) == 2
    end

    # Handle failure in setting an option.
    #
    # @param vs [Symbol] The type of validator.
    def handle_set_failure(vs)
      if vs == :array then
        raise ::Mamertes::Error.new(self, :validation_failed, @parent.i18n.invalid_value(label, ::Mamertes::Parser.smart_join(@validator)))
      else
        raise ::Mamertes::Error.new(self, :validation_failed, @parent.i18n.invalid_for_regexp(label, @validator.inspect))
      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.



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
# File 'lib/mamertes/option.rb', line 42

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] if !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 if !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 or a Regexp.
  #
  # @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(nil, true, true, true, :ensure_string) if !value.nil? && !value.is_a?(Regexp)
    @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
    requires_argument? ? (@meta.present? ? @meta : @name.upcase) : nil
  end

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

  # Check if the current option has a default value.
  #
  # @return [Boolean] If the current option has a default value.
  def has_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 = @validator.present? ? (@validator.is_a?(Array) ? :array : :regexp) : false # Check we have a validator
    rv = vs ? @validator.send(vs == :array ? "include?" : "match", value) : true

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

  # Executes the action associated to this option.
  def execute_action
    if @action.present? then
      @provided = true
      @action.call(parent, self)
    end
  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 has_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
    # Setups the forms of the this option.
    #
    # @param forms [Array] An array of short and long forms for this option. Missing forms will be inferred by the name.
    def setup_forms(forms)
      self.short = forms.length > 0 ? forms[0] : @name[0, 1]
      self.long = forms.length == 2 ? forms[1] : @name
    end

    # Setups the settings of the this option.
    #
    # @param options [Hash] The settings for this option.
    def setup_options(options)
      (options.is_a?(::Hash) ? options : {}).each_pair do |option, value|
        send("#{option}=", value) if respond_to?("#{option}=")
      end
    end

    # Setups the action of the this option.
    #
    # @param action [Proc] The action of this option.
    def setup_action(action)
      @action = action if action.present? && action.respond_to?(:call) && action.try(:arity) == 2
    end

    # Handle failure in setting an option.
    #
    # @param vs [Symbol] The type of validator.
    def handle_set_failure(vs)
      if vs == :array then
        raise ::Mamertes::Error.new(self, :validation_failed, @parent.i18n.invalid_value(label, ::Mamertes::Parser.smart_join(@validator)))
      else
        raise ::Mamertes::Error.new(self, :validation_failed, @parent.i18n.invalid_for_regexp(label, @validator.inspect))
      end
    end
end

#valueObject

Get the current value for this option.

Returns:

  • (Object)

    The current value of this option.



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
# File 'lib/mamertes/option.rb', line 42

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] if !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 if !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 or a Regexp.
  #
  # @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(nil, true, true, true, :ensure_string) if !value.nil? && !value.is_a?(Regexp)
    @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
    requires_argument? ? (@meta.present? ? @meta : @name.upcase) : nil
  end

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

  # Check if the current option has a default value.
  #
  # @return [Boolean] If the current option has a default value.
  def has_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 = @validator.present? ? (@validator.is_a?(Array) ? :array : :regexp) : false # Check we have a validator
    rv = vs ? @validator.send(vs == :array ? "include?" : "match", value) : true

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

  # Executes the action associated to this option.
  def execute_action
    if @action.present? then
      @provided = true
      @action.call(parent, self)
    end
  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 has_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
    # Setups the forms of the this option.
    #
    # @param forms [Array] An array of short and long forms for this option. Missing forms will be inferred by the name.
    def setup_forms(forms)
      self.short = forms.length > 0 ? forms[0] : @name[0, 1]
      self.long = forms.length == 2 ? forms[1] : @name
    end

    # Setups the settings of the this option.
    #
    # @param options [Hash] The settings for this option.
    def setup_options(options)
      (options.is_a?(::Hash) ? options : {}).each_pair do |option, value|
        send("#{option}=", value) if respond_to?("#{option}=")
      end
    end

    # Setups the action of the this option.
    #
    # @param action [Proc] The action of this option.
    def setup_action(action)
      @action = action if action.present? && action.respond_to?(:call) && action.try(:arity) == 2
    end

    # Handle failure in setting an option.
    #
    # @param vs [Symbol] The type of validator.
    def handle_set_failure(vs)
      if vs == :array then
        raise ::Mamertes::Error.new(self, :validation_failed, @parent.i18n.invalid_value(label, ::Mamertes::Parser.smart_join(@validator)))
      else
        raise ::Mamertes::Error.new(self, :validation_failed, @parent.i18n.invalid_for_regexp(label, @validator.inspect))
      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.



113
114
115
# File 'lib/mamertes/option.rb', line 113

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

#complete_shortString

Returns the short form with a dash prepended.

Returns:

  • (String)

    The short form with a dash prepended.



106
107
108
# File 'lib/mamertes/option.rb', line 106

def complete_short
  "-#{@short}"
end

#execute_actionObject

Executes the action associated to this option.



165
166
167
168
169
170
# File 'lib/mamertes/option.rb', line 165

def execute_action
  if @action.present? then
    @provided = true
    @action.call(parent, self)
  end
end

#has_default?Boolean

Check if the current option has a default value.

Returns:

  • (Boolean)

    If the current option has a default value.



141
142
143
# File 'lib/mamertes/option.rb', line 141

def has_default?
  !@default.nil?
end

#has_help?Boolean

Check if this command has a help.

Returns:

  • (Boolean)

    true if this command has a help, false otherwise.



189
190
191
# File 'lib/mamertes/option.rb', line 189

def has_help?
  @help.present?
end

#labelString

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

Returns:

  • (String)

    A label for this option.



120
121
122
# File 'lib/mamertes/option.rb', line 120

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.



182
183
184
# File 'lib/mamertes/option.rb', line 182

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.



175
176
177
# File 'lib/mamertes/option.rb', line 175

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.



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

def set(value, raise_error = true)
  vs = @validator.present? ? (@validator.is_a?(Array) ? :array : :regexp) : false # Check we have a validator
  rv = vs ? @validator.send(vs == :array ? "include?" : "match", value) : true

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