Sha256: 06db3ba9df3d3b7eb0bece2ee60e0eec6013f165c6042f2b0440b12669cfebb3

Contents?: true

Size: 1.43 KB

Versions: 1

Compression:

Stored size: 1.43 KB

Contents

#!/usr/bin/env ruby

require 'puma/cli'
require 'optparse'
require 'opener/core/resource_switcher'

# Puma sadly does not provide a system that allows us to cleanly inject custom
# options into their CLI. At the same time Puma doesn't provide an easy system
# of starting it *without* using the `Puma::CLI` class.
#
# To work around these problems we create our own parser and ignore any invalid
# option errors it throws up. This parser is used to handle options for the
# resource switcher.

rack_config   = File.expand_path('../../config.ru', __FILE__)
switcher      = Opener::Core::ResourceSwitcher.new
switcher_opts = {}
show_help     = false

parser = OptionParser.new do |opts|
  opts.banner = "Usage: #{File.basename($0)} [OPTIONS]"

  opts.separator "\nOptions:\n"

  # Don't abort in this block as we otherwise can't show Puma's help message.
  opts.on('-h', '--help', 'Shows this help message') do
    show_help = true

    puts parser
  end

  switcher.bind(opts, switcher_opts)

  opts.separator "\nPuma Usage:\n\n"
end

begin
  # Parse destructively so that Puma doesn't break on our custom options.
  parser.parse!(ARGV)
rescue OptionParser::InvalidOption => error
  # Catch errors generated by Puma options and ignore them.
  error.recover(ARGV)
end

# Trigger the Puma help message since we overwrite this option and parse
# destructively.
if show_help
  ARGV << '--help'
end

switcher.install(switcher_opts)

Puma::CLI.new([rack_config] + ARGV).run

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
opener-opinion-detector-1.1.1 bin/opinion-detector-server