lib/slop/option.rb in slop-1.6.1 vs lib/slop/option.rb in slop-1.7.0
- old
+ new
@@ -20,17 +20,12 @@
# @return [Object] true/false, or an optional help string to append
attr_reader :help
# @return [Boolean] true if this options argument value has been forced
- attr_reader :forced
+ attr_accessor :forced
- # @overload argument_value=(value)
- # Set this options argument value
- # @param [Object] value The value you'd like applied to this option
- attr_writer :argument_value
-
# @return [Integer] The amount of times this option has been invoked
attr_accessor :count
# @param [Slop] slop
# @param [String, #to_s] short
@@ -59,10 +54,11 @@
@tail = options[:tail]
@match = options[:match]
@delimiter = options.fetch(:delimiter, ',')
@limit = options.fetch(:limit, 0)
@help = options.fetch(:help, true)
+ @argument_type = options[:as].to_s.downcase
@forced = false
@argument_value = nil
@count = 0
@@ -85,19 +81,37 @@
# @return [String] either the long or short flag for this option
def key
@long_flag || @short_flag
end
+ # Set this options argument value.
+ #
+ # If this options argument type is expected to be an Array, this
+ # method will split the value and concat elements into the original
+ # argument value
+ #
+ # @param [Object] value The value to set this options argument to
+ def argument_value=(value)
+ if @argument_type == 'array'
+ @argument_value ||= []
+ if value.respond_to?(:to_str)
+ @argument_value.concat value.split(@delimiter, @limit)
+ end
+ else
+ @argument_value = value
+ end
+ end
+
# @return [Object] the argument value after it's been cast
# according to the `:as` option
def argument_value
return @argument_value if @forced
value = @argument_value || @options[:default]
return if value.nil?
- case @options[:as].to_s.downcase
- when 'array'; value.split @delimiter, @limit
+ case @argument_type
+ when 'array'; @argument_value
when 'range'; value_to_range value
when 'float'; value.to_s.to_f
when 'string', 'str'; value.to_s
when 'symbol', 'sym'; value.to_s.to_sym
when 'integer', 'int'; value.to_s.to_i
@@ -157,10 +171,10 @@
end
# @return [String]
def inspect
"#<Slop::Option short_flag=#{@short_flag.inspect} " +
- "long_flag=#{@long_flag.inspect} " +
+ "long_flag=#{@long_flag.inspect} argument=#{@argument.inspect} " +
"description=#{@description.inspect}>"
end
private