Class: DO::Parser
- Inherits:
-
Hash
- Object
- Hash
- DO::Parser
- Defined in:
- lib/do/parser.rb
Instance Method Summary collapse
-
#initialize(*args) ⇒ Parser
constructor
A new instance of Parser.
Constructor Details
#initialize(*args) ⇒ Parser
Returns a new instance of Parser.
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/do/parser.rb', line 4 def initialize(*args) = {} args.each_with_index do |arg, i| case arg # --foo=bar when /=/ key, value = *arg.split("=") [key.sub(/^-{1,2}/,'').to_sym] = value # --no-foo when /^-{1,2}no-(.+)/ [$1.to_sym] = false # --foo bar # --foo # -foo when /^-{1,2}(.+)/ key = $1.to_sym value = args[i+1] && args[i+1] !~ /^-{1,2}/ ? args.delete_at(i+1) : true [key] = value end end # Automatically map values .each do |k, v| case v when /^true$/i then [k] = true when /^false$/i then [k] = false when /^\d+$/ then [k] = v.to_i when /^[\d\.]+$/ then [k] = v.to_f when /,/ then [k] = v.split(",") end end self.replace() end |