lib/rprogram/option.rb in rprogram-0.0.9 vs lib/rprogram/option.rb in rprogram-0.1.0
- old
+ new
@@ -8,19 +8,36 @@
attr_reader :equals
# Can the option be specified multiple times
attr_reader :multiple
- def initialize(opts={:flag => nil, :equals => false, :multiple => false},&block)
- @flag = opts[:flag]
+ #
+ # Creates a new Option object with the specified _options_. If a _block_
+ # is given it will be used for the custom formating of the option. If a
+ # _block_ is not given, the option will use the default_format when
+ # generating the arguments.
+ #
+ # _options_ may contain the following keys:
+ # <tt>:flag</tt>:: The command-line flag to use.
+ # <tt>:equals</tt>:: Implies the option maybe formated as '--flag=value'.
+ # Defaults to +falue+, if not given.
+ # <tt>:multuple</tt>:: Implies the option maybe given an Array of
+ # values. Defaults to +false+, if not given.
+ #
+ def initialize(options={},&block)
+ @flag = options[:flag]
- @equals = opts[:equals] || false
- @multiple = opts[:multiple] || false
+ @equals = options[:equals] || false
+ @multiple = options[:multiple] || false
@formating = block
end
+ #
+ # Returns an +Array+ of the arguments for the option with the specified
+ # _value_.
+ #
def arguments(value)
return [@flag] if value==true
return [] if (value==nil || value==false)
if value.kind_of?(Hash)
@@ -39,15 +56,23 @@
end
end
protected
+ #
+ # Returns an Array of the flag and the specified _value_ in argument
+ # form.
+ #
def default_format(value)
return [@flag] + value if value.kind_of?(Array)
return ["#{flag}=#{value}"] if @equals
return [@flag, value]
end
+ #
+ # Formats specified _value_ with the option flag using
+ # either the custom formating or the default_format.
+ #
def format(value)
if @formating
return @formating.call(@flag,value).to_a
else
return default_format(value)