Class | Spec::Runner::OptionParser
In: | lib/spec/runner/option_parser.rb
Parent: | ::OptionParser

### Methods

invoke_requires new order! parse parse_drb parse_generate_options parse_help parse_options_file parse_version stdout? write_generated_options

### Constants

OPTIONS

=

{ :pattern => ["-p", "--pattern [PATTERN]","Limit files loaded to those matching this pattern. Defaults to '**/*_spec.rb'", "Separate multiple patterns with commas.", "Applies only to directories named on the command line (files", "named explicitly on the command line will be loaded regardless)."], :diff => ["-D", "--diff [FORMAT]","Show diff of objects that are expected to be equal when they are not", "Builtin formats: unified|u|context|c", "You can also specify a custom differ class", "(in which case you should also specify --require)"], :colour => ["-c", "--colour", "--color", "Show coloured (red/green) output"], :example => ["-e", "--example [NAME|FILE_NAME]", "Execute example(s) with matching name(s). If the argument is", "the path to an existing file (typically generated by a previous", "run using --format failing_examples:file.txt), then the examples", "on each line of thatfile will be executed. If the file is empty,", "all examples will be run (as if --example was not specified).", " ", "If the argument is not an existing file, then it is treated as", "an example name directly, causing RSpec to run just the example", "matching that name"], :specification => ["-s", "--specification [NAME]", "DEPRECATED - use -e instead", "(This will be removed when autotest works with -e)"], :line => ["-l", "--line LINE_NUMBER", Integer, "Execute behaviout or specification at given line.", "(does not work for dynamically generated specs)"], :format => ["-f", "--format FORMAT[:WHERE]","Specifies what format to use for output. Specify WHERE to tell", "the formatter where to write the output. All built-in formats", "expect WHERE to be a file name, and will write to STDOUT if it's", "not specified. The --format option may be specified several times", "if you want several outputs", " ", "Builtin formats for examples: ", "progress|p : Text progress", "profile|o : Text progress with profiling of 10 slowest examples", "specdoc|s : Example doc as text", "indented|i : Example doc as indented text", "html|h : A nice HTML report", "failing_examples|e : Write all failing examples - input for --example", "failing_example_groups|g : Write all failing example groups - input for --example", " ", "Builtin formats for stories: ", "plain|p : Plain Text", "html|h : A nice HTML report", " ", "FORMAT can also be the name of a custom formatter class", "(in which case you should also specify --require to load it)"], :require => ["-r", "--require FILE", "Require FILE before running specs", "Useful for loading custom formatters or other extensions.", "If this option is used it must come before the others"], :backtrace => ["-b", "--backtrace", "Output full backtrace"], :loadby => ["-L", "--loadby STRATEGY", "Specify the strategy by which spec files should be loaded.", "STRATEGY can currently only be 'mtime' (File modification time)", "By default, spec files are loaded in alphabetical order if --loadby", "is not specified."], :reverse => ["-R", "--reverse", "Run examples in reverse order"], :timeout => ["-t", "--timeout FLOAT", "Interrupt and fail each example that doesn't complete in the", "specified time"], :heckle => ["-H", "--heckle CODE", "If all examples pass, this will mutate the classes and methods", "identified by CODE little by little and run all the examples again", "for each mutation. The intent is that for each mutation, at least", "one example *should* fail, and RSpec will tell you if this is not the", "case. CODE should be either Some::Module, Some::Class or", "Some::Fabulous#method}"], :dry_run => ["-d", "--dry-run", "Invokes formatters without executing the examples."], :options_file => ["-O", "--options PATH", "Read options from a file"], :generate_options => ["-G", "--generate-options PATH", "Generate an options file for --options"], :runner => ["-U", "--runner RUNNER", "Use a custom Runner."], :drb => ["-X", "--drb", "Run examples via DRb. (For example against script/spec_server)"], :version => ["-v", "--version", "Show version"], :help => ["-h", "--help", "You're looking at it"]

### Attributes

options | [R] |

### Public Class methods

new(err, out)

[Source]

         # File lib/spec/runner/option_parser.rb, line 86
     86:       def initialize(err, out)
     87:         super()
     88:         @error_stream = err
     89:         @out_stream = out
     90:         @options = Options.new(@error_stream, @out_stream)
     91:
     92:         @file_factory = File
     93:
     94:         self.banner = "Usage: spec (FILE|DIRECTORY|GLOB)+ [options]"
     95:         self.separator ""
     96:         on(*OPTIONS[:pattern]) {|pattern| @options.filename_pattern = pattern}
     97:         on(*OPTIONS[:diff]) {|diff| @options.parse_diff(diff)}
     98:         on(*OPTIONS[:colour]) {@options.colour = true}
     99:         on(*OPTIONS[:example]) {|example| @options.parse_example(example)}
    100:         on(*OPTIONS[:specification]) {|example| @options.parse_example(example)}
    101:         on(*OPTIONS[:line]) {|line_number| @options.line_number = line_number.to_i}
    102:         on(*OPTIONS[:format]) {|format| @options.parse_format(format)}
    103:         on(*OPTIONS[:require]) {|requires| invoke_requires(requires)}
    104:         on(*OPTIONS[:backtrace]) {@options.backtrace_tweaker = NoisyBacktraceTweaker.new}
    105:         on(*OPTIONS[:loadby]) {|loadby| @options.loadby = loadby}
    106:         on(*OPTIONS[:reverse]) {@options.reverse = true}
    107:         on(*OPTIONS[:timeout]) {|timeout| @options.timeout = timeout.to_f}
    108:         on(*OPTIONS[:heckle]) {|heckle| @options.load_heckle_runner(heckle)}
    109:         on(*OPTIONS[:dry_run]) {@options.dry_run = true}
    110:         on(*OPTIONS[:options_file]) {|options_file| parse_options_file(options_file)}
    111:         on(*OPTIONS[:generate_options]) {|options_file|}
    112:         on(*OPTIONS[:runner]) {|runner|  @options.user_input_for_runner = runner}
    113:         on(*OPTIONS[:drb]) {}
    114:         on(*OPTIONS[:version]) {parse_version}
    115:         on_tail(*OPTIONS[:help]) {parse_help}
    116:       end

parse(args, err, out)

[Source]

        # File lib/spec/runner/option_parser.rb, line 8
     8:         def parse(args, err, out)
     9:           parser = new(err, out)
    10:           parser.parse(args)
    11:           parser.options
    12:         end

### Public Instance methods

order!(argv, &blk)

[Source]

         # File lib/spec/runner/option_parser.rb, line 118
    118:       def order!(argv, &blk)
    119:         @argv = argv
    120:         @options.argv = @argv.dup
    121:         return if parse_generate_options
    122:         return if parse_drb
    123:
    124:         super(@argv) do |file|
    125:           @options.files << file
    126:           blk.call(file) if blk
    127:         end
    128:
    129:         @options
    130:       end

### Protected Instance methods

invoke_requires(requires)

[Source]

         # File lib/spec/runner/option_parser.rb, line 133
    133:       def invoke_requires(requires)
    134:         requires.split(",").each do |file|
    135:           require file
    136:         end
    137:       end

parse_drb(argv = nil)

[Source]

         # File lib/spec/runner/option_parser.rb, line 175
    175:       def parse_drb(argv = nil)
    176:         argv ||= @options.argv # TODO - see note about about http://rspec.lighthouseapp.com/projects/5645/tickets/293
    177:         is_drb = false
    178:         is_drb ||= argv.delete(OPTIONS[:drb][0])
    179:         is_drb ||= argv.delete(OPTIONS[:drb][1])
    180:         return false unless is_drb
    181:         @options.examples_should_not_be_run
    182:         DrbCommandLine.run(
    183:           self.class.parse(argv, @error_stream, @out_stream)
    184:         )
    185:         true
    186:       end

parse_generate_options()

[Source]

         # File lib/spec/runner/option_parser.rb, line 148
    148:       def parse_generate_options
    149:         # Remove the --generate-options option and the argument before writing to file
    150:         options_file = nil
    151:         ['-G', '--generate-options'].each do |option|
    152:           if index = @argv.index(option)
    153:             @argv.delete_at(index)
    154:             options_file = @argv.delete_at(index)
    155:           end
    156:         end
    157:
    158:         if options_file
    159:           write_generated_options(options_file)
    160:           return true
    161:         else
    162:           return false
    163:         end
    164:       end

parse_help()

[Source]

         # File lib/spec/runner/option_parser.rb, line 193
    193:       def parse_help
    194:         @out_stream.puts self
    195:         exit if stdout?
    196:       end

parse_options_file(options_file)

[Source]

         # File lib/spec/runner/option_parser.rb, line 139
    139:       def parse_options_file(options_file)
    140:         option_file_args = IO.readlines(options_file).map {|l| l.chomp.split " "}.flatten
    141:         @argv.push(*option_file_args)
    142:         # TODO - this is a brute force solution to http://rspec.lighthouseapp.com/projects/5645/tickets/293.
    143:         # Let's look for a cleaner way. Might not be one. But let's look. If not, perhaps
    144:         # this can be moved to a different method to indicate the special handling for drb?
    145:         parse_drb(@argv)
    146:       end

parse_version()

[Source]

         # File lib/spec/runner/option_parser.rb, line 188
    188:       def parse_version
    189:         @out_stream.puts ::Spec::VERSION::DESCRIPTION
    190:         exit if stdout?
    191:       end

stdout?()

[Source]

         # File lib/spec/runner/option_parser.rb, line 198
    198:       def stdout?
    199:         @out_stream == $stdout
    200:       end

write_generated_options(options_file)

[Source]

         # File lib/spec/runner/option_parser.rb, line 166
    166:       def write_generated_options(options_file)
    167:         File.open(options_file, 'w') do |io|
    168:           io.puts @argv.join("\n")
    169:         end
    170:         @out_stream.puts "\nOptions written to #{options_file}. You can now use these options with:"
    171:         @out_stream.puts "spec --options #{options_file}"
    172:         @options.examples_should_not_be_run
    173:       end

[Validate]