Class: Bovem::Option
- Inherits:
-
Object
- Object
- Bovem::Option
- Defined in:
- lib/bovem/option.rb
Overview
This class represents an option for a command.
Instance Attribute Summary (collapse)
-
- (Proc) action
The action associated to this option.
-
- (Object) default
Get the current default value for this option.
-
- (String) help
An help message for this option.
-
- (String) long
The long form (i.e.:
--help
) for this option. -
- (String|NilClass) meta
Returns the meta argument for this option.
-
- (String) name
The name of this option.
-
- (Command) parent
The parent of this option.
-
- (Boolean) required
If this option is required.
-
- (String) short
The short form (i.e.:
-h
) for this option. -
- (Class) type
The type of this option.
-
- (Array|Regexp) validator
Or A constraint for valid values.
-
- (Object) value
Get the current value for this option.
Instance Method Summary (collapse)
-
- (String) complete_long
Returns the long form with two dashes prepended.
-
- (String) complete_short
Returns the short form with a dash prepended.
-
- (Object) execute_action
Executes the action associated to this option.
-
- (Boolean) has_default?
Check if the current option has a default value.
-
- (Boolean) has_help?
Check if this command has a help.
-
- (Option) initialize(name, forms = [], options = {}, &action)
constructor
Creates a new option.
-
- (String) label
Returns a label for this option, combining short and long forms.
-
- (Boolean) provided?
If this option was provided.
-
- (Boolean) requires_argument?
Checks if this option requires an argument.
-
- (Boolean) set(value, raise_error = true)
Sets the value of this option and also make sure that it is validated.
Constructor Details
- (Option) initialize(name, forms = [], options = {}, &action)
Creates a new option.
62 63 64 65 66 67 68 |
# File 'lib/bovem/option.rb', line 62 def initialize(name, forms = [], = {}, &action) @name = name.ensure_string @provided = false setup_forms(forms) () setup_action(action) end |
Instance Attribute Details
- (Proc) action
Returns 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 236 237 238 239 240 241 242 243 244 245 246 247 248 249 |
# File 'lib/bovem/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 = [], = {}, &action) @name = name.ensure_string @provided = false setup_forms(forms) () 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, 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(nil, true, true, 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 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 || Bovem::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 = get_validator_method(@validator) rv = vs ? @validator.send(vs, value) : true if rv then @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 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 () (.is_a?(::Hash) ? : {}).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 == "include?" then raise Bovem::Errors::Error.new(self, :validation_failed, @parent.i18n.invalid_value(label, Bovem::Parser.smart_join(@validator))) else raise Bovem::Errors::Error.new(self, :validation_failed, @parent.i18n.invalid_for_regexp(label, @validator.is_a?(::Proc) ? "[FUNCTION]" : @validator.inspect)) end end # Gets the method required to verify a validator. # # @param validator [Object] The type of the validator. # @return [String] A method to call to verify the validator. 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 |
- (Object) default
Get the current 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 236 237 238 239 240 241 242 243 244 245 246 247 248 249 |
# File 'lib/bovem/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 = [], = {}, &action) @name = name.ensure_string @provided = false setup_forms(forms) () 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, 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(nil, true, true, 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 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 || Bovem::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 = get_validator_method(@validator) rv = vs ? @validator.send(vs, value) : true if rv then @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 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 () (.is_a?(::Hash) ? : {}).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 == "include?" then raise Bovem::Errors::Error.new(self, :validation_failed, @parent.i18n.invalid_value(label, Bovem::Parser.smart_join(@validator))) else raise Bovem::Errors::Error.new(self, :validation_failed, @parent.i18n.invalid_for_regexp(label, @validator.is_a?(::Proc) ? "[FUNCTION]" : @validator.inspect)) end end # Gets the method required to verify a validator. # # @param validator [Object] The type of the validator. # @return [String] A method to call to verify the validator. 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 |
- (String) help
Returns 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 236 237 238 239 240 241 242 243 244 245 246 247 248 249 |
# File 'lib/bovem/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 = [], = {}, &action) @name = name.ensure_string @provided = false setup_forms(forms) () 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, 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(nil, true, true, 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 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 || Bovem::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 = get_validator_method(@validator) rv = vs ? @validator.send(vs, value) : true if rv then @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 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 () (.is_a?(::Hash) ? : {}).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 == "include?" then raise Bovem::Errors::Error.new(self, :validation_failed, @parent.i18n.invalid_value(label, Bovem::Parser.smart_join(@validator))) else raise Bovem::Errors::Error.new(self, :validation_failed, @parent.i18n.invalid_for_regexp(label, @validator.is_a?(::Proc) ? "[FUNCTION]" : @validator.inspect)) end end # Gets the method required to verify a validator. # # @param validator [Object] The type of the validator. # @return [String] A method to call to verify the validator. 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 |
- (String) long
Returns 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 236 237 238 239 240 241 242 243 244 245 246 247 248 249 |
# File 'lib/bovem/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 = [], = {}, &action) @name = name.ensure_string @provided = false setup_forms(forms) () 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, 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(nil, true, true, 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 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 || Bovem::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 = get_validator_method(@validator) rv = vs ? @validator.send(vs, value) : true if rv then @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 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 () (.is_a?(::Hash) ? : {}).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 == "include?" then raise Bovem::Errors::Error.new(self, :validation_failed, @parent.i18n.invalid_value(label, Bovem::Parser.smart_join(@validator))) else raise Bovem::Errors::Error.new(self, :validation_failed, @parent.i18n.invalid_for_regexp(label, @validator.is_a?(::Proc) ? "[FUNCTION]" : @validator.inspect)) end end # Gets the method required to verify a validator. # # @param validator [Object] The type of the validator. # @return [String] A method to call to verify the validator. 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 |
- (String|NilClass) meta
Returns the meta argument 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 236 237 238 239 240 241 242 243 244 245 246 247 248 249 |
# File 'lib/bovem/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 = [], = {}, &action) @name = name.ensure_string @provided = false setup_forms(forms) () 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, 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(nil, true, true, 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 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 || Bovem::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 = get_validator_method(@validator) rv = vs ? @validator.send(vs, value) : true if rv then @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 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 () (.is_a?(::Hash) ? : {}).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 == "include?" then raise Bovem::Errors::Error.new(self, :validation_failed, @parent.i18n.invalid_value(label, Bovem::Parser.smart_join(@validator))) else raise Bovem::Errors::Error.new(self, :validation_failed, @parent.i18n.invalid_for_regexp(label, @validator.is_a?(::Proc) ? "[FUNCTION]" : @validator.inspect)) end end # Gets the method required to verify a validator. # # @param validator [Object] The type of the validator. # @return [String] A method to call to verify the validator. 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 |
- (String) name
Returns 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 236 237 238 239 240 241 242 243 244 245 246 247 248 249 |
# File 'lib/bovem/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 = [], = {}, &action) @name = name.ensure_string @provided = false setup_forms(forms) () 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, 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(nil, true, true, 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 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 || Bovem::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 = get_validator_method(@validator) rv = vs ? @validator.send(vs, value) : true if rv then @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 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 () (.is_a?(::Hash) ? : {}).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 == "include?" then raise Bovem::Errors::Error.new(self, :validation_failed, @parent.i18n.invalid_value(label, Bovem::Parser.smart_join(@validator))) else raise Bovem::Errors::Error.new(self, :validation_failed, @parent.i18n.invalid_for_regexp(label, @validator.is_a?(::Proc) ? "[FUNCTION]" : @validator.inspect)) end end # Gets the method required to verify a validator. # # @param validator [Object] The type of the validator. # @return [String] A method to call to verify the validator. 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 |
- (Command) parent
Returns 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 236 237 238 239 240 241 242 243 244 245 246 247 248 249 |
# File 'lib/bovem/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 = [], = {}, &action) @name = name.ensure_string @provided = false setup_forms(forms) () 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, 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(nil, true, true, 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 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 || Bovem::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 = get_validator_method(@validator) rv = vs ? @validator.send(vs, value) : true if rv then @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 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 () (.is_a?(::Hash) ? : {}).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 == "include?" then raise Bovem::Errors::Error.new(self, :validation_failed, @parent.i18n.invalid_value(label, Bovem::Parser.smart_join(@validator))) else raise Bovem::Errors::Error.new(self, :validation_failed, @parent.i18n.invalid_for_regexp(label, @validator.is_a?(::Proc) ? "[FUNCTION]" : @validator.inspect)) end end # Gets the method required to verify a validator. # # @param validator [Object] The type of the validator. # @return [String] A method to call to verify the validator. 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 |
- (Boolean) required
Returns 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 236 237 238 239 240 241 242 243 244 245 246 247 248 249 |
# File 'lib/bovem/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 = [], = {}, &action) @name = name.ensure_string @provided = false setup_forms(forms) () 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, 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(nil, true, true, 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 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 || Bovem::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 = get_validator_method(@validator) rv = vs ? @validator.send(vs, value) : true if rv then @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 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 () (.is_a?(::Hash) ? : {}).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 == "include?" then raise Bovem::Errors::Error.new(self, :validation_failed, @parent.i18n.invalid_value(label, Bovem::Parser.smart_join(@validator))) else raise Bovem::Errors::Error.new(self, :validation_failed, @parent.i18n.invalid_for_regexp(label, @validator.is_a?(::Proc) ? "[FUNCTION]" : @validator.inspect)) end end # Gets the method required to verify a validator. # # @param validator [Object] The type of the validator. # @return [String] A method to call to verify the validator. 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 |
- (String) short
Returns 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 236 237 238 239 240 241 242 243 244 245 246 247 248 249 |
# File 'lib/bovem/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 = [], = {}, &action) @name = name.ensure_string @provided = false setup_forms(forms) () 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, 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(nil, true, true, 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 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 || Bovem::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 = get_validator_method(@validator) rv = vs ? @validator.send(vs, value) : true if rv then @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 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 () (.is_a?(::Hash) ? : {}).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 == "include?" then raise Bovem::Errors::Error.new(self, :validation_failed, @parent.i18n.invalid_value(label, Bovem::Parser.smart_join(@validator))) else raise Bovem::Errors::Error.new(self, :validation_failed, @parent.i18n.invalid_for_regexp(label, @validator.is_a?(::Proc) ? "[FUNCTION]" : @validator.inspect)) end end # Gets the method required to verify a validator. # # @param validator [Object] The type of the validator. # @return [String] A method to call to verify the validator. 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 |
- (Class) type
Returns 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 236 237 238 239 240 241 242 243 244 245 246 247 248 249 |
# File 'lib/bovem/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 = [], = {}, &action) @name = name.ensure_string @provided = false setup_forms(forms) () 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, 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(nil, true, true, 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 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 || Bovem::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 = get_validator_method(@validator) rv = vs ? @validator.send(vs, value) : true if rv then @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 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 () (.is_a?(::Hash) ? : {}).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 == "include?" then raise Bovem::Errors::Error.new(self, :validation_failed, @parent.i18n.invalid_value(label, Bovem::Parser.smart_join(@validator))) else raise Bovem::Errors::Error.new(self, :validation_failed, @parent.i18n.invalid_for_regexp(label, @validator.is_a?(::Proc) ? "[FUNCTION]" : @validator.inspect)) end end # Gets the method required to verify a validator. # # @param validator [Object] The type of the validator. # @return [String] A method to call to verify the validator. 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 |
- (Array|Regexp) validator
Returns 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 236 237 238 239 240 241 242 243 244 245 246 247 248 249 |
# File 'lib/bovem/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 = [], = {}, &action) @name = name.ensure_string @provided = false setup_forms(forms) () 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, 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(nil, true, true, 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 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 || Bovem::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 = get_validator_method(@validator) rv = vs ? @validator.send(vs, value) : true if rv then @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 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 () (.is_a?(::Hash) ? : {}).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 == "include?" then raise Bovem::Errors::Error.new(self, :validation_failed, @parent.i18n.invalid_value(label, Bovem::Parser.smart_join(@validator))) else raise Bovem::Errors::Error.new(self, :validation_failed, @parent.i18n.invalid_for_regexp(label, @validator.is_a?(::Proc) ? "[FUNCTION]" : @validator.inspect)) end end # Gets the method required to verify a validator. # # @param validator [Object] The type of the validator. # @return [String] A method to call to verify the validator. 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 |
- (Object) value
Get the current 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 236 237 238 239 240 241 242 243 244 245 246 247 248 249 |
# File 'lib/bovem/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 = [], = {}, &action) @name = name.ensure_string @provided = false setup_forms(forms) () 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, 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(nil, true, true, 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 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 || Bovem::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 = get_validator_method(@validator) rv = vs ? @validator.send(vs, value) : true if rv then @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 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 () (.is_a?(::Hash) ? : {}).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 == "include?" then raise Bovem::Errors::Error.new(self, :validation_failed, @parent.i18n.invalid_value(label, Bovem::Parser.smart_join(@validator))) else raise Bovem::Errors::Error.new(self, :validation_failed, @parent.i18n.invalid_for_regexp(label, @validator.is_a?(::Proc) ? "[FUNCTION]" : @validator.inspect)) end end # Gets the method required to verify a validator. # # @param validator [Object] The type of the validator. # @return [String] A method to call to verify the validator. 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
- (String) complete_long
Returns the long form with two dashes prepended.
113 114 115 |
# File 'lib/bovem/option.rb', line 113 def complete_long "--#{@long}" end |
- (String) complete_short
Returns the short form with a dash prepended.
106 107 108 |
# File 'lib/bovem/option.rb', line 106 def complete_short "-#{@short}" end |
- (Object) execute_action
Executes the action associated to this option.
166 167 168 169 170 171 |
# File 'lib/bovem/option.rb', line 166 def execute_action if @action.present? then @provided = true @action.call(parent, self) end end |
- (Boolean) has_default?
Check if the current option has a default value.
141 142 143 |
# File 'lib/bovem/option.rb', line 141 def has_default? !@default.nil? end |
- (Boolean) has_help?
Check if this command has a help.
190 191 192 |
# File 'lib/bovem/option.rb', line 190 def has_help? @help.present? end |
- (String) label
Returns a label for this option, combining short and long forms.
120 121 122 |
# File 'lib/bovem/option.rb', line 120 def label [complete_short, complete_long].compact.join("/") end |
- (Boolean) provided?
If this option was provided.
183 184 185 |
# File 'lib/bovem/option.rb', line 183 def provided? @provided end |
- (Boolean) requires_argument?
Checks if this option requires an argument.
176 177 178 |
# File 'lib/bovem/option.rb', line 176 def requires_argument? [String, Integer, Float, Array].include?(@type) && @action.blank? end |
- (Boolean) set(value, raise_error = true)
Sets the value of this option and also make sure that it is validated.
150 151 152 153 154 155 156 157 158 159 160 161 162 163 |
# File 'lib/bovem/option.rb', line 150 def set(value, raise_error = true) vs = get_validator_method(@validator) rv = vs ? @validator.send(vs, value) : true if rv then @value = value @provided = true else # Validation failed @value = nil @provided = false handle_set_failure(vs) if raise_error false end end |