Class: GetOptions
Defined Under Namespace
Classes: OptionDefinition, ParseError
Instance Method Summary collapse
-
#[](k) ⇒ Object
:call-seq: opt -> value.
-
#each ⇒ Object
:call-seq: opt.each { |key,val| block } -> Hash .
-
#has_option?(k) ⇒ Boolean
:call-seq: has_option?(key) -> true or false.
-
#initialize(option_specs, input = ARGV) ⇒ GetOptions
constructor
:call-seq: new(option_specs, input = ARGV) -> opt.
- #inspect ⇒ Object
- #to_s ⇒ Object
Constructor Details
#initialize(option_specs, input = ARGV) ⇒ GetOptions
:call-seq:
new(option_specs, input = ARGV) -> opt
Parse input based on metadata in option_specs.
Examples
opt = GetOptions.new(%w(help verbose! strarg=s))
puts "I'm going to go do stuff..." if (opt.verbose)
...
143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 |
# File 'lib/getoptions.rb', line 143 def initialize(option_specs, input = ARGV) build_dict(option_specs) @options = {} leftover = [] until input.empty? arg = input.shift case arg # Stop if you hit -- when '--' break # Long form when /^--(\S+)/ o, a = $1.split('=', 2) input.unshift(a) if a input = process_arguments(o, input) # Short form when /^-(\S+)/ o, a = $1.split('=', 2) input.unshift(a) if a o.scan(/./) do |c| input = process_arguments(c, input) end # Not an option, leave it else leftover << arg end end # Put what didn't parse back into input input.concat(leftover) end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method, *args) ⇒ Object (private)
Wrapper to hash accessor
242 243 244 |
# File 'lib/getoptions.rb', line 242 def method_missing(method, *args) #:nodoc: self[method] end |
Instance Method Details
#[](k) ⇒ Object
:call-seq:
opt[key] -> value
Returns the value of the specified option. If the option was in the specification but not found in the input data, nill is returned.
186 187 188 189 190 191 192 193 194 195 196 |
# File 'lib/getoptions.rb', line 186 def [](k) raise ParseError.new("`nil' cannot be an option key") if (k.nil?) sym = k.to_sym key = k.to_s case when @options.has_key?(sym); @options[sym] when @dict.has_key?(key); (@dict[key].option_type == :increment) ? 0 : nil else raise ParseError.new("program tried to access an unknown option: #{key.inspect}") end end |
#each ⇒ Object
:call-seq:
opt.each { |key,val| block } -> Hash
Iterate over each parsed option name and value.
Examples
opt.each do |key,val|
puts "#{key} -> #{val.inspect}"
end
233 234 235 236 237 |
# File 'lib/getoptions.rb', line 233 def each @options.each do |key,value| yield key.to_s, value end end |
#has_option?(k) ⇒ Boolean
:call-seq:
has_option?(key) -> true or false
Returns true if the specified key exists in the option input.
Examples
opt = GetOptions.new(%w(help verbose! strarg=s))
puts "I'm going to go do stuff..." if (opt.has_option(:verbose))
puts "I don't exist..." if (opt.has_option(:bogus))
...
210 211 212 213 |
# File 'lib/getoptions.rb', line 210 def has_option?(k) raise ParseError.new("`nil' cannot be an option key") if (k.nil?) @options.has_key?(k.to_sym) end |
#inspect ⇒ Object
217 218 219 220 221 |
# File 'lib/getoptions.rb', line 217 def inspect @options.sort_by{|k| k.to_s}.collect do |key, val| "%s: %s" % [key.inspect, val.inspect] end.join($/) end |
#to_s ⇒ Object
215 |
# File 'lib/getoptions.rb', line 215 alias to_s inspect |