Class: Oryx::Options

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

Constant Summary collapse

DEFAULT_INPUT =
""
DEFAULT_OUTPUT =
Pathname.new "a.out"

Instance Method Summary collapse

Constructor Details

#initialize(argv) ⇒ Options

Returns a new instance of Options.



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/oryx/options.rb', line 9

def initialize(argv)
  @config = {output:  DEFAULT_OUTPUT,
             input:   DEFAULT_INPUT,
             verbose: false}

  parse(argv)

  begin
    input = Pathname.new(argv.pop).expand_path
    @config[:input] = input
  rescue TypeError => e
    STDERR.puts "No input_file specified.\nUse --help for more information."
    exit(-1)
  end
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(m, *args, &block) ⇒ Object



25
26
27
28
29
30
31
# File 'lib/oryx/options.rb', line 25

def method_missing m, *args, &block
  if @config.has_key? m
    @config[m]
  else
    super
  end
end

Instance Method Details

#parse(argv) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/oryx/options.rb', line 33

def parse(argv)
  OptionParser.new do |opts|
    opts.banner = "Usage:   oryx [ options ] input_file"
    opts.on("-h", "--help", "Show this message") do
      puts opts
      exit
    end
    opts.on("-o", "--output FILE", String, "Output filename") do |path|
      @config[:output] = Pathname.new(path).expand_path
    end
    opts.on("-v", "--verbose", "Auxilary information about compilation process") do
      @config[:verbose] = true
    end

    begin
      argv = ["-h"] if argv.empty?
      opts.parse!(argv)
    rescue OptionParser::ParseError => e
      STDERR.puts e.message, "\n", opts
      exit(-1)
    end
  end
end