Class: Optimus::Implementations::Standard::Parser

Inherits:
Parser
  • Object
show all
Defined in:
lib/optimus/implementations/standard.rb

Instance Attribute Summary

Attributes inherited from Parser

#implementation, #options

Instance Method Summary collapse

Methods inherited from Parser

#inspect

Constructor Details

#initialize(implementation, options = nil) ⇒ Parser

Returns a new instance of Parser.



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/optimus/implementations/standard.rb', line 35

def initialize (implementation, options=nil)
  super(implementation, options)
  
  if !@options[:separators]
    @options[:separators] = {}
  end
  
  if !@options[:separators][:long]
    @options[:separators][:long] = '--'
  end
  
  if !@options[:separators][:short]
    @options[:separators][:short] = '-'
  end
end

Instance Method Details

#parse(result, values, options = nil) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/optimus/implementations/standard.rb', line 51

def parse (result, values, options=nil)
  options   = @options.merge(options) if options.is_a? Hash
  active  = nil
  type    = nil
  
  values.each {|value|
    if active
      if value.match(/^#{Regexp.escape(@options[:separators][:long])}|#{Regexp.escape(@options[:separators][:short])}\w/)
        result.data[:parameters][active] = { :type => type, :value => true }
      else
        result.data[:parameters][active] = { :type => type, :value => value }
      end

      active = nil
    else
      if matches = value.match(/^#{Regexp.escape(@options[:separators][:long])}(\w+)$/)
        active = matches[1]
        type = :long
      elsif matches = value.match(/^#{Regexp.escape(@options[:separators][:short])}(\w+)$/)
        if matches[1].length == 1
          active = matches[1]
          type = :short
        else
          matches[1].chars.each {|char|
            result.data[:parameters][char] = { :type => :short, :value => true }
          }
        end
      else
        result.data[:arguments] << value
      end
    end
  }

  result.normalize
  result
end