Class: Elm::OptParser

Inherits:
Object
  • Object
show all
Includes:
Contracts::Builtin, Contracts::Core
Defined in:
lib/elm/opt_parser.rb

Overview

Parse command line options

Class Method Summary collapse

Class Method Details

.parse(args) ⇒ Object



18
19
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/elm/opt_parser.rb', line 18

def self.parse(args)
  options = Options.new

  opt_parser = OptionParser.new do |opts|
    opts.banner = 'Usage: ruby-elm [FILES...] ' \
      '[--output FILE] [--yes] [--report FORMAT] [--warn] [--docs FILE]'

    opts.separator ''
    opts.separator 'Available options:'

    opts.on_tail('-h', '--help',
                 'Show this help text') do
      puts opts
      exit
    end

    opts.on('--output [FILE]',
            'Write result to the given .html or .js FILE.') do |output|
      options.output = output
    end

    opts.on('--yes',
            'Reply \'yes\' to all automated prompts.') do
      options.yes = true
    end

    opts.on('--report [FORMAT]', [:normal, :json],
            'Format of error and warning reports',
            '  (normal or json)') do |format|
      options.report = format
    end

    opts.on('--warn',
            'Report warnings to improve code quality.') do
      options.warn = true
    end

    opts.on('--docs [FILE]',
            'Write documentation to FILE as JSON.') do |doc|
      options.docs = doc
    end

    opts.separator ''
    opts.separator 'Examples:'
  end

  begin
    opt_parser.parse! args
  rescue OptionParser::InvalidOption => err
    raise InvalidOptionError, err.message
  end
  options
end