lib/bini/optparser.rb in bini-0.5.5 vs lib/bini/optparser.rb in bini-0.6.0
- old
+ new
@@ -1,43 +1,45 @@
require 'optparse'
module Bini
+ # An extension of [OptionParser] that behaves like a hash, with saving, loading, and
+ # mashing in other hashs.
class OptionParser < ::OptionParser
def initialize
super
@options = {}
on("-V", "--version", "Print version") { |version| @options[:version] = true}
end
- def parse!
+ # Parse out ARGV, includes a catch for returning version, otherwise just calls super.
+ def parse!(*argv)
super
- if @options[:version]
- if Bini.version
- puts Bini.version
- else
- puts "No version supplied."
- end
- exit 0
+ if Options[:version] && Bini.version
+ puts Bini.version
+ # don't exit if RSpec is around, we are in a testing environment.
+ exit 0 if !Object.constants.include? :RSpec
end
-
- mash Bini.config if Bini.config
end
-
# These are the hash like bits.
+ # Clear the contents of the builtin Hash.
def clear
@options.clear
end
- def [](k = nil)
- return @options[k] if k
+ # Get results from the builtin in Hash.
+ # @param [Symbol,String,nil] key Either a single key or nil for the entire hash.
+ # @return [Hash] a hash, empty or otherwise.
+ def [](key = nil)
+ return @options[key] if key
return @options if @options.any?
{}
end
+ # Set a key/value pair in the buildin Hash.
def []=(k,v)
@options[k] = v
end
# merge takes in a set of values and overwrites the previous values.
@@ -46,8 +48,10 @@
h.merge! @options
@options.clear
h.each {|k,v| self[k] = v}
end
end
- Options = OptionParser.new
+ # An automatically created entry point into the OptionParser class.
+ Options = Bini::OptionParser.new
end
+