Class: Choosy::Parser

Inherits:
Object
  • Object
show all
Defined in:
lib/choosy/parser.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(command, lazy = nil, terminals = nil) ⇒ Parser

Returns a new instance of Parser.



5
6
7
8
9
10
11
12
13
14
15
# File 'lib/choosy/parser.rb', line 5

def initialize(command, lazy=nil, terminals=nil)
  @command = command
  @lazy = lazy || false
  @terminals = terminals || []

  @flags = {}
  return if command.options.nil?
  command.options.each do |o|
    verify_option(o)
  end
end

Instance Attribute Details

#commandObject (readonly)

Returns the value of attribute command.



3
4
5
# File 'lib/choosy/parser.rb', line 3

def command
  @command
end

#flagsObject (readonly)

Returns the value of attribute flags.



3
4
5
# File 'lib/choosy/parser.rb', line 3

def flags
  @flags
end

#terminalsObject (readonly)

Returns the value of attribute terminals.



3
4
5
# File 'lib/choosy/parser.rb', line 3

def terminals
  @terminals
end

Instance Method Details

#lazy?Boolean

Returns:

  • (Boolean)


43
44
45
# File 'lib/choosy/parser.rb', line 43

def lazy?
  @lazy
end

#parse!(argv, result = nil) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/choosy/parser.rb', line 17

def parse!(argv, result=nil)
  index = 0
  result ||= ParseResult.new(@command, false)

  while index < argv.length
    case argv[index]
    when '-'
      if lazy?
        result.unparsed << '-'
        index += 1
      else
        raise Choosy::ParseError.new("Unfinished option '-'")
      end
    when '--'
      result.unparsed << '--' if lazy?
      index = parse_rest(argv, index, result)
    when /^-/
      index = parse_option(argv, index, result)
    else
      index = parse_arg(argv, index, result)
    end
  end

  result
end