Sha256: 9fe97ee30c8ef3369e6d25bbbd5e0122f9bb90ebc320b2f6929558dc240aebe6

Contents?: true

Size: 1.37 KB

Versions: 25

Compression:

Stored size: 1.37 KB

Contents

require 'bake/toolchain/colorizing_formatter'
require 'common/exit_helper'

module Bake

  class Parser
    
    def initialize(argv)
      @arguments = {}
      @argv = argv
      @default = nil
    end
    
    def add_option(opt)
      @arguments[opt.param] = opt
    end
    
    def add_default(opt)
      @default = opt
    end
    
    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
  
  end

end

Version data entries

25 entries across 25 versions & 1 rubygems

Version Path
bake-toolkit-2.12.2 lib/common/options/parser.rb
bake-toolkit-2.12.1 lib/common/options/parser.rb
bake-toolkit-2.12.0 lib/common/options/parser.rb
bake-toolkit-2.11.4 lib/common/options/parser.rb
bake-toolkit-2.11.3 lib/common/options/parser.rb
bake-toolkit-2.11.2 lib/common/options/parser.rb
bake-toolkit-2.11.1 lib/common/options/parser.rb
bake-toolkit-2.11.0 lib/common/options/parser.rb
bake-toolkit-2.10.3 lib/common/options/parser.rb
bake-toolkit-2.10.2 lib/common/options/parser.rb
bake-toolkit-2.10.1 lib/common/options/parser.rb
bake-toolkit-2.10.0 lib/common/options/parser.rb
bake-toolkit-2.9.2 lib/common/options/parser.rb
bake-toolkit-2.9.1 lib/common/options/parser.rb
bake-toolkit-2.9.0 lib/common/options/parser.rb
bake-toolkit-2.8.0 lib/common/options/parser.rb
bake-toolkit-2.7.0 lib/common/options/parser.rb
bake-toolkit-2.5.0 lib/common/options/parser.rb
bake-toolkit-2.4.4 lib/common/options/parser.rb
bake-toolkit-2.4.3 lib/common/options/parser.rb