Module: OptionBinder::Switch
- Defined in:
- lib/optbind.rb
Class Method Summary collapse
- .parser_opts_from_hash(hash = {}, &handler) ⇒ Object
- .parser_opts_from_string(string = '', &handler) ⇒ Object
Class Method Details
.parser_opts_from_hash(hash = {}, &handler) ⇒ Object
116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 |
# File 'lib/optbind.rb', line 116 def self.parser_opts_from_hash(hash = {}, &handler) style = case (hash[:style] || hash[:mode]).to_s.downcase when 'required' then :REQUIRED when 'optional' then :OPTIONAL end pattern = hash[:pattern] || hash[:type] values = hash[:values] names = [hash[:long], hash[:longs]].flatten.map { |n| n.to_s.sub(/\A-{,2}/, '--') if n } names += [hash[:short], hash[:shorts]].flatten.map { |n| n.to_s.sub(/\A-{,2}/, '-') if n } names += [hash[:name], hash[:names]].flatten.map do |n| next unless n n = n.to_s next n if n[0] == '-' n[2] ? "--#{n}" : "-#{n}" end argument = (hash[:argument].to_s.sub(/\A\[=/, '=[') if hash[:argument]) description = ([hash[:description]].flatten * ' ' if hash[:description]) handler ||= hash[:handler] return ([style, pattern, values] + names + [argument, description]).compact, handler end |
.parser_opts_from_string(string = '', &handler) ⇒ Object
139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 |
# File 'lib/optbind.rb', line 139 def self.parser_opts_from_string(string = '', &handler) shorts, longs = [], [] while string.sub!(/\A(?:(?<short>-\w)\s+)/, '') shorts << $~[:short] end style, pattern, values, argument = nil while string.sub!(/\A(?:(?<long>--[\[\]\-\w]+[\]\w]+)?(?:(?<argument>\[?=[<(]\S+[)>]\.{,3}\]?)|\s+))/, '') longs << $~[:long] if $~[:long] next unless $~[:argument] argument = $~[:argument] style = argument[0] == '=' ? :REQUIRED : :OPTIONAL values = $~[:values].split('|') if argument =~ /\[?=\((?<values>\S*)\)\]?/ if values.nil? && argument =~ /\[?=<(?<name>\S+):(?<pattern>\S+)>\]?/ pattern = Module.const_get($~[:pattern]) rescue Regexp.new($~[:pattern]) argument = "=<#{$~[:name]}>" argument = "=[#{argument[1..-1]}]" if style == :OPTIONAL else argument.sub!(/\A\[=/, '=[') end end description = !string.empty? ? string.strip : nil return ([style, pattern, values] + shorts + longs + [argument, description]).compact, handler end |