Class: BoggleSolver::Options

Inherits:
Object
  • Object
show all
Defined in:
lib/boggle_solver/options.rb

Constant Summary collapse

DEFAULT_DICTIONARY =
"/usr/share/dict/words"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(argv) ⇒ Options



10
11
12
13
14
15
16
# File 'lib/boggle_solver/options.rb', line 10

def initialize(argv)
  @dictionary = DEFAULT_DICTIONARY
  @mode = :default
  parse(argv)
  @input = argv[0]
  @mode = :given unless argv.empty?
end

Instance Attribute Details

#dictionaryObject (readonly)

Returns the value of attribute dictionary.



8
9
10
# File 'lib/boggle_solver/options.rb', line 8

def dictionary
  @dictionary
end

#inputObject (readonly)

Returns the value of attribute input.



8
9
10
# File 'lib/boggle_solver/options.rb', line 8

def input
  @input
end

#modeObject (readonly)

Returns the value of attribute mode.



8
9
10
# File 'lib/boggle_solver/options.rb', line 8

def mode
  @mode
end

Instance Method Details

#parse(argv) ⇒ Object



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

def parse(argv)
  OptionParser.new do |opts|
    opts.banner = "Usage:   boggle_solver [ options ] "
    opts.on("-d", "--dict path",  String, "Path to dictionary") do |dict|
      @dictionary = dict
    end
    opts.on("-h", "--help", "Show this message") do
      puts opts
      exit
    end
    opts.on("-r", "--random", "Use a random board") do
      @mode = :random
    end

    begin
      argv = ["-r"] if argv.empty?
      opts.parse!(argv)
    rescue OptionParser::ParseError => e
      STDERR.puts e.message, "\n", opts
      exit(-1)
    end
  end
end