Class: Optitron::Tokenizer

Inherits:
Object
  • Object
show all
Defined in:
lib/optitron/tokenizer.rb

Defined Under Namespace

Classes: Named, NamedWithValue, Value

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(parser, opts) ⇒ Tokenizer

Returns a new instance of Tokenizer.


4
5
6
7
# File 'lib/optitron/tokenizer.rb', line 4

def initialize(parser, opts)
  @parser, @opts = parser, opts
  tokenize
end

Instance Attribute Details

#tokensObject (readonly)

Returns the value of attribute tokens


3
4
5
# File 'lib/optitron/tokenizer.rb', line 3

def tokens
  @tokens
end

Instance Method Details

#find_names_values(short) ⇒ Object


27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/optitron/tokenizer.rb', line 27

def find_names_values(short)
  toks = []
  letters = short.split('')
  while !letters.empty?
    let = letters.shift
    toks << Named.new(let)
    if @parser.short_opts[let] && !@parser.short_opts[let].boolean? && !letters.empty?
      toks << Value.new(letters.join)
      letters.clear
    end
  end
  toks
end

#tokenizeObject


13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/optitron/tokenizer.rb', line 13

def tokenize
  unless @tokens
    @tokens = @opts.map do |t|
      case t
      when /^--([^-][^=]+)=([^=]+)$/ then NamedWithValue.new($1, $2)
      when /^--([^-][^=]+)$/         then NamedWithValue.new($1, nil)
      when /^-(.*)/                  then find_names_values($1)
      else                                Value.new(t)
      end
    end
    @tokens.flatten!
  end
end