Class: Kwartz::CommandOptions
- Inherits:
-
Object
- Object
- Kwartz::CommandOptions
- Defined in:
- lib/kwartz/main.rb
Overview
command option class
ex.
option_table = [
[?h, :help, nil],
[?v, :version, nil],
[?f, :file, "filename"],
]
properties = {}
= CommandOption.new(option_table, properties)
filenames = .parse_argv(ARGV)
p .help
p .version
p .file
Instance Method Summary collapse
- #[](key) ⇒ Object
- #[]=(key, val) ⇒ Object
- #char(key) ⇒ Object
- #chr(key) ⇒ Object
-
#initialize(option_table, properties = {}) ⇒ CommandOptions
constructor
A new instance of CommandOptions.
- #key?(key) ⇒ Boolean
- #parse_argv(argv) ⇒ Object
Constructor Details
#initialize(option_table, properties = {}) ⇒ CommandOptions
Returns a new instance of CommandOptions.
57 58 59 60 61 62 63 64 65 66 |
# File 'lib/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 |
Instance Method Details
#[](key) ⇒ Object
78 79 80 81 82 83 84 |
# File 'lib/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 |
#[]=(key, val) ⇒ Object
86 87 88 |
# File 'lib/kwartz/main.rb', line 86 def []=(key, val) instance_variable_set("@#{key}", val) end |
#char(key) ⇒ Object
94 95 96 97 |
# File 'lib/kwartz/main.rb', line 94 def char(key) entry = _find_entry(key) return entry && entry[0] end |
#chr(key) ⇒ Object
99 100 101 102 |
# File 'lib/kwartz/main.rb', line 99 def chr(key) ch = char(key) return ch ? ch.chr : '' end |
#key?(key) ⇒ Boolean
90 91 92 |
# File 'lib/kwartz/main.rb', line 90 def key?(key) return instance_variables.include?("@#{key}") end |
#parse_argv(argv) ⇒ Object
104 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 |
# File 'lib/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 |