| 
									
										
										
										
											2018-02-04 22:09:35 +05:30
										 |  |  | require "optparse" | 
					
						
							|  |  |  | require "ostruct" | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | module Homebrew | 
					
						
							|  |  |  |   module CLI | 
					
						
							|  |  |  |     class Parser | 
					
						
							| 
									
										
										
										
											2018-03-25 17:48:22 +05:30
										 |  |  |       def self.parse(&block) | 
					
						
							|  |  |  |         new(&block).parse | 
					
						
							|  |  |  |       end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-02-04 22:09:35 +05:30
										 |  |  |       def initialize(&block) | 
					
						
							|  |  |  |         @parser = OptionParser.new | 
					
						
							|  |  |  |         @parsed_args = OpenStruct.new | 
					
						
							|  |  |  |         instance_eval(&block) | 
					
						
							|  |  |  |       end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-25 11:04:18 +05:30
										 |  |  |       def switch(*names, description: nil, env: nil) | 
					
						
							| 
									
										
										
										
											2018-02-04 22:09:35 +05:30
										 |  |  |         description = option_to_description(*names) if description.nil? | 
					
						
							| 
									
										
										
										
											2018-03-29 03:20:14 +05:30
										 |  |  |         names, env = common_switch(*names) if names.first.is_a?(Symbol) | 
					
						
							| 
									
										
										
										
											2018-02-04 22:09:35 +05:30
										 |  |  |         @parser.on(*names, description) do | 
					
						
							| 
									
										
										
										
											2018-03-25 11:04:18 +05:30
										 |  |  |           enable_switch(*names) | 
					
						
							| 
									
										
										
										
											2018-02-04 22:09:35 +05:30
										 |  |  |         end | 
					
						
							| 
									
										
										
										
											2018-03-25 11:04:18 +05:30
										 |  |  |         enable_switch(*names) if !env.nil? && !ENV["HOMEBREW_#{env.to_s.upcase}"].nil? | 
					
						
							| 
									
										
										
										
											2018-02-04 22:09:35 +05:30
										 |  |  |       end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       def comma_array(name, description: nil) | 
					
						
							|  |  |  |         description = option_to_description(name) if description.nil? | 
					
						
							|  |  |  |         @parser.on(name, OptionParser::REQUIRED_ARGUMENT, Array, description) do |list| | 
					
						
							|  |  |  |           @parsed_args[option_to_name(name)] = list | 
					
						
							|  |  |  |         end | 
					
						
							|  |  |  |       end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       def flag(name, description: nil, required: false) | 
					
						
							|  |  |  |         if required | 
					
						
							|  |  |  |           option_required = OptionParser::REQUIRED_ARGUMENT | 
					
						
							|  |  |  |         else | 
					
						
							|  |  |  |           option_required = OptionParser::OPTIONAL_ARGUMENT | 
					
						
							|  |  |  |         end | 
					
						
							|  |  |  |         description = option_to_description(name) if description.nil? | 
					
						
							|  |  |  |         @parser.on(name, description, option_required) do |option_value| | 
					
						
							|  |  |  |           @parsed_args[option_to_name(name)] = option_value | 
					
						
							|  |  |  |         end | 
					
						
							|  |  |  |       end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       def option_to_name(name) | 
					
						
							|  |  |  |         name.sub(/\A--?/, "").tr("-", "_") | 
					
						
							|  |  |  |       end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       def option_to_description(*names) | 
					
						
							| 
									
										
										
										
											2018-03-29 03:20:14 +05:30
										 |  |  |         names.map { |name| name.to_s.sub(/\A--?/, "").tr("-", " ") }.sort.last | 
					
						
							| 
									
										
										
										
											2018-02-04 22:09:35 +05:30
										 |  |  |       end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       def parse(cmdline_args = ARGV) | 
					
						
							|  |  |  |         @parser.parse!(cmdline_args) | 
					
						
							|  |  |  |         @parsed_args | 
					
						
							|  |  |  |       end | 
					
						
							| 
									
										
										
										
											2018-03-25 11:04:18 +05:30
										 |  |  | 
 | 
					
						
							|  |  |  |       private | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       def enable_switch(*names) | 
					
						
							|  |  |  |         names.each do |name| | 
					
						
							|  |  |  |           @parsed_args["#{option_to_name(name)}?"] = true | 
					
						
							|  |  |  |         end | 
					
						
							|  |  |  |       end | 
					
						
							| 
									
										
										
										
											2018-03-29 03:20:14 +05:30
										 |  |  | 
 | 
					
						
							|  |  |  |       def common_switch(name) | 
					
						
							|  |  |  |         case name | 
					
						
							|  |  |  |         when :quiet   then [["-q", "--quiet"], :quiet] | 
					
						
							|  |  |  |         when :verbose then [["-v", "--verbose"], :verbose] | 
					
						
							|  |  |  |         when :debug   then [["-d", "--debug"], :debug] | 
					
						
							|  |  |  |         else name | 
					
						
							|  |  |  |         end | 
					
						
							|  |  |  |       end | 
					
						
							| 
									
										
										
										
											2018-02-04 22:09:35 +05:30
										 |  |  |     end | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | end |