view graster/graster/lib/graster/runner.rb @ 11:f952052e37b7

trying a fix.
author Robert McIntyre <rlm@mit.edu>
date Tue, 24 Aug 2010 19:06:45 -0400
parents
children
line wrap: on
line source
1 require 'optparse'
3 class Graster
4 class Runner
6 attr_reader :options, :args, :opts
8 def initialize(args)
9 @args = args
10 @options = { :default_config_file => true }
11 @opts = OptionParser.new do |opts|
12 opts.banner = "Usage: graster [options] image"
14 opts.on "-c", "--config FILE", "Use specified configuration file.",
15 "The default is ./graster.yml" do |c|
16 @options[:config_file] = c
17 end
19 opts.on "-g", "--generate", "generate a configuration file with","defaults" do
20 @options[:generate_config] = true
21 end
23 opts.on "-d", "--debug", "Dump useless debug info" do
24 @options[:debug] = true
25 end
27 Graster::OPTIONS.each do |key,info|
28 type,sym,*desc = info
30 if type.is_a? Array
31 cast = type[0].name.intern
32 type = Array
33 else
34 cast = type.name.intern
35 end
37 opts.on "--#{key.to_s.gsub /_/, '-'} #{sym}", type, *desc do |x|
38 @options[:config] ||= {}
39 if type == Array
40 x = x.map {|s| Kernel.send(cast,s) }
41 else
42 x = Kernel.send(cast,x)
43 end
45 @options[:config][key] = x
46 end
47 end
48 end
50 @opts.parse!(args)
51 end
53 def start!
54 if @options[:generate_config]
55 print Graster.new(@options).config_to_yaml
56 else
57 unless options[:image_file] = args.shift
58 puts @opts
59 exit 1
60 end
62 Graster.new(options).generate_all_files
63 end
64 end
65 end
66 end