Class: Oryx::Options

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

Constant Summary collapse

DEFAULT_INPUT =
""
DEFAULT_OUTPUT =
Pathname.new("/tmp/oryx-output")

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
# File 'lib/oryx/options.rb', line 9

def initialize(argv)
  @config = {output: DEFAULT_OUTPUT,
             input:  DEFAULT_INPUT}
  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



22
23
24
25
26
27
28
# File 'lib/oryx/options.rb', line 22

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

Instance Method Details

#parse(argv) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/oryx/options.rb', line 30

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

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