Class: Metacrunch::Job::Dsl::Options
- Inherits:
-
Object
- Object
- Metacrunch::Job::Dsl::Options
- Defined in:
- lib/metacrunch/job/dsl/options.rb
Defined Under Namespace
Classes: Dsl
Instance Attribute Summary collapse
-
#options ⇒ Object
readonly
Returns the value of attribute options.
Instance Method Summary collapse
-
#initialize(argv = ARGV, require_args: false, &block) ⇒ Options
constructor
A new instance of Options.
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..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? [key] = option end end # Finally parse CLI options with OptionParser parser.parse!(argv) # Make sure required options are present (@options) # Make sure args are present if required ensure_required_args!(argv) if require_args rescue OptionParser::ParseError => e error(e.) end |
Instance Attribute Details
#options ⇒ Object (readonly)
Returns the value of attribute options.
5 6 7 |
# File 'lib/metacrunch/job/dsl/options.rb', line 5 def @options end |