Class: Bini::OptionParser

Inherits:
OptionParser
  • Object
show all
Defined in:
lib/bini/optparser.rb

Overview

An extension of [OptionParser] that behaves like a hash, with saving, loading, and mashing in other hashs.

Instance Method Summary collapse

Constructor Details

#initializeOptionParser

Returns a new instance of OptionParser.



7
8
9
10
11
12
# File 'lib/bini/optparser.rb', line 7

def initialize
  super
  @options = {}

  on("-V", "--version", "Print version") { |version| @options[:version] = true}
end

Instance Method Details

#[](key = nil) ⇒ Hash

Get results from the builtin in Hash.

Parameters:

  • key (Symbol, String, nil) (defaults to: nil)

    Either a single key or nil for the entire hash.

Returns:

  • (Hash)

    a hash, empty or otherwise.



34
35
36
37
38
# File 'lib/bini/optparser.rb', line 34

def [](key = nil)
  return @options[key] if key
  return @options if @options.any?
  {}
end

#[]=(k, v) ⇒ Object

Set a key/value pair in the buildin Hash.



41
42
43
# File 'lib/bini/optparser.rb', line 41

def []=(k,v)
  @options[k] = v
end

#clearObject

Clear the contents of the builtin Hash.



27
28
29
# File 'lib/bini/optparser.rb', line 27

def clear
  @options.clear
end

#mash(h) ⇒ Object

merge takes in a set of values and overwrites the previous values. mash does this in reverse.



47
48
49
50
51
# File 'lib/bini/optparser.rb', line 47

def mash(h)
  h.merge! @options
  @options.clear
  h.each {|k,v| self[k] = v}
end

#parse!(*argv) ⇒ Object

Parse out ARGV, includes a catch for returning version, otherwise just calls super.



15
16
17
18
19
20
21
22
23
# File 'lib/bini/optparser.rb', line 15

def parse!(*argv)
  super

  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
end