Class: Dice::Parser

Inherits:
Object
  • Object
show all
Defined in:
lib/dice/parser.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args) ⇒ Parser

Returns a new instance of Parser.



8
9
10
11
# File 'lib/dice/parser.rb', line 8

def initialize(args)
  @options = {}
  @args = args
end

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options.



6
7
8
# File 'lib/dice/parser.rb', line 6

def options
  @options
end

Instance Method Details

#callObject



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/dice/parser.rb', line 13

def call
  OptionParser.new do |parser|
    parser.banner = 'Usage: dice [options]'
    parser.version = Dice::VERSION

    parser.on('-f', '--faces N', Integer, 'Roll the dice with N faces.') do |faces|
      options[:faces] = faces
    end

    parser.on('-c', '--count COUNT', Integer, 'Roll the dice COUNT times.') do |count|
      options[:count] = count
    end

    parser.on('-h', '--help', 'Show this help message.') do
      puts parser
      exit
    end
  end.parse!(@args)
end