Refactor option parsing and running out of bin file.

This commit is contained in:
Samuel Cochran 2011-05-27 12:19:04 +08:00
parent cbf6a77b5d
commit 9c6b80cb63
2 changed files with 47 additions and 46 deletions

View File

@ -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!

View File

@ -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]}"