lib/rprogram/option.rb in rprogram-0.1.1 vs lib/rprogram/option.rb in rprogram-0.1.2
- old
+ new
@@ -1,5 +1,7 @@
+require 'rprogram/extensions'
+
module RProgram
class Option
# Flag of the option
attr_reader :flag
@@ -11,32 +13,42 @@
attr_reader :multiple
# Argument separator
attr_reader :separator
+ # Does the option contain sub-options
+ attr_reader :sub_options
+
#
# 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:
+ # _options_ must contain the following key:
# <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
+ #
+ # _options_ may also contain the following keys:
+ # <tt>:equals</tt>:: Implies the option maybe formated as
+ # <tt>"--flag=value"</tt>. Defaults to +falue+, if
+ # not given.
+ # <tt>:multuple</tt>:: Specifies the option maybe given an Array of
# values. Defaults to +false+, if not given.
# <tt>:separator</tt>:: The separator to use for formating multiple
# arguments into one +String+. Cannot be used
- # with +:multiple+.
+ # with <tt>:multiple</tt>.
+ # <tt>:sub_options</tt>:: Specifies that the option contains
+ # sub-options. Defaults to false, if not given.
#
+ #
def initialize(options={},&block)
@flag = options[:flag]
@equals = options[:equals] || false
@multiple = options[:multiple] || false
@separator = options[:separator]
+ @sub_options = options[:sub_options] || false
@formating = block
end
#
@@ -45,27 +57,24 @@
#
def arguments(value)
return [@flag] if value==true
return [] if (value==nil || value==false)
- if value.kind_of?(Hash)
- value = value.map { |name,value| "#{name}=#{value}" }
- elsif value.kind_of?(Array)
- value = value.compact
+ if value.respond_to?(:arguments)
+ value = value.arguments
end
if @multiple
- args = []
-
- value.each { |arg| args += format(arg) }
- return args
- else
- if (value.kind_of?(Array) && @separator)
- value = value.join(@separator)
+ if value.respond_to?(:map)
+ return value.map { |arg| format(arg) }
end
+ end
- return format(value)
+ if (value.kind_of?(Array) && @separator)
+ value = value.join(@separator)
end
+
+ return format(value)
end
protected
#