From 9c6b80cb63d1163c00cba04cad031a7504706ea6 Mon Sep 17 00:00:00 2001 From: Samuel Cochran Date: Fri, 27 May 2011 12:19:04 +0800 Subject: [PATCH] Refactor option parsing and running out of bin file. --- bin/mailcatcher | 42 +------------------------------------ lib/mail_catcher.rb | 51 ++++++++++++++++++++++++++++++++++++++++----- 2 files changed, 47 insertions(+), 46 deletions(-) diff --git a/bin/mailcatcher b/bin/mailcatcher index cfabee8..46ea7c5 100755 --- a/bin/mailcatcher +++ b/bin/mailcatcher @@ -1,44 +1,4 @@ #!/usr/bin/env ruby -$: << File.expand_path(File.join(File.dirname(__FILE__), '../lib')) - require 'mail_catcher' -require 'optparse' - -options = {} - -OptionParser.new do |opts| - opts.banner = 'Usage: mailcatcher [options]' - - options[:smtp_ip] = '127.0.0.1' - opts.on('--smtp-ip IP', 'Set the ip address of the smtp server') do |ip| - options[:smtp_ip] = ip - end - - options[:smtp_port] = 1025 - opts.on('--smtp-port PORT', Integer, 'Set the port of the smtp server') do |port| - options[:smtp_port] = port - end - - options[:http_ip] = '127.0.0.1' - opts.on('--http-ip IP', 'Set the ip address of the http server') do |ip| - options[:http_ip] = ip - end - - options[:http_port] = 1080 - opts.on('--http-port PORT', Integer, 'Set the port address of the http server') do |port| - options[:http_port] = port - end - - options[:verbose] = false - opts.on('-v', '--verbose', 'Be more verbose') do - options[:verbose] = true - end - - opts.on('-h', '--help', 'Display this help information') do - puts opts - exit! - end -end.parse! - -MailCatcher.run(options) +MailCatcher.run! diff --git a/lib/mail_catcher.rb b/lib/mail_catcher.rb index b90cb7a..f5b1647 100644 --- a/lib/mail_catcher.rb +++ b/lib/mail_catcher.rb @@ -7,11 +7,52 @@ module MailCatcher autoload :Smtp, 'mail_catcher/smtp' autoload :Web, 'mail_catcher/web' - def self.run(options = {}) - options[:smtp_ip] ||= '127.0.0.1' - options[:smtp_port] ||= 1025 - options[:http_ip] ||= '127.0.0.1' - options[:http_port] ||= 1080 + @@defaults = { + :smtp_ip => '127.0.0.1', + :smtp_port => '1025', + :http_ip => '127.0.0.1', + :http_port => '1080', + :verbose => false, + } + + def self.parse! arguments=ARGV, defaults=@@defaults + @@defaults.dup.tap do |options| + OptionParser.new do |parser| + parser.banner = 'Usage: mailcatcher [options]' + + parser.on('--smtp-ip IP', 'Set the ip address of the smtp server') do |ip| + options[:smtp_ip] = ip + end + + parser.on('--smtp-port PORT', Integer, 'Set the port of the smtp server') do |port| + options[:smtp_port] = port + end + + parser.on('--http-ip IP', 'Set the ip address of the http server') do |ip| + options[:http_ip] = ip + end + + parser.on('--http-port PORT', Integer, 'Set the port address of the http server') do |port| + options[:http_port] = port + end + + parser.on('-v', '--verbose', 'Be more verbose') do + options[:verbose] = true + end + + parser.on('-h', '--help', 'Display this help information') do + puts parser + exit! + end + end.parse! + end + end + + def self.run! options=nil + # If we are passed options, fill in the blanks + options &&= @@defaults.merge options + # Otherwise, parse them from ARGV + options ||= parse! puts "Starting MailCatcher" puts "==> smtp://#{options[:smtp_ip]}:#{options[:smtp_port]}"