From 1f809c68c8e81297c3dcfcdd14c65c41f3b10165 Mon Sep 17 00:00:00 2001 From: Ben Jolitz Date: Tue, 22 Mar 2016 14:13:49 -0700 Subject: [PATCH 1/2] Support additional options as part of the Config This commit adds the ability to add a closure suitable for adding additional options. It is expected to allow someone to set global, module or class level variables so they can pass changes/options suitable for making decisions. --- spec/config_spec.cr | 15 +++++++++++++++ src/kemal/cli.cr | 4 ++++ src/kemal/config.cr | 3 ++- 3 files changed, 21 insertions(+), 1 deletion(-) diff --git a/spec/config_spec.cr b/spec/config_spec.cr index cc0cbb4..4cd6b3e 100644 --- a/spec/config_spec.cr +++ b/spec/config_spec.cr @@ -28,4 +28,19 @@ describe "Config" do config.add_handler CustomTestHandler.new config.handlers.size.should eq(5) end + + it "adds custom options" do + config = Kemal.config + ARGV.push("--test") + ARGV.push("FOOBAR") + $test_option = nil + + config.on_options = ->(parser : OptionParser) { + parser.on("--test TEST_OPTION", "Test an option") do |opt| + $test_option = opt + end + } + Kemal::CLI.new + $test_option.should eq("FOOBAR") + end end diff --git a/src/kemal/cli.cr b/src/kemal/cli.cr index d074f09..9e24acd 100644 --- a/src/kemal/cli.cr +++ b/src/kemal/cli.cr @@ -35,6 +35,10 @@ module Kemal puts opts exit 0 end + unless @config.on_options.nil? + c = @config.on_options.not_nil! + c.call opts + end end end diff --git a/src/kemal/config.cr b/src/kemal/config.cr index f9b6b6b..6b926b8 100644 --- a/src/kemal/config.cr +++ b/src/kemal/config.cr @@ -3,7 +3,7 @@ module Kemal INSTANCE = Config.new HANDLERS = [] of HTTP::Handler property host_binding, ssl, port, env, public_folder, logging, - always_rescue, error_handler, serve_static, run + always_rescue, error_handler, serve_static, run, on_options def initialize @host_binding = "0.0.0.0" @@ -16,6 +16,7 @@ module Kemal @always_rescue = true @error_handler = nil @run = false + @on_options = nil end def logger From 108e732274b55f42b28b1b5e88fdf45fce1559f1 Mon Sep 17 00:00:00 2001 From: Ben Jolitz Date: Sat, 7 May 2016 17:18:42 -0700 Subject: [PATCH 2/2] rename on_options -> extra_options --- spec/config_spec.cr | 2 +- src/kemal/cli.cr | 4 ++-- src/kemal/config.cr | 4 ++-- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/spec/config_spec.cr b/spec/config_spec.cr index 4cd6b3e..1da8908 100644 --- a/spec/config_spec.cr +++ b/spec/config_spec.cr @@ -35,7 +35,7 @@ describe "Config" do ARGV.push("FOOBAR") $test_option = nil - config.on_options = ->(parser : OptionParser) { + config.extra_options = ->(parser : OptionParser) { parser.on("--test TEST_OPTION", "Test an option") do |opt| $test_option = opt end diff --git a/src/kemal/cli.cr b/src/kemal/cli.cr index 9e24acd..8e5f198 100644 --- a/src/kemal/cli.cr +++ b/src/kemal/cli.cr @@ -35,8 +35,8 @@ module Kemal puts opts exit 0 end - unless @config.on_options.nil? - c = @config.on_options.not_nil! + unless @config.extra_options.nil? + c = @config.extra_options.not_nil! c.call opts end end diff --git a/src/kemal/config.cr b/src/kemal/config.cr index 6b926b8..504018e 100644 --- a/src/kemal/config.cr +++ b/src/kemal/config.cr @@ -3,7 +3,7 @@ module Kemal INSTANCE = Config.new HANDLERS = [] of HTTP::Handler property host_binding, ssl, port, env, public_folder, logging, - always_rescue, error_handler, serve_static, run, on_options + always_rescue, error_handler, serve_static, run, extra_options def initialize @host_binding = "0.0.0.0" @@ -16,7 +16,7 @@ module Kemal @always_rescue = true @error_handler = nil @run = false - @on_options = nil + @extra_options = nil end def logger