mirror of
				https://gitea.invidious.io/iv-org/shard-kemal.git
				synced 2024-08-15 00:53:36 +00:00 
			
		
		
		
	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.
This commit is contained in:
		
							parent
							
								
									839d120a50
								
							
						
					
					
						commit
						1f809c68c8
					
				
					 3 changed files with 21 additions and 1 deletions
				
			
		|  | @ -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 | ||||
|  |  | |||
|  | @ -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 | ||||
| 
 | ||||
|  |  | |||
|  | @ -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 | ||||
|  |  | |||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue