class Bake::Parser

Public Class Methods

new(argv) click to toggle source
# File lib/common/options/parser.rb, line 8
def initialize(argv)
  @arguments = {}
  @argv = argv
end

Public Instance Methods

add_option(params, block) click to toggle source

def add_option(opt)

@arguments[opt.param] = opt

end

# File lib/common/options/parser.rb, line 17
def add_option(params, block)
  params.each { |p| @arguments[p] = block }
end
get_block(argument) click to toggle source
# File lib/common/options/parser.rb, line 27
def get_block(argument)
  arg = nil
  block = nil
  @arguments.each do |a, b|
    if argument.start_with?(a) && a != ""
      return [b, nil] if a == argument
      if b && b.parameters.length==3 && argument[a.length..-1].scan(/\A\d*\z/).length > 0
        block = b
        arg = argument[a.length..-1]
      end
    end
  end
  return [block, arg]
end
num_parameter?(argument) click to toggle source
# File lib/common/options/parser.rb, line 42
def num_parameter?(argument)
  b, inPlaceArg = get_block(argument)
  return 0 unless b
  return 0 if inPlaceArg
  return b.parameters.length
end
parse_internal(ignore_invalid, subOptions = nil) click to toggle source
# File lib/common/options/parser.rb, line 49
def parse_internal(ignore_invalid, subOptions = nil)
  pos = 0
  begin
    while pos < @argv.length do
      arg = @argv[pos]
      if not valid?arg

        # used in bake config, must be passed from bakery to bake
        if subOptions and subOptions.valid?arg
          num = subOptions.num_parameter?(arg)
          if num > 0
            if pos+1 < @argv.length and @argv[pos+1][0] != "-"
              pos = pos + 1
            else
              if num != 2 # default
                raise "Argument for option #{arg} missing"
              end
            end
          end
        end

        index = arg.index('-')
        if index != nil and index == 0
          raise "Option #{arg} unknown" if not ignore_invalid
        else
          @arguments[""].call(arg) # default parameter without "-"
        end
      else
        option, inPlaceArg = get_block(arg)
        hasArgument = (pos+1 < @argv.length and @argv[pos+1][0] != "-")
        if option.parameters.length == 3
          if (hasArgument || inPlaceArg)
            if inPlaceArg
              option.call(inPlaceArg, nil, nil)
            else
              ignore = option.call(@argv[pos+1], nil, nil) # do not use inplace value
              pos = pos + 1 if ignore != :ignore
            end
          else
            option.call(nil, nil, nil)
          end
        elsif option.parameters.length == 2
          if hasArgument
            option.call(@argv[pos+1], nil) # do not use default value
            pos = pos + 1
          else
            option.call(nil, nil) # use default value
          end
        elsif option.parameters.length == 1 && hasArgument
          option.call(@argv[pos+1])
          pos = pos + 1
        elsif option.parameters.length == 0
          option.call()
        else
          raise "Argument for option #{arg} missing"
        end
      end
      pos = pos + 1
    end
  rescue SystemExit => e
    raise
  rescue Exception => e
    Bake.formatter.printError("Error: " + e.message)
    ExitHelper.exit(1)
  end

end
valid?(argument) click to toggle source
# File lib/common/options/parser.rb, line 21
def valid?(argument)
  @arguments.any? { |a, b|
    argument == a || (a != "" && argument.start_with?(a) && (!b || (b.parameters.length==3 && argument[a.length..-1].scan(/\A\d*\z/).length > 0)))
  }
end