Add support for parsing command line parameters from other sources than ARGV (#483)

This commit is contained in:
Diego Guraieb 2018-11-30 13:16:08 -03:00 committed by Serdar Dogruyol
parent 50f48509fd
commit a5d8df7382
3 changed files with 18 additions and 13 deletions

View file

@ -51,7 +51,7 @@ describe "Config" do
test_option = opt
end
end
Kemal::CLI.new
Kemal::CLI.new ARGV
test_option.should eq("FOOBAR")
end

View file

@ -7,25 +7,28 @@ require "./kemal/helpers/*"
module Kemal
# Overload of `self.run` with the default startup logging.
def self.run(port : Int32?)
self.run(port) { }
def self.run(port : Int32?, args = ARGV)
self.run(port, args) { }
end
# Overload of `self.run` without port.
def self.run
self.run(nil)
def self.run(args = ARGV)
self.run(nil, args: args)
end
# Overload of `self.run` to allow just a block.
def self.run(&block)
self.run nil, &block
def self.run(args = ARGV, &block)
self.run(nil, args: args, &block)
end
# The command to run a `Kemal` application.
#
# If *port* is not given Kemal will use `Kemal::Config#port`
def self.run(port : Int32? = nil, &block)
Kemal::CLI.new
#
# To use custom command line arguments, set args to nil
#
def self.run(port : Int32? = nil, args = ARGV, &block)
Kemal::CLI.new args
config = Kemal.config
config.setup
config.port = port if port

View file

@ -3,18 +3,20 @@ require "option_parser"
module Kemal
# Handles all the initialization from the command line.
class CLI
def initialize
def initialize(args)
@ssl_enabled = false
@key_file = ""
@cert_file = ""
@config = Kemal.config
read_env
parse
if args
parse args
end
configure_ssl
end
private def parse
OptionParser.parse! do |opts|
private def parse(args : Array(String))
OptionParser.parse args do |opts|
opts.on("-b HOST", "--bind HOST", "Host to bind (defaults to 0.0.0.0)") do |host_binding|
@config.host_binding = host_binding
end