module Options
Constants
- VERSION
Attributes
arguments[RW]
Public Class Methods
description()
click to toggle source
# File lib/options.rb, line 9 def description 'parse options from *args cleanly' end
for(hash)
click to toggle source
# File lib/options.rb, line 35 def for(hash) hash = case hash when Hash hash when Array Hash[*hash.flatten] when String, Symbol {hash => true} else hash.to_hash end normalize!(hash) ensure hash.extend(Options) unless hash.is_a?(Options) end
normalize(hash)
click to toggle source
# File lib/options.rb, line 19 def normalize(hash) normalize!(hash.dup) end
Also aliased as: to_options
normalize!(hash)
click to toggle source
# File lib/options.rb, line 13 def normalize!(hash) hash.keys.each{|key| hash[key.to_s.to_sym] = hash.delete(key) unless Symbol===key} hash end
Also aliased as: to_options!
parse(args)
click to toggle source
# File lib/options.rb, line 52 def parse(args) case args when Array args.extend(Arguments) unless args.is_a?(Arguments) [args, args.options.pop] when Hash Options.for(args) else raise ArgumentError, "`args` should be an Array or Hash" end end
stringify(hash)
click to toggle source
# File lib/options.rb, line 30 def stringify(hash) stringify!(hash) end
Also aliased as: stringified
stringify!(hash)
click to toggle source
# File lib/options.rb, line 24 def stringify!(hash) hash.keys.each{|key| hash[key.to_s] = hash.delete(key) unless String===key} hash end
Also aliased as: stringified!
version()
click to toggle source
# File lib/options.rb, line 5 def version Options::VERSION end
Public Instance Methods
delopt(key, default = nil)
click to toggle source
# File lib/options.rb, line 105 def delopt key, default = nil [ key ].flatten.each do |key| return delete(key) if has_key?(key) key = key.to_s return delete(key) if has_key?(key) key = key.to_sym return delete(key) if has_key?(key) end default end
delopts(*args)
click to toggle source
# File lib/options.rb, line 116 def delopts *args args.flatten.map{|arg| delopt arg} end
getopt(key, default = nil)
click to toggle source
# File lib/options.rb, line 73 def getopt key, default = nil [ key ].flatten.each do |key| return fetch(key) if has_key?(key) key = key.to_s return fetch(key) if has_key?(key) key = key.to_sym return fetch(key) if has_key?(key) end default end
getopts(*args)
click to toggle source
# File lib/options.rb, line 84 def getopts *args args.flatten.map{|arg| getopt arg} end
hasopt(key, default = nil)
click to toggle source
# File lib/options.rb, line 88 def hasopt key, default = nil [ key ].flatten.each do |key| return true if has_key?(key) key = key.to_s return true if has_key?(key) key = key.to_sym return true if has_key?(key) end default end
Also aliased as: hasopt?
hasopts(*args)
click to toggle source
# File lib/options.rb, line 100 def hasopts *args args.flatten.map{|arg| hasopt arg} end
Also aliased as: hasopts?
normalize()
click to toggle source
# File lib/options.rb, line 148 def normalize Options.normalize(self) end
Also aliased as: normalized, to_options
normalize!()
click to toggle source
# File lib/options.rb, line 142 def normalize! Options.normalize!(self) end
Also aliased as: normalized!, to_options!
pop()
click to toggle source
# File lib/options.rb, line 165 def pop pop! unless popped? self end
pop!()
click to toggle source
# File lib/options.rb, line 174 def pop! if arguments.last.is_a?(Hash) @popped = arguments.pop else @popped = true end end
popped?()
click to toggle source
# File lib/options.rb, line 170 def popped? defined?(@popped) and @popped end
select!(*a, &b)
click to toggle source
# File lib/options.rb, line 138 def select! *a, &b replace select(*a, &b).to_hash end
setopt(key, value = nil)
click to toggle source
# File lib/options.rb, line 120 def setopt key, value = nil [ key ].flatten.each do |key| return self[key]=value if has_key?(key) key = key.to_s return self[key]=value if has_key?(key) key = key.to_sym return self[key]=value if has_key?(key) end return self[key]=value end
Also aliased as: setopt!
setopts(opts)
click to toggle source
# File lib/options.rb, line 132 def setopts opts opts.each{|key, value| setopt key, value} opts end
Also aliased as: setopts!
stringify()
click to toggle source
# File lib/options.rb, line 159 def stringify Options.stringify(self) end
Also aliased as: stringified
stringify!()
click to toggle source
# File lib/options.rb, line 154 def stringify! Options.stringify!(self) end
Also aliased as: stringified!
to_options()
click to toggle source
# File lib/options.rb, line 69 def to_options keys.inject(Hash.new){|h,k| h.update k.to_s.to_sym => fetch(k)} end
to_options!()
click to toggle source
# File lib/options.rb, line 65 def to_options! replace to_options end
validate(*acceptable_options)
click to toggle source
Validates that the options provided are acceptable.
@param [Symbol] *acceptable_options List of options that are
allowed
# File lib/options.rb, line 186 def validate(*acceptable_options) remaining = (provided_options - acceptable_options).map{|opt| opt.to_s}.sort raise ArgumentError, "Unrecognized options: #{remaining.join(', ')}" unless remaining.empty? self end
Protected Instance Methods
provided_options()
click to toggle source
# File lib/options.rb, line 195 def provided_options normalize!.keys end