Class: Kwartz::CommandOptions

Inherits:
Object
  • Object
show all
Defined in:
lib/kwartz/main.rb

Overview

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

Instance Method Summary collapse

Constructor Details

#initialize(option_table, properties = {}) ⇒ CommandOptions

Returns a new instance of CommandOptions.



58
59
60
61
62
63
64
65
66
67
# File 'lib/kwartz/main.rb', line 58

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

Instance Method Details

#[](key) ⇒ Object



79
80
81
82
83
84
85
# File 'lib/kwartz/main.rb', line 79

def [](key)
  if key.is_a?(Fixnum) || (key.is_a?(String) && key.length == 1)
    entry = _find_entry(key)  or return
    key = entry[1]
  end
  instance_variable_get("@#{key}")
end

#[]=(key, val) ⇒ Object



87
88
89
# File 'lib/kwartz/main.rb', line 87

def []=(key, val)
  instance_variable_set("@#{key}", val)
end

#char(key) ⇒ Object



95
96
97
98
# File 'lib/kwartz/main.rb', line 95

def char(key)
  entry = _find_entry(key)
  return entry && entry[0]
end

#chr(key) ⇒ Object



100
101
102
103
# File 'lib/kwartz/main.rb', line 100

def chr(key)
  ch = char(key)
  return ch ? ch.chr : ''
end

#key?(key) ⇒ Boolean

Returns:

  • (Boolean)


91
92
93
# File 'lib/kwartz/main.rb', line 91

def key?(key)
  return instance_variables.include?("@#{key}")
end

#parse_argv(argv) ⇒ Object



105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
# File 'lib/kwartz/main.rb', line 105

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