def handle_cli_input
if File.exists? options[:app_file]
$APP_PATH = File.dirname(options[:app_file]).gsub(/\/(lib|bin)\/?$/, '')
else
require 'rubygems'
matches = Gem::source_index.find_name(app)
raise LoadError, "#{app} gem doesn't appear to be installed!" if matches.empty?
gem_spec = matches.last
$APP_PATH = gem_spec.full_gem_path
gem(app)
end
$: << $APP_PATH+"/lib"
$PID_FILE = "/etc/#{app}/#{app}.pid"
OptionParser.new do |opts|
opts.banner = "Usage: #{app} [options]"
opts.on("-c", "--config FILE", "Use config file (default is /etc/#{app}/config.yml)") do |c|
puts "Using config file #{c}"
$CONFIG_FILE = c
end
opts.on("-d", "--daemonize", "Run as a daemon (only when using webrick or mongrel)") do |c|
$DAEMONIZE = true
end
opts.on("-P", "--pid_file FILE", "Use pid file (default is /etc/#{app}/#{app}.pid)") do |c|
if $DAEMONIZE && !File.exists?(c)
puts "Using pid file '#{c}'"
$PID_FILE = c
elsif File.exists?(c)
puts "The pid file already exists. Is #{app} running?\n" +
"You will have to first manually remove the pid file at '#{c}' to start the server as a daemon."
exit 1
else
puts "Not running as daemon. Ignoring pid option"
end
end
if @options[:extra_cli_options]
@options[:extra_cli_options].call(opts)
end
opts.on_tail("-h", "--help", "Show this message") do
puts opts
exit
end
opts.on_tail("-V", "--version", "Show version number") do
require "#{$APP_PATH}/lib/#{app}/version.rb"
app_mod = Object.const_get(@options[:app_module])
puts "#{app}-#{app_mod::VERSION::STRING}"
exit
end
end.parse!
require "#{$APP_PATH}/lib/#{app}.rb"
end