Class Kwartz::CommandOptions
In: kwartz/main.rb
Parent: Object

command option class

ex.

 option_table = [
   [?h, :help,    nil],
   [?v, :version, nil],
   [?f, :file,    "filename"],
 ]
 properties = {}
 options = CommandOption.new(option_table, properties)
 filenames = options.parse_argv(ARGV)
 p options.help
 p options.version
 p options.file

Methods

[]   []=   char   chr   key?   new   parse_argv  

Public Class methods

[Source]

# File kwartz/main.rb, line 57
    def initialize(option_table, properties={})
      @_option_table = option_table
      @_properties = properties
      buf = []
      optchars = {}
      option_table.each do |char, key, argname|
        buf << "def #{key}; @#{key}; end; def #{key}=(val); @#{key}=val; end\n"
      end
      instance_eval buf.join
    end

Public Instance methods

[Source]

# File kwartz/main.rb, line 78
    def [](key)
      if key.is_a?(Fixnum)
        entry = _find_entry(key)  or return
        key = entry[1]
      end
      instance_variable_get("@#{key}")
    end

[Source]

# File kwartz/main.rb, line 86
    def []=(key, val)
      instance_variable_set("@#{key}", val)
    end

[Source]

# File kwartz/main.rb, line 94
    def char(key)
      entry = _find_entry(key)
      return entry && entry[0]
    end

[Source]

# File kwartz/main.rb, line 99
    def chr(key)
      ch = char(key)
      return ch ? ch.chr : ''
    end

[Source]

# File kwartz/main.rb, line 90
    def key?(key)
      return instance_variables.include?("@#{key}")
    end

[Source]

# File kwartz/main.rb, line 104
    def parse_argv(argv)
      properties = @_properties
      while !argv.empty? && argv[0][0] == ?-
        optstr = argv.shift
        optstr = optstr[1, optstr.length - 1]
        if optstr[0] == ?-           # properties
          unless optstr =~ /\A-([-\w]+)(?:=(.*))?/
            raise option_error("'-#{optstr}': invalid property pattern.")
          end
          pname = $1 ;  pvalue = $2
          case pvalue
          when nil                    ;  pvalue = true
          when /\A\d+\z/              ;  pvalue = pvalue.to_i
          when /\A\d+\.\d+\z/         ;  pvalue = pvalue.to_f
          when 'true', 'yes', 'on'    ;  pvalue = true
          when 'false', 'no', 'off'   ;  pvalue = false
          when 'nil', 'null'          ;  pvalue = nil
          when /\A'.*'\z/, /\A".*"\z/ ; pvalue = eval pvalue
          end
          properties[pname.intern] = pvalue
        else                         # command-line options
          while optstr && !optstr.empty?
            optchar = optstr[0]
            optstr = optstr[1, optstr.length - 1]
            entry = _find_entry(optchar)
            entry  or raise CommandOptionError.new("-#{optchar.chr}: unknown option.")
            char, key, argname = entry
            case argname
            when nil, false
              instance_variable_set("@#{key}", true)
            when String
              arg = optstr
              arg = argv.shift unless arg && !arg.empty?
              arg  or raise CommandOptionError.new("-#{optchar.chr}: #{argname} required.")
              instance_variable_set("@#{key}", arg)
              optstr = ''
            when true
              arg = optstr
              arg = true unless arg && !arg.empty?
              instance_variable_set("@#{key}", arg)
              optstr = ''
            else
              raise "** internal error **"
            end
          end #while
        end #if
      end #while
      filenames = argv
      return filenames
    end

[Validate]