Class: RProgram::Option
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.
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 56 def initialize(={},&block) @flag = [:flag] @equals = ([:equals] || false) @multiple = ([:multiple] || false) @separator = if [:separator] then [:separator] elsif [:equals] then ' ' 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
10 11 12 |
# File 'lib/rprogram/option.rb', line 10 def equals @equals end |
#flag ⇒ Object (readonly)
Flag of the option
7 8 9 |
# File 'lib/rprogram/option.rb', line 7 def flag @flag end |
#multiple ⇒ Object (readonly)
Can the option be specified multiple times
13 14 15 |
# File 'lib/rprogram/option.rb', line 13 def multiple @multiple end |
#separator ⇒ Object (readonly)
Argument separator
16 17 18 |
# File 'lib/rprogram/option.rb', line 16 def separator @separator end |
#sub_options ⇒ Object (readonly)
Does the option contain sub-options
19 20 21 |
# File 'lib/rprogram/option.rb', line 19 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 |
# File 'lib/rprogram/option.rb', line 88 def arguments(value) case value when true [@flag] when false, nil [] else value = super(value) if @multiple args = [] value.each do |arg| args += Array(@formatter.call(self,[arg])) end return args else value = [value.join(@separator)] if @separator return Array(@formatter.call(self,value)) end end end |