Class: BrewDG::OptionParser

Inherits:
Object
  • Object
show all
Defined in:
lib/brew_dg/option_parser.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeOptionParser

Returns a new instance of OptionParser.



6
7
8
# File 'lib/brew_dg/option_parser.rb', line 6

def initialize
  @options = {}
end

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options.



10
11
12
# File 'lib/brew_dg/option_parser.rb', line 10

def options
  @options
end

Instance Method Details

#destination(file) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
# File 'lib/brew_dg/option_parser.rb', line 47

def destination(file)
  if file
    file = File.expand_path(file)

    if File.directory?(file)
      file = File.join(file, 'library.png')
    end
  end

  update(:destination, file || STDOUT)
end

#parse(args) ⇒ Object



12
13
14
# File 'lib/brew_dg/option_parser.rb', line 12

def parse(args)
  tap { parser.parse!(Array(args)) }
end

#parserObject



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/brew_dg/option_parser.rb', line 16

def parser
  all_types = [:required, :recommended, :optional, :build]

  ::OptionParser.new do |parser|
    parser.on('-a') do |a|
      update(:types, all_types) if a
    end

    parser.on('-t type1,type2', Array) do |types|
      types = Array(types).map(&:strip).map(&:downcase).map(&:intern)

      update(:types, types)
    end

    parser.on('-o [file]') do |file|
      destination(file)
    end

    parser.on('-O [file]') do |file|
      destination(file)
      update(:inverted, true)
    end
  end
end

#update(key, val) ⇒ Object



41
42
43
44
45
# File 'lib/brew_dg/option_parser.rb', line 41

def update(key, val)
  options.update(key => val) do |_, oldval, _|
    oldval
  end
end