proper options parsing

This commit is contained in:
Gwenhael Le Moine 2023-03-15 14:43:28 +01:00
parent a1f0d62984
commit 5ca2b0b27e
No known key found for this signature in database
GPG key ID: FDFE3669426707A7
2 changed files with 30 additions and 8 deletions

View file

@ -2,12 +2,13 @@
# frozen_string_literal: true # frozen_string_literal: true
require 'gosu' require 'gosu'
require 'optparse'
require 'minehunt' require 'minehunt'
class GosuHunt < Gosu::Window class GosuHunt < Gosu::Window
def initialize( number_of_mines = 20 ) def initialize( number_of_mines, scale )
@scale = 3 @scale = scale
@sprite_size = 6 @sprite_size = 6
@number_of_mines = number_of_mines @number_of_mines = number_of_mines
@ -114,7 +115,20 @@ class GosuHunt < Gosu::Window
end end
end end
number_of_mines = 20 options = { number_of_mines: 20,
number_of_mines = ARGV.first.to_i if ARGV.length == 1 scale: 3 }
OptionParser.new do |opts|
opts.banner = 'Usage: gosuhunt <options>'
GosuHunt.new( number_of_mines ).show opts.on( '-m', '--mines INT' ) do |nb_mines|
options[:number_of_mines] = nb_mines.to_i
end
opts.on( '-s', '--scale INT' ) do |scale|
options[:scale] = scale.to_i
end
end
.parse!
GosuHunt.new( options[:number_of_mines], options[:scale] )
.show

View file

@ -2,6 +2,7 @@
# frozen_string_literal: true # frozen_string_literal: true
require 'curses' require 'curses'
require 'optparse'
require 'minehunt' require 'minehunt'
@ -108,7 +109,14 @@ def ncurses_main( number_of_mines )
exit! exit!
end end
number_of_mines = 20 options = { number_of_mines: 20 }
number_of_mines = ARGV.first.to_i if ARGV.length == 1 OptionParser.new do |opts|
opts.banner = 'Usage: minehunt <options>'
ncurses_main( number_of_mines ) opts.on( '-m', '--mines INT' ) do |nb_mines|
options[:number_of_mines] = nb_mines.to_i
end
end
.parse!
ncurses_main( options[:number_of_mines] )