| 
									
										
										
										
											2018-02-04 22:09:35 +05:30
										 |  |  | require "optparse" | 
					
						
							|  |  |  | require "ostruct" | 
					
						
							| 
									
										
										
										
											2018-04-14 16:17:14 +05:30
										 |  |  | require "set" | 
					
						
							| 
									
										
										
										
											2018-02-04 22:09:35 +05:30
										 |  |  | 
 | 
					
						
							|  |  |  | module Homebrew | 
					
						
							|  |  |  |   module CLI | 
					
						
							|  |  |  |     class Parser | 
					
						
							| 
									
										
										
										
											2018-06-01 14:19:00 +02:00
										 |  |  |       def self.parse(args = ARGV, &block) | 
					
						
							|  |  |  |         new(&block).parse(args) | 
					
						
							| 
									
										
										
										
											2018-03-25 17:48:22 +05:30
										 |  |  |       end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-02-04 22:09:35 +05:30
										 |  |  |       def initialize(&block) | 
					
						
							|  |  |  |         @parser = OptionParser.new | 
					
						
							| 
									
										
										
										
											2018-05-05 18:40:01 +05:30
										 |  |  |         Homebrew.args = OpenStruct.new | 
					
						
							| 
									
										
										
										
											2018-04-08 16:40:02 -07:00
										 |  |  |         # undefine tap to allow --tap argument | 
					
						
							| 
									
										
										
										
											2018-05-05 18:40:01 +05:30
										 |  |  |         Homebrew.args.instance_eval { undef tap } | 
					
						
							| 
									
										
										
										
											2018-04-14 16:17:14 +05:30
										 |  |  |         @constraints = [] | 
					
						
							| 
									
										
										
										
											2018-04-01 22:01:06 +05:30
										 |  |  |         @conflicts = [] | 
					
						
							| 
									
										
										
										
											2018-02-04 22:09:35 +05:30
										 |  |  |         instance_eval(&block) | 
					
						
							|  |  |  |       end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-05-25 04:05:45 +05:30
										 |  |  |       def switch(*names, description: nil, env: nil, required_for: nil, depends_on: nil) | 
					
						
							| 
									
										
										
										
											2018-02-04 22:09:35 +05:30
										 |  |  |         description = option_to_description(*names) if description.nil? | 
					
						
							| 
									
										
										
										
											2018-04-01 16:47:30 +05:30
										 |  |  |         global_switch = names.first.is_a?(Symbol) | 
					
						
							|  |  |  |         names, env = common_switch(*names) if global_switch | 
					
						
							| 
									
										
										
										
											2018-02-04 22:09:35 +05:30
										 |  |  |         @parser.on(*names, description) do | 
					
						
							| 
									
										
										
										
											2018-05-05 18:40:01 +05:30
										 |  |  |           enable_switch(*names) | 
					
						
							| 
									
										
										
										
											2018-02-04 22:09:35 +05:30
										 |  |  |         end | 
					
						
							| 
									
										
										
										
											2018-05-25 04:05:45 +05:30
										 |  |  | 
 | 
					
						
							|  |  |  |         names.each do |name| | 
					
						
							|  |  |  |           set_constraints(name, required_for: required_for, depends_on: depends_on) | 
					
						
							|  |  |  |         end | 
					
						
							| 
									
										
										
										
											2018-05-05 18:40:01 +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| | 
					
						
							| 
									
										
										
										
											2018-05-05 18:40:01 +05:30
										 |  |  |           Homebrew.args[option_to_name(name)] = list | 
					
						
							| 
									
										
										
										
											2018-02-04 22:09:35 +05:30
										 |  |  |         end | 
					
						
							|  |  |  |       end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-04-14 16:17:14 +05:30
										 |  |  |       def flag(name, description: nil, required_for: nil, depends_on: nil) | 
					
						
							| 
									
										
										
										
											2018-04-14 19:04:24 +05:30
										 |  |  |         if name.end_with? "=" | 
					
						
							|  |  |  |           required = OptionParser::REQUIRED_ARGUMENT | 
					
						
							|  |  |  |           name.chomp! "=" | 
					
						
							| 
									
										
										
										
											2018-02-04 22:09:35 +05:30
										 |  |  |         else | 
					
						
							| 
									
										
										
										
											2018-04-14 19:04:24 +05:30
										 |  |  |           required = OptionParser::OPTIONAL_ARGUMENT | 
					
						
							| 
									
										
										
										
											2018-02-04 22:09:35 +05:30
										 |  |  |         end | 
					
						
							|  |  |  |         description = option_to_description(name) if description.nil? | 
					
						
							| 
									
										
										
										
											2018-04-14 19:04:24 +05:30
										 |  |  |         @parser.on(name, description, required) do |option_value| | 
					
						
							| 
									
										
										
										
											2018-05-05 18:40:01 +05:30
										 |  |  |           Homebrew.args[option_to_name(name)] = option_value | 
					
						
							| 
									
										
										
										
											2018-02-04 22:09:35 +05:30
										 |  |  |         end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-04-14 16:17:14 +05:30
										 |  |  |         set_constraints(name, required_for: required_for, depends_on: depends_on) | 
					
						
							| 
									
										
										
										
											2018-04-01 22:01:06 +05:30
										 |  |  |       end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-04-14 16:17:14 +05:30
										 |  |  |       def conflicts(*options) | 
					
						
							|  |  |  |         @conflicts << options.map { |option| option_to_name(option) } | 
					
						
							| 
									
										
										
										
											2018-02-04 22:09:35 +05:30
										 |  |  |       end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-06-01 14:19:00 +02:00
										 |  |  |       def option_to_name(option) | 
					
						
							|  |  |  |         option.sub(/\A--?/, "") | 
					
						
							|  |  |  |               .tr("-", "_") | 
					
						
							|  |  |  |               .delete("=") | 
					
						
							|  |  |  |       end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       def name_to_option(name) | 
					
						
							|  |  |  |         if name.length == 1
 | 
					
						
							|  |  |  |           "-#{name}" | 
					
						
							|  |  |  |         else | 
					
						
							|  |  |  |           "--#{name}" | 
					
						
							|  |  |  |         end | 
					
						
							| 
									
										
										
										
											2018-02-04 22:09:35 +05:30
										 |  |  |       end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       def option_to_description(*names) | 
					
						
							| 
									
										
										
										
											2018-04-17 10:42:41 +01:00
										 |  |  |         names.map { |name| name.to_s.sub(/\A--?/, "").tr("-", " ") }.max | 
					
						
							| 
									
										
										
										
											2018-02-04 22:09:35 +05:30
										 |  |  |       end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-06-01 14:19:00 +02:00
										 |  |  |       def parse(cmdline_args) | 
					
						
							|  |  |  |         remaining_args = @parser.parse(cmdline_args) | 
					
						
							| 
									
										
										
										
											2018-04-01 22:01:06 +05:30
										 |  |  |         check_constraint_violations | 
					
						
							| 
									
										
										
										
											2018-06-01 14:19:00 +02:00
										 |  |  |         Homebrew.args[:remaining] = remaining_args | 
					
						
							| 
									
										
										
										
											2018-02-04 22:09:35 +05:30
										 |  |  |       end | 
					
						
							| 
									
										
										
										
											2018-03-25 11:04:18 +05:30
										 |  |  | 
 | 
					
						
							|  |  |  |       private | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-05-05 18:40:01 +05:30
										 |  |  |       def enable_switch(*names) | 
					
						
							| 
									
										
										
										
											2018-03-25 11:04:18 +05:30
										 |  |  |         names.each do |name| | 
					
						
							| 
									
										
										
										
											2018-05-05 18:40:01 +05:30
										 |  |  |           Homebrew.args["#{option_to_name(name)}?"] = true | 
					
						
							| 
									
										
										
										
											2018-03-25 11:04:18 +05:30
										 |  |  |         end | 
					
						
							|  |  |  |       end | 
					
						
							| 
									
										
										
										
											2018-03-29 03:20:14 +05:30
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-04-01 16:47:30 +05:30
										 |  |  |       # These are common/global switches accessible throughout Homebrew | 
					
						
							| 
									
										
										
										
											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] | 
					
						
							| 
									
										
										
										
											2018-04-01 16:47:30 +05:30
										 |  |  |         when :force   then [["-f", "--force"], :force] | 
					
						
							| 
									
										
										
										
											2018-03-29 03:20:14 +05:30
										 |  |  |         else name | 
					
						
							|  |  |  |         end | 
					
						
							|  |  |  |       end | 
					
						
							| 
									
										
										
										
											2018-04-01 22:01:06 +05:30
										 |  |  | 
 | 
					
						
							|  |  |  |       def option_passed?(name) | 
					
						
							| 
									
										
										
										
											2018-05-05 18:40:01 +05:30
										 |  |  |         Homebrew.args.respond_to?(name) || Homebrew.args.respond_to?("#{name}?") | 
					
						
							| 
									
										
										
										
											2018-04-01 22:01:06 +05:30
										 |  |  |       end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-04-14 16:17:14 +05:30
										 |  |  |       def set_constraints(name, depends_on:, required_for:) | 
					
						
							|  |  |  |         secondary = option_to_name(name) | 
					
						
							|  |  |  |         unless required_for.nil? | 
					
						
							|  |  |  |           primary = option_to_name(required_for) | 
					
						
							|  |  |  |           @constraints << [primary, secondary, :mandatory] | 
					
						
							|  |  |  |         end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return if depends_on.nil? | 
					
						
							| 
									
										
										
										
											2018-09-17 02:45:00 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-04-14 16:17:14 +05:30
										 |  |  |         primary = option_to_name(depends_on) | 
					
						
							|  |  |  |         @constraints << [primary, secondary, :optional] | 
					
						
							|  |  |  |       end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       def check_constraints | 
					
						
							|  |  |  |         @constraints.each do |primary, secondary, constraint_type| | 
					
						
							| 
									
										
										
										
											2018-04-01 22:01:06 +05:30
										 |  |  |           primary_passed = option_passed?(primary) | 
					
						
							|  |  |  |           secondary_passed = option_passed?(secondary) | 
					
						
							| 
									
										
										
										
											2018-04-14 16:17:14 +05:30
										 |  |  |           if :mandatory.equal?(constraint_type) && primary_passed && !secondary_passed | 
					
						
							|  |  |  |             raise OptionConstraintError.new(primary, secondary) | 
					
						
							|  |  |  |           end | 
					
						
							|  |  |  |           if secondary_passed && !primary_passed | 
					
						
							|  |  |  |             raise OptionConstraintError.new(primary, secondary, missing: true) | 
					
						
							|  |  |  |           end | 
					
						
							| 
									
										
										
										
											2018-04-01 22:01:06 +05:30
										 |  |  |         end | 
					
						
							|  |  |  |       end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       def check_conflicts | 
					
						
							| 
									
										
										
										
											2018-04-14 16:17:14 +05:30
										 |  |  |         @conflicts.each do |mutually_exclusive_options_group| | 
					
						
							|  |  |  |           violations = mutually_exclusive_options_group.select do |option| | 
					
						
							|  |  |  |             option_passed? option | 
					
						
							|  |  |  |           end | 
					
						
							| 
									
										
										
										
											2018-06-01 14:19:00 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |           next if violations.count < 2
 | 
					
						
							| 
									
										
										
										
											2018-09-17 02:45:00 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-06-01 14:19:00 +02:00
										 |  |  |           raise OptionConflictError, violations.map(&method(:name_to_option)) | 
					
						
							| 
									
										
										
										
											2018-04-14 16:17:14 +05:30
										 |  |  |         end | 
					
						
							|  |  |  |       end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       def check_invalid_constraints | 
					
						
							|  |  |  |         @conflicts.each do |mutually_exclusive_options_group| | 
					
						
							|  |  |  |           @constraints.each do |p, s| | 
					
						
							|  |  |  |             next unless Set[p, s].subset?(Set[*mutually_exclusive_options_group]) | 
					
						
							| 
									
										
										
										
											2018-09-17 02:45:00 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-04-14 16:17:14 +05:30
										 |  |  |             raise InvalidConstraintError.new(p, s) | 
					
						
							|  |  |  |           end | 
					
						
							| 
									
										
										
										
											2018-04-01 22:01:06 +05:30
										 |  |  |         end | 
					
						
							|  |  |  |       end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       def check_constraint_violations | 
					
						
							| 
									
										
										
										
											2018-04-14 16:17:14 +05:30
										 |  |  |         check_invalid_constraints | 
					
						
							| 
									
										
										
										
											2018-04-01 22:01:06 +05:30
										 |  |  |         check_conflicts | 
					
						
							| 
									
										
										
										
											2018-04-14 16:17:14 +05:30
										 |  |  |         check_constraints | 
					
						
							| 
									
										
										
										
											2018-04-01 22:01:06 +05:30
										 |  |  |       end | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-04-14 16:17:14 +05:30
										 |  |  |     class OptionConstraintError < RuntimeError | 
					
						
							| 
									
										
										
										
											2018-04-01 22:01:06 +05:30
										 |  |  |       def initialize(arg1, arg2, missing: false) | 
					
						
							|  |  |  |         if !missing | 
					
						
							|  |  |  |           message = <<~EOS | 
					
						
							|  |  |  |             `#{arg1}` and `#{arg2}` should be passed together | 
					
						
							|  |  |  |           EOS | 
					
						
							|  |  |  |         else | 
					
						
							|  |  |  |           message = <<~EOS | 
					
						
							|  |  |  |             `#{arg2}` cannot be passed without `#{arg1}` | 
					
						
							|  |  |  |           EOS | 
					
						
							|  |  |  |         end | 
					
						
							|  |  |  |         super message | 
					
						
							|  |  |  |       end | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     class OptionConflictError < RuntimeError | 
					
						
							| 
									
										
										
										
											2018-04-14 16:17:14 +05:30
										 |  |  |       def initialize(args) | 
					
						
							| 
									
										
										
										
											2018-06-01 14:19:00 +02:00
										 |  |  |         args_list = args.map(&Formatter.public_method(:option)) | 
					
						
							|  |  |  |                         .join(" and ") | 
					
						
							| 
									
										
										
										
											2018-04-14 16:17:14 +05:30
										 |  |  |         super <<~EOS | 
					
						
							| 
									
										
										
										
											2018-06-01 14:19:00 +02:00
										 |  |  |           Options #{args_list} are mutually exclusive. | 
					
						
							| 
									
										
										
										
											2018-04-14 16:17:14 +05:30
										 |  |  |         EOS | 
					
						
							|  |  |  |       end | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     class InvalidConstraintError < RuntimeError | 
					
						
							| 
									
										
										
										
											2018-04-01 22:01:06 +05:30
										 |  |  |       def initialize(arg1, arg2) | 
					
						
							|  |  |  |         super <<~EOS | 
					
						
							| 
									
										
										
										
											2018-04-14 16:17:14 +05:30
										 |  |  |           `#{arg1}` and `#{arg2}` cannot be mutually exclusive and mutually dependent simultaneously | 
					
						
							| 
									
										
										
										
											2018-04-01 22:01:06 +05:30
										 |  |  |         EOS | 
					
						
							|  |  |  |       end | 
					
						
							| 
									
										
										
										
											2018-02-04 22:09:35 +05:30
										 |  |  |     end | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | end |