Class: StoryTeller::Options::ArgumentsParser

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

Overview

The ArgumentsParser

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args, defaults = {}, option_parser = OptionParser.new, **params) ⇒ ArgumentsParser

Returns a new instance of ArgumentsParser.



37
38
39
40
41
42
43
# File 'lib/story_teller/options.rb', line 37

def initialize(args, defaults = {}, option_parser = OptionParser.new, **params)
  @args = args
  @parser = option_parser
  @options = defaults
  flags.each { |method_name| self.method(method_name).call }
  @parser.parse!(args, **params)
end

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options.



35
36
37
# File 'lib/story_teller/options.rb', line 35

def options
  @options
end

#parserObject (readonly)

Returns the value of attribute parser.



35
36
37
# File 'lib/story_teller/options.rb', line 35

def parser
  @parser
end

Instance Method Details

#adminObject



81
82
83
84
85
# File 'lib/story_teller/options.rb', line 81

def admin
  @parser.on_tail('--admin', 'Set player character as admin; default: false') do
    @options[:admin] = true
  end
end

rubocop: disable Metrics/MethodLength



52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/story_teller/options.rb', line 52

def banner
  @parser.banner = "Usage: #{File.basename($PROGRAM_NAME)} [game_path] [options]"
  @parser.separator ''
  @parser.separator 'Arguments:'
  @parser.separator '    game_path                        Path to game file or directory'
  @parser.separator(
    format(
      '                                     Default: %<game_path>s',
      game_path: @options[:game_path]
    )
  )
  @parser.separator ''
  @parser.separator 'Options:'
end

#builderObject



87
88
89
90
91
# File 'lib/story_teller/options.rb', line 87

def builder
  @parser.on_tail('--builder', 'Set player character as builder; default: false') do
    @options[:builder] = true
  end
end

#flagsObject



45
46
47
48
49
# File 'lib/story_teller/options.rb', line 45

def flags
  @flags ||= %i[
    banner game_path admin builder persist word_wrap log_level
    help version]
end

#game_pathObject

rubocop: enable Metrics/MethodLength



68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/story_teller/options.rb', line 68

def game_path
  first = @args.first
  return if first.nil?

  path = File.expand_path(first)

  # Only treat it as game_path if it exists
  return unless File.exist?(path)

  @options[:game_path] = path
  @args.shift
end

#helpObject



114
115
116
117
118
119
# File 'lib/story_teller/options.rb', line 114

def help
  @parser.on_tail('-?', '--help', 'Show this message') do
    puts @parser
    exit
  end
end

#log_levelObject



107
108
109
110
111
112
# File 'lib/story_teller/options.rb', line 107

def log_level
  @parser.on_tail('-v', '--verbose', 'Increase verbosity') do
    @options[:log_level] ||= Logger::INFO
    @options[:log_level] -= 1
  end
end

#persistObject



93
94
95
96
97
98
99
# File 'lib/story_teller/options.rb', line 93

def persist
  desc = 'Persist the world tree across sessions; default: false'
  @parser.on_tail('--persist', desc) do
    @options[:persist] = true
    @options[:reset_db_each_session] = false
  end
end

#versionObject



121
122
123
124
125
126
# File 'lib/story_teller/options.rb', line 121

def version
  @parser.on_tail('--version', 'Show version') do
    puts "#{$PROGRAM_NAME} version #{StoryTellerCli::VERSION}"
    exit
  end
end

#word_wrapObject



101
102
103
104
105
# File 'lib/story_teller/options.rb', line 101

def word_wrap
  @parser.on('--word-wrap COLUMNS', Integer, 'Set default line width') do |columns|
    @options[:word_wrap] = columns
  end
end