class Bovem::Option
This class represents an option for a command.
@attribute name
@return [String] The name of this option.
@attribute short
@return [String] The short form (i.e.: `-h`) for this option.
@attribute long
@return [String] The long form (i.e.: `--help`) for this option.
@attribute type
@return [Class] The type of this option.
@attribute required
@return [Boolean] If this option is required.
@attribute default
@return [Object] The default value of this option.
@attribute meta
@return [String] The META argument for this option, used only when showing the help.
@attribute help
@return [String] An help message for this option.
@attribute value
@return [Object] The current value of this option.
@attribute action
@return [Proc] The action associated to this option.
@attribute validator
@return [Array|Regexp] or A constraint for valid values. Can be an Array of valid values or a Regexp.
@attribute parent
@return [Command] The parent of this option.
Attributes
Public Class Methods
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.
# 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
Public Instance Methods
Returns the long form with two dashes prepended.
@return [String] The short form with two dashes prepended.
# File lib/bovem/option.rb, line 112 def complete_long "--#{@long}" end
Returns the short form with a dash prepended.
@return [String] The short form with a dash prepended.
# File lib/bovem/option.rb, line 105 def complete_short "-#{@short}" end
Check if the current option has a default value.
@return [Boolean] If the current option has a default value.
# File lib/bovem/option.rb, line 142 def default? !@default.nil? end
Executes the action associated to this option.
# File lib/bovem/option.rb, line 167 def execute_action return nil unless @action.present? @provided = true @action.call(parent, self) end
Check if this command has a help.
@return [Boolean] `true` if this command has a help, `false` otherwise.
# File lib/bovem/option.rb, line 190 def help? @help.present? end
Returns a label for this option, combining short and long forms.
@return [String] A label for this option.
# File lib/bovem/option.rb, line 119 def label [complete_short, complete_long].compact.join("/") end
Sets the long form of this option.
@param value [String] The short form of this option.
# File lib/bovem/option.rb, line 84 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
If this option was provided.
@return [Boolean] `true` if this option was provided, `false` otherwise.
# File lib/bovem/option.rb, line 183 def provided? @provided end
Checks if this option requires an argument.
@return [Boolean] `true` if this option requires an argument, `false` otherwise.
# File lib/bovem/option.rb, line 176 def requires_argument? [String, Integer, Float, Array].include?(@type) && @action.blank? 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.
# 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
Sets the short form of this option.
@param value [String] The short form of this option.
# File lib/bovem/option.rb, line 72 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. 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.
# File lib/bovem/option.rb, line 96 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