diff options
| author | a14m <[email protected]> | 2020-11-29 16:50:58 +0100 |
|---|---|---|
| committer | a14m <[email protected]> | 2020-11-29 16:50:58 +0100 |
| commit | b1f4bdc35c5e9b47d40085c4e65ee403ab0195f9 (patch) | |
| tree | da2b493b89c543c406e6cf1d9631270945837c99 /CLI/ruby/bin | |
| parent | 97ec394950f6a319b9238467b1bf0d8d3d5ec548 (diff) | |
Add CLI default options parsing (Thor)
Diffstat (limited to 'CLI/ruby/bin')
| -rwxr-xr-x | CLI/ruby/bin/game-of-life | 43 |
1 files changed, 43 insertions, 0 deletions
diff --git a/CLI/ruby/bin/game-of-life b/CLI/ruby/bin/game-of-life index 4d62bac..8abd1e0 100755 --- a/CLI/ruby/bin/game-of-life +++ b/CLI/ruby/bin/game-of-life @@ -2,3 +2,46 @@ # frozen_string_literal: true require "game_of_life" +require "thor" +require "io/console" + +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", aliases: "-w", type: :numeric, banner: :WIDTH, + desc: "Specify the width of generated universe. (default to terminal width)" + + class_option "height", aliases: "-h", type: :numeric, banner: :HEIGHT, + desc: "Specify the hight of generated universe. (default to terminal height)" + def start + options_with_defaults = { + "height" => IO.console.winsize[0], + "width" => IO.console.winsize[1], + "seed" => rand(100_000), + }.merge(options) + + GameOfLife::Universe.new(options_with_defaults).run + 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 + +CLI.start(ARGV) |
