Class: PriorityTest::Core::OptionParser

Inherits:
Object
  • Object
show all
Defined in:
lib/priority_test/core/option_parser.rb

Constant Summary collapse

AVAILABLE_TEST_FRAMEWORKS =
['rspec']
OPTIONS =
['--priority']

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.parse!(args) ⇒ Object



9
10
11
# File 'lib/priority_test/core/option_parser.rb', line 9

def self.parse!(args)
  new.parse!(args)
end

Instance Method Details

#parse!(args) ⇒ Object



32
33
34
35
36
37
# File 'lib/priority_test/core/option_parser.rb', line 32

def parse!(args)
  options = parse_options(args)
  options[:test_framework] = parse_test_framework(args)

  options
end

#parse_options(args) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/priority_test/core/option_parser.rb', line 13

def parse_options(args)
  options = {}
  opts_parser = parser(options)

  if args.empty?
    puts opts_parser
    return options
  end

  begin
    opts_parser.parse!(args.clone)
  rescue ::OptionParser::InvalidOption
  end

  remove_parsed_options(args)

  options
end

#parse_test_framework(args) ⇒ Object



66
67
68
69
70
71
72
73
74
75
# File 'lib/priority_test/core/option_parser.rb', line 66

def parse_test_framework(args)
  test_framework = args.shift
  if AVAILABLE_TEST_FRAMEWORKS.include?(test_framework)
    test_framework
  else
    puts "Invalid test framework: #{test_framework}"
    puts "Run `pt -h` for more info"
    raise ::OptionParser::InvalidArgument
  end
end

#parser(options) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/priority_test/core/option_parser.rb', line 39

def parser(options)
  ::OptionParser.new do |parser|
    parser.banner = <<-EOS
Usage: pt <test-framework> [options] [files or directories]

Test framework:
  * rspec

Options:
    EOS

    parser.on('--priority', 'Filter and run priority tests') do |o|
      options[:priority] = true
    end

    parser.on_tail("-h", "--help", "Show help") do
      puts parser
      exit
    end

    parser.on_tail('-v', '--version', 'Show version') do
      puts PriorityTest::VERSION
      exit
    end
  end
end

#remove_parsed_options(args) ⇒ Object



77
78
79
# File 'lib/priority_test/core/option_parser.rb', line 77

def remove_parsed_options(args)
  args.reject! { |arg| OPTIONS.include?(arg) }
end