#!/usr/bin/env ruby require 'optparse' require 'ostruct' require "rdoc" require "rdoc/rdoc" require File.dirname(__FILE__) + "/../lib/experiment/runner" class App VERSION = '0.3.2' attr_reader :options def initialize(arguments, stdin) @arguments = arguments @stdin = stdin # Set defaults @options = OpenStruct.new @options.verbose = false @options.quiet = false @options.env = :development @options.cv = nil#5 @options.description = "" @options.opts = "" @options.distributed = false @options.master = "localhost" @options.summary = false end # Parse options, check arguments, then process the command def run if parsed_options? && arguments_valid? puts "Start at #{DateTime.now}\n\n" if @options.verbose output_options if @options.verbose # [Optional] process_command puts "\nFinished at #{DateTime.now}" if @options.verbose else output_usage end end protected def parsed_options? # Specify options @opts = OptionParser.new @opts.on('-v', '--version') { output_version ; exit 0 } @opts.on('-h', '--help') { output_help } @opts.on('-V', '--verbose') { @options.verbose = true; @options.summary = true } @opts.on('-q', '--quiet') { @options.quiet = true } @opts.on('-e', '--env [ENV]', [:development, :compute], "Sets the environment to run in.") { |v| @options.env = v } if ARGV.first == 'generate' || ARGV.first == "-h" @opts.separator "" @opts.separator "Options for `generate`:" @opts.on('-m', '--description M', String, "Description or hypothesis for the condition being generated.") { |v| @options.description = v } end if ARGV.first == 'run' || ARGV.first == "-h" || ARGV.first == 'console' @opts.separator "" @opts.separator "Options for `run`:" @opts.on('-c', '--cv CV', Integer, "The number of cross validations to run.") { |v| @options.cv = v } @opts.on('-o', '--options OPTSTRING', String, "Options to override or define configuration with.", "format as: key1:val1,key2:val2") do |v| @options.opts = v end @opts.on('--summary', "After a run of the experiment print out the summary to STDOUT.") { @options.summary = true } @opts.on('-D', '--distributed', "Run with a distributed computing mode.", "This will be the master server/work cue.") { @options.distributed = true } @opts.separator " Overrideable options (defined in config/config.yaml)" @opts.separator " No options defined." unless Experiment::Config::parsing_for_options(@opts, @options) end if ARGV.first == 'worker' || ARGV.first == "-h" @opts.separator "" @opts.separator "Options for `worker`:" @opts.on('-a', '--address MODE', String, "Address to the master machine.") { |v| @options.master = v } end @opts.parse!(@arguments) #rescue return false process_options true end # Performs post-parse processing on options def process_options @options.verbose = false if @options.quiet end def output_options puts "Options:\n" @options.marshal_dump.each do |name, val| puts " #{name} = #{val}" end end # True if required arguments were provided def arguments_valid? true #if @arguments.length > 0 end def output_help output_version puts "= Synopsis This program will run an experimental batch or generate files for a new experiment" puts output_usage puts puts "= Options" puts @opts.help puts puts "= Commands" # Bizzare hax to make RDoc parse the files top_level = RDoc::TopLevel.new File.dirname(__FILE__) + "/../lib/experiment/runner.rb" opts = RDoc::Options.new stats = RDoc::Stats.new 1 parser = RDoc::Parser.for top_level, File.dirname(__FILE__) + "/../lib/experiment/runner.rb", File.read(File.dirname(__FILE__) + "/../lib/experiment/runner.rb"), opts, stats d = parser.scan d.modules.first.classes.first.method_list.each do |m| unless m.comment == "" || m.name == 'initialize' || m.name == 'new' puts "== #{m.name == 'new_project' ? 'new' : m.name}" puts m.comment puts end end puts "----" exit 0 end def output_usage puts "= Usage experiment command [options] For help use: experiment -h" end def output_version puts "#{File.basename(__FILE__)} version #{VERSION}" end def process_command command = @arguments.shift runner = Experiment::Runner.new @arguments, options command = "new_project" if command == 'new' #begin runner.send command.to_sym #rescue NoMethodError => e # puts "Wrong input #{e.inspect}" # output_usage #end end def process_standard_input input = @stdin.read end end # Create and run the application app = App.new(ARGV, STDIN) app.run