Class: Metacrunch::Job::Dsl::Options

Inherits:
Object
  • Object
show all
Defined in:
lib/metacrunch/job/dsl/options.rb

Defined Under Namespace

Classes: Dsl

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(argv = ARGV, require_args: false, &block) ⇒ Options

Returns a new instance of Options.



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/metacrunch/job/dsl/options.rb', line 7

def initialize(argv = ARGV, require_args: false, &block)
  @options = {}
  dsl.instance_eval(&block)

  dsl.options.each do |key, opt_def|
    # Set default value
    @options[key] = opt_def[:default]

    # Register with OptionParser
    if opt_def[:args].present?
      option = parser.define(*opt_def[:args]) { |value| @options[key] = value }

      option.desc << "REQUIRED" if opt_def[:required]
      option.desc << "DEFAULT: #{opt_def[:default]}" if opt_def[:default].present?

      parser_options[key] = option
    end
  end

  # Finally parse CLI options with OptionParser
  parser.parse!(argv)

  # Make sure required options are present
  ensure_required_options!(@options)

  # Make sure args are present if required
  ensure_required_args!(argv) if require_args
rescue OptionParser::ParseError => e
  error(e.message)
end

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options.



5
6
7
# File 'lib/metacrunch/job/dsl/options.rb', line 5

def options
  @options
end