Class: Rack::Server::Options

Inherits:
Object
  • Object
show all
Defined in:
lib/rack/server/options.rb

Instance Method Summary collapse

Instance Method Details

#handler_opts(options) ⇒ Object



98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/rack/server/options.rb', line 98

def handler_opts(options)
  begin
    info = []
    server = Rack::Handler.get(options[:server]) || Rack::Handler.default(options)
    if server && server.respond_to?(:valid_options)
      info << ""
      info << "Server-specific options for #{server.name}:"

      has_options = false
      server.valid_options.each do |name, description|
        next if name.to_s.match(/^(Host|Port)[^a-zA-Z]/) # ignore handler's host and port options, we do our own.
        info << "  -O %-21s %s" % [name, description]
        has_options = true
      end
      return "" if !has_options
    end
    info.join("\n")
  rescue NameError
    return "Warning: Could not find handler specified (#{options[:server] || 'default'}) to determine handler-specific options"
  end
end

#parse!(args) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/rack/server/options.rb', line 4

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("--eval LINE", "evaluate a LINE of code") do |line|
      eval line, TOPLEVEL_BINDING, "-e", lineno
      lineno += 1
    end

    opts.on("--builder BUILDER_LINE", "evaluate a BUILDER_LINE of code as a builder script") do |line|
      options[:builder] = line
    end

    opts.on("--debug", "set debugging flags (set $DEBUG to true)") do
      options[:debug] = true
    end

    opts.on("--warn", "turn warnings on for your script") do
      options[:warn] = true
    end

    opts.on("--quiet", "turn off logging") do
      options[:quiet] = true
    end

    opts.on("--include PATH",
            "specify $LOAD_PATH (may be used more than once)") do |path|
      (options[:include] ||= []).concat(path.split(":"))
    end

    opts.on("--require LIBRARY", "require the library, before executing your script") do |library|
      options[:require] = library
    end

    opts.separator ""
    opts.separator "Rack options:"

    opts.on("-s", "--server SERVER", "serve using SERVER (thin/puma/webrick/mongrel)") do |s|
      options[:server] = s
    end

    opts.on("-o", "--host HOST", "listen on HOST (default: 0.0.0.0)") do |host|
      options[:Host] = host
    end

    opts.on("-p", "--port PORT", "use PORT (default: 9292)") do |port|
      options[:Port] = port
    end

    opts.on("-O", "--option NAME[=VALUE]", "pass VALUE to the server as option NAME. If no VALUE, sets it to true. Run '#{$0} -s SERVER -h' to get a list of options for SERVER") do |name|
      name, value = name.split('=', 2)
      value = true if value.nil?
      options[name.to_sym] = value
    end

    opts.on("--env ENVIRONMENT", "use ENVIRONMENT for defaults (default: development)") do |e|
      options[:environment] = e
    end

    opts.on("--pid FILE", "file to store PID") do |file|
      options[:pid] = ::File.expand_path(file)
    end

    opts.separator ""
    opts.separator "Common options:"

    opts.on_tail("-h", "-?", "--help", "Show this message") do
      puts opts
      puts handler_opts(options)
      exit
    end

    opts.on_tail("--version", "Show version") do
      puts "Rack #{Rack.version} (Release: #{Rack.release})"
      exit
    end
  end

  begin
    opt_parser.parse! args
  rescue OptionParser::InvalidOption => e
    warn e.message
    abort opt_parser.to_s
  end

  options[:config] = args.last if args.last
  options
end