require 'optparse' require 'rformat' require 'rformat/environment' module RFormat class << self def application @application ||= RFormat::Application.new end end class Application attr_reader :args, :command, :options, :environment def initialize(output = $stdout, env = RFormat::Environment.new) @output = output @environment = env @args = Array.try_convert(ARGV) end def run parse_command parse_options if @command && @command !~ /^-/ self.send(@command, @args) else help end end def help @output.puts File.read File.join(RFormat::root_dir, 'HELP.txt') end def list(args) @output.puts "Formatters:" @output.puts @environment.formatters.keys.map { |f| " #{f}\n" } end def version version = RFormat::Version copyright = RFormat::Copyright @output.puts "#{version}\n#{copyright}" end def write(formats) config = { color: true, formats: {} } formats.each do |format| config[:formats][format] = @environment.formatters[format] if @environment.formatters[format] end @environment.config = config if config[:formats].empty? @output.puts "No formatters matching #{formats.join(', ')}" else @environment.write_config @output.puts "rspec now using format(s): #{config[:formats].values.join(', ')}" end end def parse_command command = @args.first == 'list' ? @args.shift : @args.first command = command.to_sym if command.respond_to? :to_sym @command = command == :list ? :list : :write end def parse_options @options = {} OptionParser.new do |opts| opts.on("-v", "--version", "Version info") do @options[:version] = true version end opts.on('-h', '--help', 'Display help') do @options[:help] = true help end end.parse! end end end