Class: Micron::App::Options

Inherits:
Object
  • Object
show all
Defined in:
lib/micron/app/options.rb

Constant Summary collapse

DEFAULTS =
{
  :coverage => true,
  :tests    => [],
  :methods  => [],
}

Class Method Summary collapse

Class Method Details

.parse(options = nil) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/micron/app/options.rb', line 14

def self.parse(options=nil)

  # always try to use default options first
  if options then
    options = DEFAULTS.merge(options)
  else
    options = DEFAULTS.dup
  end

  # then rc file
  rc = File.join(Dir.pwd, ".micronrc")
  if File.exists? rc then
    parse_opts(options, File.read(rc).split(" "))
  end

  # then anything on command line
  parse_opts(options, ARGV)

  return options
end

.parse_opts(options, argv) ⇒ Object



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
# File 'lib/micron/app/options.rb', line 35

def self.parse_opts(options, argv)
  parser = OptionParser.new do |opts|
    opts.banner = "usage: #{$0} [options]"

    opts.on("-t", "--test PATTERN", "Only run test files matching pattern") do |p|
      options[:tests] << p
    end

    opts.on("-m", "--method PATTERN", "Only run test methods matching pattern") do |p|
      p.strip!
      options[:methods] << p if not p.empty?
    end

    opts.on("--nocov", "Disable coverage reporting") {
      options[:coverage] = false
    }

    opts.on("--proc", "Use the process runner") {
      options[:proc] = true
    }

    opts.on("--fork", "Use the forking runner") {
      options[:fork] = true
    }

    opts.on("--runclass", "Run class in child process") {
      options[:runclass] = true
    }

    opts.on("--runmethod", "Run method in child process") {
      options[:runmethod] = true
    }

    opts.on("-h", "--help", "Show this message") do
      puts opts
      exit
    end
  end

  begin
    parser.parse!(argv)

  rescue Exception => ex
    exit if ex.kind_of? SystemExit
    STDERR.puts "error: #{ex}"
    STDERR.puts
    STDERR.puts parser
    exit 1
  end

  return options
end