Class: RProgram::Option
- Inherits:
-
Object
- Object
- RProgram::Option
- Defined in:
- lib/rprogram/option.rb
Instance Attribute Summary collapse
-
#equals ⇒ Object
readonly
Is the option in equals format.
-
#flag ⇒ Object
readonly
Flag of the option.
-
#multiple ⇒ Object
readonly
Can the option be specified multiple times.
-
#separator ⇒ Object
readonly
Argument separator.
-
#sub_options ⇒ Object
readonly
Does the option contain sub-options.
Instance Method Summary collapse
-
#arguments(value) ⇒ Array
Formats the arguments for the option.
-
#initialize(options = {}) {|option, value| ... } ⇒ Option
constructor
Creates a new Option object with.
Constructor Details
#initialize(options = {}) {|option, value| ... } ⇒ Option
Creates a new Option object with. If a block is given it will be used for the custom formatting of the option. If a block is not given, the option will use the default_format when generating the arguments.
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/rprogram/option.rb', line 54 def initialize(={},&block) @flag = [:flag] @equals = ([:equals] || false) @multiple = ([:multiple] || false) @separator = if [:separator] [:separator] elsif [:equals] ' ' end @sub_options = ([:sub_options] || false) @formatter = if block block else Proc.new do |opt,value| if opt.equals ["#{opt.flag}=#{value.first}"] else [opt.flag] + value end end end end |
Instance Attribute Details
#equals ⇒ Object (readonly)
Is the option in equals format
8 9 10 |
# File 'lib/rprogram/option.rb', line 8 def equals @equals end |
#flag ⇒ Object (readonly)
Flag of the option
5 6 7 |
# File 'lib/rprogram/option.rb', line 5 def flag @flag end |
#multiple ⇒ Object (readonly)
Can the option be specified multiple times
11 12 13 |
# File 'lib/rprogram/option.rb', line 11 def multiple @multiple end |
#separator ⇒ Object (readonly)
Argument separator
14 15 16 |
# File 'lib/rprogram/option.rb', line 14 def separator @separator end |
#sub_options ⇒ Object (readonly)
Does the option contain sub-options
17 18 19 |
# File 'lib/rprogram/option.rb', line 17 def @sub_options end |
Instance Method Details
#arguments(value) ⇒ Array
Formats the arguments for the option.
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 |
# File 'lib/rprogram/option.rb', line 88 def arguments(value) return [@flag] if value == true return [] unless value value = value.arguments if value.respond_to?(:arguments) if value.kind_of?(Hash) value = value.map { |key,sub_value| if sub_value == true key.to_s elsif sub_value "#{key}=#{sub_value}" end } elsif value.kind_of?(Array) value.flatten! else value = [value] end value.compact! if @multiple return value.inject([]) do |args,value| arg = @formatter.call(self,[value]) args += arg if arg args end else value = [value.join(@separator)] if @separator return (@formatter.call(self,value) || []) end end |