#!/usr/bin/env ruby require 'rbbt' require 'rbbt/util/simpleopt' options = SOPT.get("--log* Log level from 0 (debug) 6 (errors):-cd--command_dir* Directory from where to load commands:--profile Profile execution:--no_color Disable colored output:--locate_file Locate file been executed") ENV["RBBT_NOCOLOR"] = "true" if options[:no_color] locate = options.delete :locate_file if options[:log] Log.severity = options[:log].to_i end if options[:command_dir] $rbbt_command_dir = Path.setup(options[:command_dir].dup) else $rbbt_command_dir = Rbbt.share.rbbt_commands end if options[:profile] require 'ruby-prof' RubyProf.start end SOPT.synopsys = "rbbt ... -a --arg1 --arg2='value' --arg3 'another-value'" SOPT.summary = "Ruby bioinformatics toolkit" SOPT.description = <<-EOF This command controls many aspects of the Rbbt framework, from configuration tasks to running applications. Commands are implemented in separate files under the Rbbt path '#{$rbbt_command_dir}'. Known locations are: #{([$rbbt_command_dir] + $rbbt_command_dir.find_all) * ", " }. You can place your own commads at #{$rbbt_command_dir.find(:user)}. EOF def commands(prev) rbbt_command_dir = $rbbt_command_dir prev.each do |previous_command| rbbt_command_dir = rbbt_command_dir[previous_command] end command_file_dirs = rbbt_command_dir.find_all command_files = command_file_dirs.collect{|d| d.glob('*') }.flatten command_files.collect{|p| File.basename(p) }.uniq.reject{|p| p =~ /\.desc$/}.sort end def rbbt_usage(prev = nil) puts puts SOPT.doc if prev puts puts Log.color :magenta, "## COMMANDS" puts puts Log.color :magenta, "Command:" puts puts " #{File.basename($0)} #{prev * " "}" puts puts Log.color :magenta, "Subcommands:" puts commands(prev).each do |command| puts " " << command end end puts end def print_error(error, backtrace = nil) puts Term::ANSIColor.red("Error:") puts puts error if backtrace puts puts Term::ANSIColor.red("Backtrace:") puts puts backtrace * "\n" end puts end dir = $rbbt_command_dir prev = [] begin while ARGV.any? command = ARGV.shift case when File.directory?(dir[command].find) prev << command dir = dir[command] when dir[command].exists? if locate puts dir[command].find exit 0 else load dir[command].find exit 0 end else error = "Command '#{command }' not understood" rbbt_usage(prev) print_error(error) exit -1 end end rbbt_usage(prev) exit 0 rescue ParameterException puts rbbt_usage print_error($!.message, $!.backtrace) puts exit -1 ensure if options[:profile] result = RubyProf.stop printer = RubyProf::FlatPrinter.new(result) printer.print(STDOUT, :min_percent => 10) end end