Class: RSpec::Core::Parser

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

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.parseObject



8
9
10
# File 'lib/rspec/core/option_parser.rb', line 8

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

.parse!(args) ⇒ Object



3
4
5
# File 'lib/rspec/core/option_parser.rb', line 3

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

Instance Method Details

#parse!(args) ⇒ Object Also known as: parse



11
12
13
14
15
16
17
18
19
20
# File 'lib/rspec/core/option_parser.rb', line 11

def parse!(args)
  return {} if args.empty?
  if args.include?("--formatter")
    RSpec.deprecate("the --formatter option", "-f or --format")
    args[args.index("--formatter")] = "--format"
  end
  options = {}
  parser(options).parse!(args)
  options
end

#parser(options) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/rspec/core/option_parser.rb', line 24

def parser(options)
  OptionParser.new do |parser|
    parser.banner = "Usage: rspec [options] [files or directories]"

    parser.on('-b', '--backtrace', 'Enable full backtrace') do |o|
      options[:full_backtrace] = true
    end

    parser.on('-c', '--[no-]color', '--[no-]colour', 'Enable color in the output') do |o|
      options[:color_enabled] = o
    end

    parser.on('-d', '--debug', 'Enable debugging') do |o|
      options[:debug] = true
    end

    parser.on('-e', '--example PATTERN', "Run examples whose full descriptions match this pattern",
            "(PATTERN is compiled into a Ruby regular expression)") do |o|
      options[:full_description] = /#{o}/
    end

    parser.on('-f', '--format FORMATTER', 'Choose a formatter',
            '  [p]rogress (default - dots)',
            '  [d]ocumentation (group and example names)',
            '  [h]tml',
            '  [t]extmate',
            '  custom formatter class name') do |o|
      options[:formatter] = o
    end

    parser.on('-o', '--out FILE', 'output to a file instead of STDOUT') do |o|
      options[:output_stream] = File.open(o,'w')
    end

    parser.on_tail('-h', '--help', "You're looking at it.") do
      puts parser
      exit
    end

    parser.on('-I DIRECTORY', 'specify $LOAD_PATH directory (may be used more than once)') do |dir|
      options[:libs] ||= []
      options[:libs] << dir
    end

    parser.on('-l', '--line_number LINE', 'Specify the line number of a single example to run') do |o|
      options[:line_number] = o
    end

    parser.on('-p', '--profile', 'Enable profiling of examples with output of the top 10 slowest examples') do |o|
      options[:profile_examples] = o
    end

    parser.on('-r', '--require PATH', 'Require a file') do |path|
      options[:requires] ||= []
      options[:requires] << path
    end

    parser.on('-v', '--version', 'Show version') do
      puts RSpec::Core::Version::STRING
      exit
    end

    parser.on('-X', '--drb', 'Run examples via DRb') do |o|
      options[:drb] = true
    end

    parser.on('--configure COMMAND', 'Generate configuration files') do |cmd|
      CommandLineConfiguration.new(cmd).run
      exit
    end

    parser.on('--drb-port [PORT]', 'Port to connect to on the DRb server') do |o|
      options[:drb_port] = o.to_i
    end

    parser.on('--tty') do |o|
      options[:tty] = true
    end

    parser.on('--fail-fast', 'Use the fail_fast option to tell RSpec to abort the run on first failure.') do |o|
      options[:fail_fast] = true
    end

    parser.on('-t', '--tag TAG[:VALUE]', 'Run examples with the specified tag',
            'To exclude examples, add ~ before the tag (e.g. ~slow)',
            '(TAG is always converted to a symbol)') do |tag|
      filter_type = tag =~ /^~/ ? :exclusion_filter : :filter

      name,value = tag.gsub(/^(~@|~|@)/, '').split(':')
      name = name.to_sym
      value = true if value.nil?

      options[filter_type] ||= {}
      options[filter_type][name] = value
    end
  end
end