diff 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 diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/graster/graster/lib/graster/runner.rb	Tue Aug 24 19:06:45 2010 -0400
     1.3 @@ -0,0 +1,66 @@
     1.4 +require 'optparse'
     1.5 +
     1.6 +class Graster
     1.7 +  class Runner
     1.8 +
     1.9 +    attr_reader :options, :args, :opts
    1.10 +    
    1.11 +    def initialize(args)
    1.12 +      @args = args
    1.13 +      @options = { :default_config_file => true }
    1.14 +      @opts = OptionParser.new do |opts|
    1.15 +        opts.banner = "Usage: graster [options] image"
    1.16 +
    1.17 +        opts.on "-c", "--config FILE", "Use specified configuration file.",
    1.18 +                                       "The default is ./graster.yml" do |c|
    1.19 +          @options[:config_file] = c
    1.20 +        end
    1.21 +
    1.22 +        opts.on "-g", "--generate", "generate a configuration file with","defaults" do
    1.23 +          @options[:generate_config] = true
    1.24 +        end
    1.25 +
    1.26 +        opts.on "-d", "--debug", "Dump useless debug info" do
    1.27 +          @options[:debug] = true
    1.28 +        end
    1.29 +
    1.30 +        Graster::OPTIONS.each do |key,info|
    1.31 +          type,sym,*desc = info
    1.32 +
    1.33 +          if type.is_a? Array
    1.34 +            cast = type[0].name.intern
    1.35 +            type = Array
    1.36 +          else
    1.37 +            cast = type.name.intern
    1.38 +          end
    1.39 +
    1.40 +          opts.on "--#{key.to_s.gsub /_/, '-'} #{sym}", type, *desc do |x|
    1.41 +            @options[:config] ||= {}
    1.42 +            if type == Array
    1.43 +              x = x.map {|s| Kernel.send(cast,s) }
    1.44 +            else
    1.45 +              x = Kernel.send(cast,x)
    1.46 +            end
    1.47 +
    1.48 +            @options[:config][key] = x
    1.49 +          end
    1.50 +        end
    1.51 +      end
    1.52 +
    1.53 +      @opts.parse!(args)
    1.54 +    end
    1.55 +    
    1.56 +    def start!
    1.57 +      if @options[:generate_config]
    1.58 +        print Graster.new(@options).config_to_yaml
    1.59 +      else
    1.60 +        unless options[:image_file] = args.shift
    1.61 +          puts @opts
    1.62 +          exit 1
    1.63 +        end
    1.64 +
    1.65 +        Graster.new(options).generate_all_files
    1.66 +      end
    1.67 +    end
    1.68 +  end
    1.69 +end
    1.70 \ No newline at end of file