diff --git a/spec/config_spec.cr b/spec/config_spec.cr index 291f93b..31a8438 100644 --- a/spec/config_spec.cr +++ b/spec/config_spec.cr @@ -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 diff --git a/src/kemal.cr b/src/kemal.cr index 575ff30..a57330e 100644 --- a/src/kemal.cr +++ b/src/kemal.cr @@ -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 diff --git a/src/kemal/cli.cr b/src/kemal/cli.cr index 56c92f0..656a4e6 100644 --- a/src/kemal/cli.cr +++ b/src/kemal/cli.cr @@ -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