Class: Lrama::OptionParser

Inherits:
Object
  • Object
show all
Defined in:
lib/lrama/option_parser.rb

Overview

Handle option parsing for the command line interface.

Instance Method Summary collapse

Constructor Details

#initializeOptionParser

Returns a new instance of OptionParser.



8
9
10
11
12
# File 'lib/lrama/option_parser.rb', line 8

def initialize
  @options = Options.new
  @trace = []
  @report = []
end

Instance Method Details

#parse(argv) ⇒ Object



14
15
16
17
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
# File 'lib/lrama/option_parser.rb', line 14

def parse(argv)
  parse_by_option_parser(argv)

  @options.trace_opts = validate_trace(@trace)
  @options.report_opts = validate_report(@report)
  @options.grammar_file = argv.shift

  unless @options.grammar_file
    abort "File should be specified\n"
  end

  if @options.grammar_file == '-'
    @options.grammar_file = argv.shift or abort "File name for STDIN should be specified\n"
  else
    @options.y = File.open(@options.grammar_file, 'r')
  end

  if !@report.empty? && @options.report_file.nil? && @options.grammar_file
    @options.report_file = File.dirname(@options.grammar_file) + "/" + File.basename(@options.grammar_file, ".*") + ".output"
  end

  if !@options.header_file && @options.header
    case
    when @options.outfile
      @options.header_file = File.dirname(@options.outfile) + "/" + File.basename(@options.outfile, ".*") + ".h"
    when @options.grammar_file
      @options.header_file = File.dirname(@options.grammar_file) + "/" + File.basename(@options.grammar_file, ".*") + ".h"
    end
  end

  @options
end