From adf996587aecdd604eff441b8b69e4c47a8c2617 Mon Sep 17 00:00:00 2001 From: Carl Lerche Date: Mon, 23 Nov 2009 15:06:30 -0800 Subject: Extract the option parser Signed-off-by: Joshua Peek --- lib/rack/builder.rb | 10 +-- lib/rack/server.rb | 185 +++++++++++++++++++++++++++++----------------------- 2 files changed, 110 insertions(+), 85 deletions(-) diff --git a/lib/rack/builder.rb b/lib/rack/builder.rb index f769b5fd..530f0aaf 100644 --- a/lib/rack/builder.rb +++ b/lib/rack/builder.rb @@ -24,19 +24,21 @@ module Rack # You can use +map+ to construct a Rack::URLMap in a convenient way. class Builder - def self.parse_file(config, opts = nil) + def self.parse_file(config, opts = Server::Options.new) + options = {} if config =~ /\.ru$/ cfgfile = ::File.read(config) if cfgfile[/^#\\(.*)/] && opts - opts.parse! $1.split(/\s+/) + options = opts.parse! $1.split(/\s+/) end cfgfile.sub!(/^__END__\n.*/, '') - eval "Rack::Builder.new {( " + cfgfile + "\n )}.to_app", + app = eval "Rack::Builder.new {( " + cfgfile + "\n )}.to_app", TOPLEVEL_BINDING, config else require config - Object.const_get(::File.basename(config, '.rb').capitalize) + app = Object.const_get(::File.basename(config, '.rb').capitalize) end + return app, options end def initialize(&block) diff --git a/lib/rack/server.rb b/lib/rack/server.rb index ff041b3d..7d0705ad 100644 --- a/lib/rack/server.rb +++ b/lib/rack/server.rb @@ -2,6 +2,83 @@ require 'optparse' module Rack class Server + class Options + def parse!(args) + options = {} + opt_parser = OptionParser.new("", 24, ' ') do |opts| + opts.banner = "Usage: rackup [ruby options] [rack options] [rackup config]" + + opts.separator "" + opts.separator "Ruby options:" + + lineno = 1 + opts.on("-e", "--eval LINE", "evaluate a LINE of code") { |line| + eval line, TOPLEVEL_BINDING, "-e", lineno + lineno += 1 + } + + opts.on("-d", "--debug", "set debugging flags (set $DEBUG to true)") { + options[:debug] = true + } + opts.on("-w", "--warn", "turn warnings on for your script") { + options[:warn] = true + } + + opts.on("-I", "--include PATH", + "specify $LOAD_PATH (may be used more than once)") { |path| + options[:include] = path.split(":") + } + + opts.on("-r", "--require LIBRARY", + "require the library, before executing your script") { |library| + options[:require] = library + } + + opts.separator "" + opts.separator "Rack options:" + opts.on("-s", "--server SERVER", "serve using SERVER (webrick/mongrel)") { |s| + options[:server] = s + } + + opts.on("-o", "--host HOST", "listen on HOST (default: 0.0.0.0)") { |host| + options[:Host] = host + } + + opts.on("-p", "--port PORT", "use PORT (default: 9292)") { |port| + options[:Port] = port + } + + opts.on("-E", "--env ENVIRONMENT", "use ENVIRONMENT for defaults (default: development)") { |e| + options[:environment] = e + } + + opts.on("-D", "--daemonize", "run daemonized in the background") { |d| + options[:daemonize] = d ? true : false + } + + opts.on("-P", "--pid FILE", "file to store PID (default: rack.pid)") { |f| + options[:pid] = ::File.expand_path(f) + } + + opts.separator "" + opts.separator "Common options:" + + opts.on_tail("-h", "--help", "Show this message") do + puts opts + exit + end + + opts.on_tail("--version", "Show version") do + puts "Rack #{Rack.version}" + exit + end + end + opt_parser.parse! args + options[:rack_file] = args.last if args.last + options + end + end + def self.start new.start end @@ -13,18 +90,17 @@ module Rack end def options - @options ||= begin - parse_options(ARGV) - end + @options ||= parse_options(ARGV) end def default_options { :environment => "development", - :pid => nil, - :Port => 9292, - :Host => "0.0.0.0", - :AccessLog => [] + :pid => nil, + :Port => 9292, + :Host => "0.0.0.0", + :AccessLog => [], + :rack_file => ::File.expand_path("config.ru") } end @@ -34,7 +110,9 @@ module Rack abort "configuration #{options[:rack_file]} not found" end - Rack::Builder.parse_file(options[:rack_file], opt_parser) + app, options = Rack::Builder.parse_file(self.options[:rack_file], opt_parser) + self.options.merge! options + app end end @@ -52,13 +130,26 @@ module Rack end def start - if $DEBUG + if options[:debug] + $DEBUG = true require 'pp' p options[:server] pp wrapped_app pp app end + if options[:warn] + $-w = true + end + + if includes = options[:include] + $LOAD_PATH.unshift *includes + end + + if library = options[:require] + require library + end + daemonize_app if options[:daemonize] write_pid if options[:pid] server.run wrapped_app, options @@ -70,86 +161,18 @@ module Rack private def parse_options(args) - @options = default_options + options = default_options # Don't evaluate CGI ISINDEX parameters. # http://hoohoo.ncsa.uiuc.edu/cgi/cl.html args.clear if ENV.include?("REQUEST_METHOD") - opt_parser.parse! args - @options[:rack_file] = args.last || ::File.expand_path("config.ru") - @options + options.merge! opt_parser.parse! args + options end def opt_parser - @opt_parser ||= OptionParser.new("", 24, ' ') do |opts| - opts.banner = "Usage: rackup [ruby options] [rack options] [rackup config]" - - opts.separator "" - opts.separator "Ruby options:" - - lineno = 1 - opts.on("-e", "--eval LINE", "evaluate a LINE of code") { |line| - eval line, TOPLEVEL_BINDING, "-e", lineno - lineno += 1 - } - - opts.on("-d", "--debug", "set debugging flags (set $DEBUG to true)") { - $DEBUG = true - } - opts.on("-w", "--warn", "turn warnings on for your script") { - $-w = true - } - - opts.on("-I", "--include PATH", - "specify $LOAD_PATH (may be used more than once)") { |path| - $LOAD_PATH.unshift(*path.split(":")) - } - - opts.on("-r", "--require LIBRARY", - "require the library, before executing your script") { |library| - require library - } - - opts.separator "" - opts.separator "Rack options:" - opts.on("-s", "--server SERVER", "serve using SERVER (webrick/mongrel)") { |s| - @options[:server] = s - } - - opts.on("-o", "--host HOST", "listen on HOST (default: 0.0.0.0)") { |host| - @options[:Host] = host - } - - opts.on("-p", "--port PORT", "use PORT (default: 9292)") { |port| - @options[:Port] = port - } - - opts.on("-E", "--env ENVIRONMENT", "use ENVIRONMENT for defaults (default: development)") { |e| - @options[:environment] = e - } - - opts.on("-D", "--daemonize", "run daemonized in the background") { |d| - @options[:daemonize] = d ? true : false - } - - opts.on("-P", "--pid FILE", "file to store PID") { |f| - @options[:pid] = ::File.expand_path(f) - } - - opts.separator "" - opts.separator "Common options:" - - opts.on_tail("-h", "--help", "Show this message") do - puts opts - exit - end - - opts.on_tail("--version", "Show version") do - puts "Rack #{Rack.version}" - exit - end - end + Options.new end def build_app(app) -- cgit v1.2.3-24-ge0c7