Class: Bake::Parser

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

Direct Known Subclasses

BakeryOptions, Options, VsOptions

Instance Method Summary collapse

Constructor Details

#initialize(argv) ⇒ Parser

Returns a new instance of Parser.



8
9
10
11
12
# File 'lib/common/options/parser.rb', line 8

def initialize(argv)
  @arguments = {}
  @argv = argv
  @default = nil
end

Instance Method Details

#add_default(opt) ⇒ Object



18
19
20
# File 'lib/common/options/parser.rb', line 18

def add_default(opt)
  @default = opt
end

#add_option(opt) ⇒ Object



14
15
16
# File 'lib/common/options/parser.rb', line 14

def add_option(opt)
  @arguments[opt.param] = opt
end

#parse_internal(ignoreInvalid = true) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/common/options/parser.rb', line 22

def parse_internal(ignoreInvalid = true)
  pos = 0
  begin
    while pos < @argv.length do
      if not @arguments.include?@argv[pos]
        if @default
          res = @default.call(@argv[pos])
          if (not res and not ignoreInvalid)
            raise "Option #{@argv[pos]} unknown"
          end
        end
      else
        option = @arguments[@argv[pos]]
        if option.arg
          if pos+1 < @argv.length and @argv[pos+1][0] != "-"
            option.block.call(@argv[pos+1])
            pos = pos + 1
          else
            raise "Argument for option #{@argv[pos]} missing" 
          end
        else
          option.block.call()
        end
      end
      pos = pos + 1
    end
  rescue SystemExit => e
    raise
  rescue Exception => e
    Bake.formatter.printError("Error: " + e.message)
    ExitHelper.exit(1)
  end
  
end