summaryrefslogtreecommitdiffstats
path: root/CLI/ruby/bin/game-of-life
blob: 16dc0fa8174d53918ca7043f39f49d7c2c6d4a17 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#!/usr/bin/env ruby
# frozen_string_literal: true

require "game_of_life"
require "thor"

class CLI < Thor
  map %w[-v --version] => :version
  desc "--version, -v", "Prints the Game of Life version information"
  def version
    puts "Game of Life version #{GameOfLife::VERSION}"
  end

  default_task :start
  desc "start [OPTIONS]", "Start the Game of Life simulations"
  class_option "seed",  aliases: "-s", type: :numeric, banner: :SEED,
                        desc: "Specify the seed number to use as an initial state (default to random)."

  class_option "input", aliases: "-i", type: :string, banner: :VALUE,
                        desc: "Specify the path/URL for the file to use as an initial state. (used instead of seed)"

  class_option "width", type: :numeric, banner: :WIDTH,
                        desc: "Specify the width of generated universe. (default to terminal width)"

  class_option "height", type: :numeric, banner: :HEIGHT,
                         desc: "Specify the hight of generated universe. (default to terminal height)"

  class_option "dead-cell", type: :string, banner: :CHAR, default: "\s",
                            desc: "Specify the dead-cell representation"

  class_option "live-cell", type: :string, banner: :CHAR, default: "\u2588",
                            desc: "Specify the live-cell representation"

  class_option "delay", aliases: "-d", type: :numeric, banner: "Milli-Seconds", default: 50,
                        desc: "Specify the introduced delay between each generation"
  def start
    universe = GameOfLife.generate(GameOfLife.parsed_options(options))
    GameOfLife.run(universe)
  end

  class << self
    # Allow exit with status 1 on failure
    # Ref: https://github.com/erikhuda/thor/issues/244#issue-6116190
    def exit_on_failure?
      true
    end
  end
end

begin
  CLI.start(ARGV)
rescue SystemExit, Interrupt
  "Do nothing when user exits with Interrupt (CMD/Ctrl + C)"
rescue NotImplementedError, GameOfLife::Error => e
  puts e.message
  exit(-1)
end