Class: One44::CLI::Optparse::Parser

Inherits:
Object
  • Object
show all
Defined in:
lib/one44-cli/optparse.rb

Class Method Summary collapse

Class Method Details

.parse(args) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/one44-cli/optparse.rb', line 9

def self.parse(args)
  options = {}
  option_parser = OptionParser.new do |opts|
    opts.on('-r', '--random-sort-questions') do
      options[:random_sort_questions] = true
    end

    opts.on('--not-random-sort-questions') do
      options[:random_sort_questions] = false
    end

    opts.on('-s ORDER', '--sort-questions=ORDER', /^random$/i) do |order|
      options[:question_order] = order.downcase.to_sym
    end

    opts.on('-f TEST', '--path-to-test-file=TEST', '[Mandatory] Specify the path to the test file.') do |test|
      options[:test] = test
    end
  end
  process_options(option_parser, options, args)
end

.process_options(option_parser, options, args) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
# File 'lib/one44-cli/optparse.rb', line 31

def self.process_options(option_parser, options, args)
  option_parser.parse!(args)
  unless options[:test]
    puts option_parser.help
    exit 1
  end
  options
rescue Exception => e
  puts "Exception encountered: #{e}"
  exit 1
end