Class: Hyrum::ScriptOptions

Inherits:
Object
  • Object
show all
Defined in:
lib/hyrum/script_options.rb

Constant Summary collapse

MANDATORY_OPTIONS =
%i[message].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args) ⇒ ScriptOptions

Returns a new instance of ScriptOptions.



13
14
15
16
# File 'lib/hyrum/script_options.rb', line 13

def initialize(args)
  @options = {}
  @args = args
end

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options.



11
12
13
# File 'lib/hyrum/script_options.rb', line 11

def options
  @options
end

Instance Method Details

#parseObject



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/hyrum/script_options.rb', line 18

def parse
  OptionParser.new do |parser|
    define_options(parser)
    parser.parse!(@args)
  end
  enforce_mandatory_options
  set_dynamic_defaults
  options
rescue OptionParser::InvalidOption => e
  raise ScriptOptionsError.new("Invalid option: #{e.message}")
rescue OptionParser::MissingArgument => e
  raise ScriptOptionsError.new("Missing argument for option: #{e.message}")
rescue OptionParser::InvalidArgument => e
  raise ScriptOptionsError.new("Invalid argument for option: #{e.message}")
end