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
# File 'lib/common/options/parser.rb', line 8

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

Instance Method Details

#add_option(params, block) ⇒ Object

def add_option(opt)

@arguments[opt.param] = opt

end



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

def add_option(params, block)
  params.each { |p| @arguments[p] = block }
end

#get_block(param) ⇒ Object



21
22
23
24
25
# File 'lib/common/options/parser.rb', line 21

def get_block(param)
  opt = @arguments[param]
  raise "Internal error in option handling" unless opt
  opt.block
end

#parse_internal(ignore_invalid = false) ⇒ Object



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
56
57
58
59
60
# File 'lib/common/options/parser.rb', line 27

def parse_internal(ignore_invalid = false)
  pos = 0
  begin
    while pos < @argv.length do
      if not @arguments.include?@argv[pos]
        index = @argv[pos].index('-')
        if index != nil and index == 0
          raise "Option #{@argv[pos]} unknown" if not ignore_invalid
        else
          @arguments[""].call(@argv[pos]) # default paramter without "-"
        end
      else
        option = @arguments[@argv[pos]]
        if option.parameters.length == 1
          if pos+1 < @argv.length and @argv[pos+1][0] != "-"
            option.call(@argv[pos+1])
            pos = pos + 1
          else
            raise "Argument for option #{@argv[pos]} missing" 
          end
        else
          option.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