6 class OptionParser < ::OptionParser
8 def parse(args, err, out)
18 :diff => ["-D", "--diff [FORMAT]", "Show diff of objects that are expected to be equal when they are not",
19 "Builtin formats: unified|u|context|c",
20 "You can also specify a custom differ class",
21 "(in which case you should also specify --require)"],
22 :colour => ["-c", "--colour", "--color", "Show coloured (red/green) output"],
23 :example => ["-e", "--example [NAME|FILE_NAME]", "Execute example(s) with matching name(s). If the argument is",
24 "the path to an existing file (typically generated by a previous",
25 "run using --format failing_examples:file.txt), then the examples",
26 "on each line of thatfile will be executed. If the file is empty,",
27 "all examples will be run (as if --example was not specified).",
29 "If the argument is not an existing file, then it is treated as",
30 "an example name directly, causing RSpec to run just the example",
31 "matching that name"],
32 :specification => ["-s", "--specification [NAME]", "DEPRECATED - use -e instead", "(This will be removed when autotest works with -e)"],
33 :line => ["-l", "--line LINE_NUMBER", Integer, "Execute behaviout or specification at given line.",
34 "(does not work for dynamically generated specs)"],
35 :format => ["-f", "--format FORMAT[:WHERE]", "Specifies what format to use for output. Specify WHERE to tell",
36 "the formatter where to write the output. All built-in formats",
37 "expect WHERE to be a file name, and will write to STDOUT if it's",
38 "not specified. The --format option may be specified several times",
39 "if you want several outputs",
41 "Builtin formats for examples: ",
42 "progress|p : Text progress",
43 "profile|o : Text progress with profiling of 10 slowest examples",
44 "specdoc|s : Example doc as text",
45 "html|h : A nice HTML report",
46 "failing_examples|e : Write all failing examples - input for --example",
47 "failing_example_groups|g : Write all failing example groups - input for --example",
49 "Builtin formats for stories: ",
50 "plain|p : Plain Text",
51 "html|h : A nice HTML report",
53 "FORMAT can also be the name of a custom formatter class",
54 "(in which case you should also specify --require to load it)"],
55 :require => ["-r", "--require FILE", "Require FILE before running specs",
56 "Useful for loading custom formatters or other extensions.",
57 "If this option is used it must come before the others"],
58 :backtrace => ["-b", "--backtrace", "Output full backtrace"],
59 :loadby => ["-L", "--loadby STRATEGY", "Specify the strategy by which spec files should be loaded.",
60 "STRATEGY can currently only be 'mtime' (File modification time)",
61 "By default, spec files are loaded in alphabetical order if --loadby",
63 :reverse => ["-R", "--reverse", "Run examples in reverse order"],
64 :timeout => ["-t", "--timeout FLOAT", "Interrupt and fail each example that doesn't complete in the",
66 :heckle => ["-H", "--heckle CODE", "If all examples pass, this will mutate the classes and methods",
67 "identified by CODE little by little and run all the examples again",
68 "for each mutation. The intent is that for each mutation, at least",
69 "one example *should* fail, and RSpec will tell you if this is not the",
70 "case. CODE should be either Some::Module, Some::Class or",
71 "Some::Fabulous#method}"],
72 :dry_run => ["-d", "--dry-run", "Invokes formatters without executing the examples."],
73 :options_file => ["-O", "--options PATH", "Read options from a file"],
74 :generate_options => ["-G", "--generate-options PATH", "Generate an options file for --options"],
75 :runner => ["-U", "--runner RUNNER", "Use a custom Runner."],
76 :drb => ["-X", "--drb", "Run examples via DRb. (For example against script/spec_server)"],
77 :version => ["-v", "--version", "Show version"],
78 :help => ["-h", "--help", "You're looking at it"]
81 def initialize(err, out)
85 @options = Options.new(@error_stream, @out_stream)
87 @spec_parser = SpecParser.new
90 self.banner = "Usage: spec (FILE|DIRECTORY|GLOB)+ [options]"
92 on(*OPTIONS[:diff]) {|diff| @options.parse_diff(diff)}
93 on(*OPTIONS[:colour]) {@options.colour = true}
94 on(*OPTIONS[:example]) {|example| @options.parse_example(example)}
95 on(*OPTIONS[:specification]) {|example| @options.parse_example(example)}
96 on(*OPTIONS[:line]) {|line_number| @options.line_number = line_number.to_i}
97 on(*OPTIONS[:format]) {|format| @options.parse_format(format)}
98 on(*OPTIONS[:require]) {|requires| invoke_requires(requires)}
99 on(*OPTIONS[:backtrace]) {@options.backtrace_tweaker = NoisyBacktraceTweaker.new}
100 on(*OPTIONS[:loadby]) {|loadby| @options.loadby = loadby}
101 on(*OPTIONS[:reverse]) {@options.reverse = true}
102 on(*OPTIONS[:timeout]) {|timeout| @options.timeout = timeout.to_f}
103 on(*OPTIONS[:heckle]) {|heckle| @options.load_heckle_runner(heckle)}
104 on(*OPTIONS[:dry_run]) {@options.dry_run = true}
105 on(*OPTIONS[:options_file]) {|options_file| parse_options_file(options_file)}
106 on(*OPTIONS[:generate_options]) do |options_file|
108 on(*OPTIONS[:runner]) do |runner|
109 @options.user_input_for_runner = runner
111 on(*OPTIONS[:drb]) {}
112 on(*OPTIONS[:version]) {parse_version}
113 on_tail(*OPTIONS[:help]) {parse_help}
116 def order!(argv, &blk)
118 @options.argv = @argv.dup
119 return if parse_generate_options
122 super(@argv) do |file|
123 @options.files << file
124 blk.call(file) if blk
127 if @options.line_number
128 set_spec_from_line_number
135 def invoke_requires(requires)
136 requires.split(",").each do |file|
141 def parse_options_file(options_file)
142 option_file_args = IO.readlines(options_file).map {|l| l.chomp.split " "}.flatten
143 @argv.push(*option_file_args)
146 def parse_generate_options
147 # Remove the --generate-options option and the argument before writing to file
149 ['-G', '--generate-options'].each do |option|
150 if index = @argv.index(option)
151 @argv.delete_at(index)
152 options_file = @argv.delete_at(index)
157 write_generated_options(options_file)
164 def write_generated_options(options_file)
165 File.open(options_file, 'w') do |io|
166 io.puts @argv.join("\n")
168 @out_stream.puts "\nOptions written to #{options_file}. You can now use these options with:"
169 @out_stream.puts "spec --options #{options_file}"
170 @options.examples_should_not_be_run
176 is_drb ||= argv.delete(OPTIONS[:drb][0])
177 is_drb ||= argv.delete(OPTIONS[:drb][1])
178 return nil unless is_drb
179 @options.examples_should_not_be_run
181 self.class.parse(argv, @error_stream, @out_stream)
187 @out_stream.puts ::Spec::VERSION::DESCRIPTION
192 @out_stream.puts self
196 def set_spec_from_line_number
197 if @options.examples.empty?
198 if @options.files.length == 1
199 if @file_factory.file?(@options.files[0])
200 source = @file_factory.open(@options.files[0])
201 example = @spec_parser.spec_name_for(source, @options.line_number)
202 @options.parse_example(example)
203 elsif @file_factory.directory?(@options.files[0])
204 @error_stream.puts "You must specify one file, not a directory when using the --line option"
207 @error_stream.puts "#{@options.files[0]} does not exist"
211 @error_stream.puts "Only one file can be specified when using the --line option: #{@options.files.inspect}"
215 @error_stream.puts "You cannot use both --line and --example"
221 @out_stream == $stdout
225 @error_stream == $stderr