Module: Umami::Options

Included in:
Runner
Defined in:
lib/chef-umami/options.rb

Instance Method Summary collapse

Instance Method Details

#parse_optionsObject

Parse command line options. Returns hash of options.



20
21
22
23
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
# File 'lib/chef-umami/options.rb', line 20

def parse_options
  options = {}
  # Default options
  options[:integration_tests] = true
  options[:policyfile] = 'Policyfile.rb'
  options[:test_root] = 'spec'
  options[:unit_tests] = true

  parser = OptionParser.new do |opts|
    opts.banner = opts.banner + "\n\nA taste you won't forget!\n\n"
    opts.on('-h', '--help', 'Prints this help message') {
      puts opts
      exit
    }
    opts.on('-i', '--[no-]integration-tests', 'Write integration tests' \
            " (DEFAULT: #{options[:integration_tests]})") do |integration_tests|
      options[:integration_tests] = integration_tests
    end
    opts.on('-p', '--policyfile POLICYFILE_PATH', 'Specify the path to a policy' \
            " (DEFAULT: #{options[:policyfile]})") do |policyfile|
      options[:policyfile] = policyfile
    end
    opts.on('-r', '--recipes RECIPE1,RECIPE2', Array,
            "Specify one or more recipes for which we'll write tests" \
            ' (DEFAULT: All recipes)') do |recipes|
      options[:recipes] = recipes
    end
    opts.on('-t', '--test-root TEST_ROOT_PATH', "Specify the path into which we'll write tests" \
            " (DEFAULT: #{options[:test_root]})") do |test_root|
      options[:test_root] = test_root
    end
    opts.on('-u', '--[no-]unit-tests', 'Write unit tests' \
            " (DEFAULT: #{options[:unit_tests]})") do |unit_tests|
      options[:unit_tests] = unit_tests
    end
    opts.on('-v', '--version', 'Show version and exit') {
      puts "chef-umami v#{Umami::VERSION}"
      exit
    }
  end
  begin
    parser.parse!
  rescue OptionParser::InvalidOption => e
    puts "Warning: #{e.message}"
    if e.message =~ /--pattern/
      puts 'Ah, this is likely parsed from `rspec` options. We can safely ignore this.'
    end
    puts 'Ignoring the option.'
  end
  options
end