Sha256: 15465fb6d6d51df28d8b4116947a2583b26650c0b9cadac5e64b36d394c06124

Contents?: true

Size: 1.25 KB

Versions: 9

Compression:

Stored size: 1.25 KB

Contents

require 'cxxproject/utils/printer'
require 'cxxproject/utils/exit_helper'

module Cxxproject


class Option
  attr_reader :param, :arg, :block
  def initialize(param, arg, &f)
    @param = param
    @arg = arg # true / false
    @block = f
    f
    
  end
end


class Parser
  
  def initialize(argv)
    @options = {}
    @argv = argv
  end
  
  def add_option(opt)
    @options[opt.param] = opt
  end
  
  def parse_internal(ignoreInvalid = true)
    pos = 0
    begin
      while pos < @argv.length do
        if not @options.include?@argv[pos]
          if ignoreInvalid
            pos = pos + 1
            next
          end
          raise "Option #{@argv[pos]} unknown"
        end
        option = @options[@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
        pos = pos + 1
      end
    rescue SystemExit => e
      raise
    rescue Exception => e
      Printer.printError e.message
      ExitHelper.exit(1)
    end
    
  end

end


end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
bake-toolkit-1.0.9 lib/option/parser.rb
bake-toolkit-1.0.8 lib/option/parser.rb
bake-toolkit-1.0.7 lib/option/parser.rb
bake-toolkit-1.0.6 lib/option/parser.rb
bake-toolkit-1.0.5 lib/option/parser.rb
bake-toolkit-1.0.4 lib/option/parser.rb
bake-toolkit-1.0.3 lib/option/parser.rb
bake-toolkit-1.0.2 lib/option/parser.rb
bake-toolkit-1.0.1 lib/option/parser.rb