#!/usr/bin/env ruby require 'optparse' require_relative '../lib/gemwarrior/game' require_relative '../lib/gemwarrior/game_options' require_relative '../lib/gemwarrior/version' include Gemwarrior GAME_NAME = 'Gem Warrior' def parse_options_cli options = { beast_mode: false, debug_mode: false, god_mode: false, new_game: false, sound_enabled: false, sound_volume: 0.3, use_wordnik: false, extra_command: nil } options_file = read_options_file unless options_file.nil? sound_enabled_option = options_file[0][1].eql?('false') ? false : true sound_volume_option = options_file[1][1].to_f use_wordnik_option = options_file[2][1].eql?('false') ? false : true options[:sound_enabled] = sound_enabled_option options[:sound_volume] = sound_volume_option options[:use_wordnik] = use_wordnik_option end optparse = OptionParser.new do |opts| opts.on('-b', '--beast', 'Enable debug[beastmode]') do options[:beast_mode] = true end opts.on('-d', '--debug', 'Enable debug commands in-game') do options[:debug_mode] = true end opts.on('-g', '--god', 'Enable debug[godmode]') do options[:god_mode] = true end opts.on('-n', '--new', 'Immediately start a new game, skipping main menu') do options[:new_game] = true end opts.on('-s', '--sound', 'Enable sound (experimental)') do options[:sound_enabled] = true end opts.on('-v', '--version', 'Display version number and exit') do puts "#{GAME_NAME} v#{Gemwarrior::VERSION}" exit end opts.on('-w', '--wordnik', 'Enable Wordnik to generate more diverse, dynamic descriptors of entities') do options[:use_wordnik] = true end opts.on('-x', '--extra COMMAND', 'Run a command immediately upon beginning the game') do |xc| options[:extra_command] = xc.to_s end end optparse.parse!() return options end def print_error(error) case error when OptionParser::InvalidOption puts "#{GAME_NAME}: illegal option #{error.args.join(' ')}" else puts "An unexpected error occurred while running #{GAME_NAME}:" puts " #{error}\n" end end def read_options_file if File.exist?(GameOptions.data['options_file_path']) options = [] File.open(GameOptions.data['options_file_path']).readlines.each do |line| options << line.chomp.split(':') end return options end nil end begin GameOptions.add 'log_file_path', "#{Dir.home}/.gemwarrior_log" GameOptions.add 'options_file_path', "#{Dir.home}/.gemwarrior_options" options = parse_options_cli Gemwarrior::Game.new(options) rescue => error print_error(error) exit(false) end