mirror of
				https://gitea.invidious.io/iv-org/shard-kemal.git
				synced 2024-08-15 00:53:36 +00:00 
			
		
		
		
	Add support for parsing command line parameters from other sources than ARGV (#483)
This commit is contained in:
		
							parent
							
								
									50f48509fd
								
							
						
					
					
						commit
						a5d8df7382
					
				
					 3 changed files with 18 additions and 13 deletions
				
			
		|  | @ -51,7 +51,7 @@ describe "Config" do | ||||||
|         test_option = opt |         test_option = opt | ||||||
|       end |       end | ||||||
|     end |     end | ||||||
|     Kemal::CLI.new |     Kemal::CLI.new ARGV | ||||||
|     test_option.should eq("FOOBAR") |     test_option.should eq("FOOBAR") | ||||||
|   end |   end | ||||||
| 
 | 
 | ||||||
|  |  | ||||||
							
								
								
									
										19
									
								
								src/kemal.cr
									
										
									
									
									
								
							
							
						
						
									
										19
									
								
								src/kemal.cr
									
										
									
									
									
								
							|  | @ -7,25 +7,28 @@ require "./kemal/helpers/*" | ||||||
| 
 | 
 | ||||||
| module Kemal | module Kemal | ||||||
|   # Overload of `self.run` with the default startup logging. |   # Overload of `self.run` with the default startup logging. | ||||||
|   def self.run(port : Int32?) |   def self.run(port : Int32?, args = ARGV) | ||||||
|     self.run(port) { } |     self.run(port, args) { } | ||||||
|   end |   end | ||||||
| 
 | 
 | ||||||
|   # Overload of `self.run` without port. |   # Overload of `self.run` without port. | ||||||
|   def self.run |   def self.run(args = ARGV) | ||||||
|     self.run(nil) |     self.run(nil, args: args) | ||||||
|   end |   end | ||||||
| 
 | 
 | ||||||
|   # Overload of `self.run` to allow just a block. |   # Overload of `self.run` to allow just a block. | ||||||
|   def self.run(&block) |   def self.run(args = ARGV, &block) | ||||||
|     self.run nil, &block |     self.run(nil, args: args, &block) | ||||||
|   end |   end | ||||||
| 
 | 
 | ||||||
|   # The command to run a `Kemal` application. |   # The command to run a `Kemal` application. | ||||||
|   # |   # | ||||||
|   # If *port* is not given Kemal will use `Kemal::Config#port` |   # 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 = Kemal.config | ||||||
|     config.setup |     config.setup | ||||||
|     config.port = port if port |     config.port = port if port | ||||||
|  |  | ||||||
|  | @ -3,18 +3,20 @@ require "option_parser" | ||||||
| module Kemal | module Kemal | ||||||
|   # Handles all the initialization from the command line. |   # Handles all the initialization from the command line. | ||||||
|   class CLI |   class CLI | ||||||
|     def initialize |     def initialize(args) | ||||||
|       @ssl_enabled = false |       @ssl_enabled = false | ||||||
|       @key_file = "" |       @key_file = "" | ||||||
|       @cert_file = "" |       @cert_file = "" | ||||||
|       @config = Kemal.config |       @config = Kemal.config | ||||||
|       read_env |       read_env | ||||||
|       parse |       if args | ||||||
|  |         parse args | ||||||
|  |       end | ||||||
|       configure_ssl |       configure_ssl | ||||||
|     end |     end | ||||||
| 
 | 
 | ||||||
|     private def parse |     private def parse(args : Array(String)) | ||||||
|       OptionParser.parse! do |opts| |       OptionParser.parse args do |opts| | ||||||
|         opts.on("-b HOST", "--bind HOST", "Host to bind (defaults to 0.0.0.0)") do |host_binding| |         opts.on("-b HOST", "--bind HOST", "Host to bind (defaults to 0.0.0.0)") do |host_binding| | ||||||
|           @config.host_binding = host_binding |           @config.host_binding = host_binding | ||||||
|         end |         end | ||||||
|  |  | ||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue