| 
									
										
										
										
											2024-04-03 20:17:02 -07:00
										 |  |  | # typed: strict | 
					
						
							| 
									
										
										
										
											2019-04-19 15:38:03 +09:00
										 |  |  | # frozen_string_literal: true | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-03-18 15:50:50 -07:00
										 |  |  | require "abstract_command" | 
					
						
							| 
									
										
										
										
											2020-08-01 02:30:46 +02:00
										 |  |  | require "env_config" | 
					
						
							| 
									
										
										
										
											2020-10-03 02:45:32 +02:00
										 |  |  | require "cask/config" | 
					
						
							| 
									
										
										
										
											2019-04-17 19:43:06 +09:00
										 |  |  | require "cli/args" | 
					
						
							| 
									
										
										
										
											2024-12-07 08:09:15 -08:00
										 |  |  | require "cli/error" | 
					
						
							| 
									
										
										
										
											2024-07-14 08:49:39 -04:00
										 |  |  | require "commands" | 
					
						
							| 
									
										
										
										
											2018-02-04 22:09:35 +05:30
										 |  |  | require "optparse" | 
					
						
							| 
									
										
										
										
											2020-08-01 02:30:46 +02:00
										 |  |  | require "utils/tty" | 
					
						
							| 
									
										
										
										
											2024-07-14 08:49:39 -04:00
										 |  |  | require "utils/formatter" | 
					
						
							| 
									
										
										
										
											2018-02-04 22:09:35 +05:30
										 |  |  | 
 | 
					
						
							|  |  |  | module Homebrew | 
					
						
							|  |  |  |   module CLI | 
					
						
							|  |  |  |     class Parser | 
					
						
							| 
									
										
										
										
											2024-04-03 20:17:02 -07:00
										 |  |  |       ArgType = T.type_alias { T.any(NilClass, Symbol, T::Array[String], T::Array[Symbol]) } | 
					
						
							|  |  |  |       HIDDEN_DESC_PLACEHOLDER = "@@HIDDEN@@" | 
					
						
							|  |  |  |       SYMBOL_TO_USAGE_MAPPING = T.let({ | 
					
						
							|  |  |  |         text_or_regex: "<text>|`/`<regex>`/`", | 
					
						
							|  |  |  |         url:           "<URL>", | 
					
						
							|  |  |  |       }.freeze, T::Hash[Symbol, String]) | 
					
						
							|  |  |  |       private_constant :ArgType, :HIDDEN_DESC_PLACEHOLDER, :SYMBOL_TO_USAGE_MAPPING | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-09-10 19:12:15 -07:00
										 |  |  |       sig { returns(Args) } | 
					
						
							|  |  |  |       attr_reader :args | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-08-18 20:30:58 -07:00
										 |  |  |       sig { returns(Args::OptionsType) } | 
					
						
							| 
									
										
										
										
											2024-04-03 20:17:02 -07:00
										 |  |  |       attr_reader :processed_options | 
					
						
							| 
									
										
										
										
											2018-09-08 22:21:04 +05:30
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-04-03 20:17:02 -07:00
										 |  |  |       sig { returns(T::Boolean) } | 
					
						
							|  |  |  |       attr_reader :hide_from_man_page | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       sig { returns(ArgType) } | 
					
						
							|  |  |  |       attr_reader :named_args_type | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       sig { params(cmd_path: Pathname).returns(T.nilable(CLI::Parser)) } | 
					
						
							| 
									
										
										
										
											2020-02-02 11:32:50 +01:00
										 |  |  |       def self.from_cmd_path(cmd_path) | 
					
						
							| 
									
										
										
										
											2020-02-02 17:05:45 +01:00
										 |  |  |         cmd_args_method_name = Commands.args_method_name(cmd_path) | 
					
						
							| 
									
										
										
										
											2024-03-19 12:43:51 -07:00
										 |  |  |         cmd_name = cmd_args_method_name.to_s.delete_suffix("_args").tr("_", "-") | 
					
						
							| 
									
										
										
										
											2020-02-02 11:32:50 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |         begin | 
					
						
							| 
									
										
										
										
											2024-03-18 15:50:50 -07:00
										 |  |  |           if require?(cmd_path) | 
					
						
							|  |  |  |             cmd = Homebrew::AbstractCommand.command(cmd_name) | 
					
						
							|  |  |  |             if cmd | 
					
						
							|  |  |  |               cmd.parser | 
					
						
							|  |  |  |             else | 
					
						
							| 
									
										
										
										
											2024-03-19 12:36:30 -07:00
										 |  |  |               # FIXME: remove once commands are all subclasses of `AbstractCommand`: | 
					
						
							| 
									
										
										
										
											2024-03-18 15:50:50 -07:00
										 |  |  |               Homebrew.send(cmd_args_method_name) | 
					
						
							|  |  |  |             end | 
					
						
							|  |  |  |           end | 
					
						
							| 
									
										
										
										
											2020-02-02 11:32:50 +01:00
										 |  |  |         rescue NoMethodError => e | 
					
						
							| 
									
										
										
										
											2022-10-18 01:32:55 +01:00
										 |  |  |           raise if e.name.to_sym != cmd_args_method_name | 
					
						
							| 
									
										
										
										
											2020-02-02 11:32:50 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |           nil | 
					
						
							|  |  |  |         end | 
					
						
							|  |  |  |       end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-04-03 20:17:02 -07:00
										 |  |  |       sig { returns(T::Array[[Symbol, String, { description: String }]]) } | 
					
						
							| 
									
										
										
										
											2020-10-03 02:45:32 +02:00
										 |  |  |       def self.global_cask_options | 
					
						
							|  |  |  |         [ | 
					
						
							|  |  |  |           [:flag, "--appdir=", { | 
					
						
							| 
									
										
										
										
											2020-11-12 10:40:48 -05:00
										 |  |  |             description: "Target location for Applications " \ | 
					
						
							|  |  |  |                          "(default: `#{Cask::Config::DEFAULT_DIRS[:appdir]}`).", | 
					
						
							| 
									
										
										
										
											2020-10-03 02:45:32 +02:00
										 |  |  |           }], | 
					
						
							| 
									
										
										
										
											2023-03-26 08:10:40 +02:00
										 |  |  |           [:flag, "--keyboard-layoutdir=", { | 
					
						
							|  |  |  |             description: "Target location for Keyboard Layouts " \ | 
					
						
							|  |  |  |                          "(default: `#{Cask::Config::DEFAULT_DIRS[:keyboard_layoutdir]}`).", | 
					
						
							|  |  |  |           }], | 
					
						
							| 
									
										
										
										
											2020-10-03 02:45:32 +02:00
										 |  |  |           [:flag, "--colorpickerdir=", { | 
					
						
							| 
									
										
										
										
											2020-11-12 10:40:48 -05:00
										 |  |  |             description: "Target location for Color Pickers " \ | 
					
						
							|  |  |  |                          "(default: `#{Cask::Config::DEFAULT_DIRS[:colorpickerdir]}`).", | 
					
						
							| 
									
										
										
										
											2020-10-03 02:45:32 +02:00
										 |  |  |           }], | 
					
						
							|  |  |  |           [:flag, "--prefpanedir=", { | 
					
						
							| 
									
										
										
										
											2020-11-12 10:40:48 -05:00
										 |  |  |             description: "Target location for Preference Panes " \ | 
					
						
							|  |  |  |                          "(default: `#{Cask::Config::DEFAULT_DIRS[:prefpanedir]}`).", | 
					
						
							| 
									
										
										
										
											2020-10-03 02:45:32 +02:00
										 |  |  |           }], | 
					
						
							|  |  |  |           [:flag, "--qlplugindir=", { | 
					
						
							| 
									
										
										
										
											2024-02-03 23:54:04 +01:00
										 |  |  |             description: "Target location for Quick Look Plugins " \ | 
					
						
							| 
									
										
										
										
											2020-11-12 10:40:48 -05:00
										 |  |  |                          "(default: `#{Cask::Config::DEFAULT_DIRS[:qlplugindir]}`).", | 
					
						
							| 
									
										
										
										
											2020-10-03 02:45:32 +02:00
										 |  |  |           }], | 
					
						
							|  |  |  |           [:flag, "--mdimporterdir=", { | 
					
						
							| 
									
										
										
										
											2020-11-12 10:40:48 -05:00
										 |  |  |             description: "Target location for Spotlight Plugins " \ | 
					
						
							|  |  |  |                          "(default: `#{Cask::Config::DEFAULT_DIRS[:mdimporterdir]}`).", | 
					
						
							| 
									
										
										
										
											2020-10-03 02:45:32 +02:00
										 |  |  |           }], | 
					
						
							|  |  |  |           [:flag, "--dictionarydir=", { | 
					
						
							| 
									
										
										
										
											2020-11-12 10:40:48 -05:00
										 |  |  |             description: "Target location for Dictionaries " \ | 
					
						
							|  |  |  |                          "(default: `#{Cask::Config::DEFAULT_DIRS[:dictionarydir]}`).", | 
					
						
							| 
									
										
										
										
											2020-10-03 02:45:32 +02:00
										 |  |  |           }], | 
					
						
							|  |  |  |           [:flag, "--fontdir=", { | 
					
						
							| 
									
										
										
										
											2020-11-12 10:40:48 -05:00
										 |  |  |             description: "Target location for Fonts " \ | 
					
						
							|  |  |  |                          "(default: `#{Cask::Config::DEFAULT_DIRS[:fontdir]}`).", | 
					
						
							| 
									
										
										
										
											2020-10-03 02:45:32 +02:00
										 |  |  |           }], | 
					
						
							|  |  |  |           [:flag, "--servicedir=", { | 
					
						
							| 
									
										
										
										
											2020-11-12 10:40:48 -05:00
										 |  |  |             description: "Target location for Services " \ | 
					
						
							|  |  |  |                          "(default: `#{Cask::Config::DEFAULT_DIRS[:servicedir]}`).", | 
					
						
							| 
									
										
										
										
											2020-10-03 02:45:32 +02:00
										 |  |  |           }], | 
					
						
							| 
									
										
										
										
											2020-12-25 12:38:38 -05:00
										 |  |  |           [:flag, "--input-methoddir=", { | 
					
						
							| 
									
										
										
										
											2020-11-12 10:40:48 -05:00
										 |  |  |             description: "Target location for Input Methods " \ | 
					
						
							|  |  |  |                          "(default: `#{Cask::Config::DEFAULT_DIRS[:input_methoddir]}`).", | 
					
						
							| 
									
										
										
										
											2020-10-03 02:45:32 +02:00
										 |  |  |           }], | 
					
						
							| 
									
										
										
										
											2020-12-25 12:38:38 -05:00
										 |  |  |           [:flag, "--internet-plugindir=", { | 
					
						
							| 
									
										
										
										
											2020-11-12 10:40:48 -05:00
										 |  |  |             description: "Target location for Internet Plugins " \ | 
					
						
							|  |  |  |                          "(default: `#{Cask::Config::DEFAULT_DIRS[:internet_plugindir]}`).", | 
					
						
							| 
									
										
										
										
											2020-10-03 02:45:32 +02:00
										 |  |  |           }], | 
					
						
							| 
									
										
										
										
											2020-12-25 12:38:38 -05:00
										 |  |  |           [:flag, "--audio-unit-plugindir=", { | 
					
						
							| 
									
										
										
										
											2020-11-12 10:40:48 -05:00
										 |  |  |             description: "Target location for Audio Unit Plugins " \ | 
					
						
							|  |  |  |                          "(default: `#{Cask::Config::DEFAULT_DIRS[:audio_unit_plugindir]}`).", | 
					
						
							| 
									
										
										
										
											2020-10-03 02:45:32 +02:00
										 |  |  |           }], | 
					
						
							| 
									
										
										
										
											2020-12-25 12:38:38 -05:00
										 |  |  |           [:flag, "--vst-plugindir=", { | 
					
						
							| 
									
										
										
										
											2020-11-12 10:40:48 -05:00
										 |  |  |             description: "Target location for VST Plugins " \ | 
					
						
							|  |  |  |                          "(default: `#{Cask::Config::DEFAULT_DIRS[:vst_plugindir]}`).", | 
					
						
							| 
									
										
										
										
											2020-10-03 02:45:32 +02:00
										 |  |  |           }], | 
					
						
							| 
									
										
										
										
											2020-12-25 12:38:38 -05:00
										 |  |  |           [:flag, "--vst3-plugindir=", { | 
					
						
							| 
									
										
										
										
											2020-11-12 10:40:48 -05:00
										 |  |  |             description: "Target location for VST3 Plugins " \ | 
					
						
							|  |  |  |                          "(default: `#{Cask::Config::DEFAULT_DIRS[:vst3_plugindir]}`).", | 
					
						
							| 
									
										
										
										
											2020-10-03 02:45:32 +02:00
										 |  |  |           }], | 
					
						
							| 
									
										
										
										
											2020-12-25 12:38:38 -05:00
										 |  |  |           [:flag, "--screen-saverdir=", { | 
					
						
							| 
									
										
										
										
											2020-11-12 10:40:48 -05:00
										 |  |  |             description: "Target location for Screen Savers " \ | 
					
						
							|  |  |  |                          "(default: `#{Cask::Config::DEFAULT_DIRS[:screen_saverdir]}`).", | 
					
						
							| 
									
										
										
										
											2020-10-03 02:45:32 +02:00
										 |  |  |           }], | 
					
						
							|  |  |  |           [:comma_array, "--language", { | 
					
						
							| 
									
										
										
										
											2020-11-12 10:40:48 -05:00
										 |  |  |             description: "Comma-separated list of language codes to prefer for cask installation. " \ | 
					
						
							|  |  |  |                          "The first matching language is used, otherwise it reverts to the cask's " \ | 
					
						
							|  |  |  |                          "default language. The default value is the language of your system.", | 
					
						
							| 
									
										
										
										
											2020-10-03 02:45:32 +02:00
										 |  |  |           }], | 
					
						
							|  |  |  |         ] | 
					
						
							|  |  |  |       end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-10-20 12:03:48 +02:00
										 |  |  |       sig { returns(T::Array[[String, String, String]]) } | 
					
						
							| 
									
										
										
										
											2018-10-02 14:44:38 +05:30
										 |  |  |       def self.global_options | 
					
						
							| 
									
										
										
										
											2020-07-30 18:40:10 +02:00
										 |  |  |         [ | 
					
						
							| 
									
										
										
										
											2020-10-03 02:45:32 +02:00
										 |  |  |           ["-d", "--debug",   "Display any debugging information."], | 
					
						
							| 
									
										
										
										
											2020-11-18 10:13:57 +00:00
										 |  |  |           ["-q", "--quiet",   "Make some output more quiet."], | 
					
						
							| 
									
										
										
										
											2020-07-30 18:40:10 +02:00
										 |  |  |           ["-v", "--verbose", "Make some output more verbose."], | 
					
						
							| 
									
										
										
										
											2020-10-03 02:45:32 +02:00
										 |  |  |           ["-h", "--help",    "Show this message."], | 
					
						
							| 
									
										
										
										
											2020-07-30 18:40:10 +02:00
										 |  |  |         ] | 
					
						
							| 
									
										
										
										
											2018-10-02 14:44:38 +05:30
										 |  |  |       end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-04-03 20:17:02 -07:00
										 |  |  |       sig { params(option: String).returns(String) } | 
					
						
							|  |  |  |       def self.option_to_name(option) | 
					
						
							|  |  |  |         option.sub(/\A--?(\[no-\])?/, "").tr("-", "_").delete("=") | 
					
						
							|  |  |  |       end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-03-18 16:11:28 -07:00
										 |  |  |       sig { | 
					
						
							|  |  |  |         params(cmd: T.nilable(T.class_of(Homebrew::AbstractCommand)), block: T.nilable(T.proc.bind(Parser).void)).void | 
					
						
							|  |  |  |       } | 
					
						
							| 
									
										
										
										
											2024-03-18 15:50:50 -07:00
										 |  |  |       def initialize(cmd = nil, &block) | 
					
						
							| 
									
										
										
										
											2024-04-03 20:17:02 -07:00
										 |  |  |         @parser = T.let(OptionParser.new, OptionParser) | 
					
						
							|  |  |  |         @parser.summary_indent = "  " | 
					
						
							| 
									
										
										
										
											2020-08-01 02:30:46 +02:00
										 |  |  |         # Disable default handling of `--version` switch. | 
					
						
							|  |  |  |         @parser.base.long.delete("version") | 
					
						
							|  |  |  |         # Disable default handling of `--help` switch. | 
					
						
							|  |  |  |         @parser.base.long.delete("help") | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-04-21 16:15:57 -07:00
										 |  |  |         @args = T.let((cmd&.args_class || Args).new, Args) | 
					
						
							| 
									
										
										
										
											2020-04-18 21:14:35 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-03-18 15:50:50 -07:00
										 |  |  |         if cmd | 
					
						
							| 
									
										
										
										
											2024-04-03 20:17:02 -07:00
										 |  |  |           @command_name = T.let(cmd.command_name, String) | 
					
						
							|  |  |  |           @is_dev_cmd = T.let(cmd.dev_cmd?, T::Boolean) | 
					
						
							| 
									
										
										
										
											2024-03-18 15:50:50 -07:00
										 |  |  |         else | 
					
						
							|  |  |  |           # FIXME: remove once commands are all subclasses of `AbstractCommand`: | 
					
						
							|  |  |  |           # Filter out Sorbet runtime type checking method calls. | 
					
						
							| 
									
										
										
										
											2024-04-03 20:17:02 -07:00
										 |  |  |           cmd_location = caller_locations.select do |location| | 
					
						
							| 
									
										
										
										
											2024-03-18 15:50:50 -07:00
										 |  |  |             T.must(location.path).exclude?("/gems/sorbet-runtime-") | 
					
						
							|  |  |  |           end.fetch(1) | 
					
						
							| 
									
										
										
										
											2024-04-03 20:17:02 -07:00
										 |  |  |           @command_name = T.let(T.must(cmd_location.label).chomp("_args").tr("_", "-"), String) | 
					
						
							|  |  |  |           @is_dev_cmd = T.let(T.must(cmd_location.absolute_path).start_with?(Commands::HOMEBREW_DEV_CMD_PATH), | 
					
						
							|  |  |  |                               T::Boolean) | 
					
						
							| 
									
										
										
										
											2025-04-22 17:15:23 +01:00
										 |  |  |           odisabled( | 
					
						
							| 
									
										
										
										
											2024-09-24 10:15:34 +01:00
										 |  |  |             "`brew #{@command_name}'. This command needs to be refactored, as it is written in a style that", | 
					
						
							| 
									
										
										
										
											2025-05-10 09:23:34 -07:00
										 |  |  |             "subclassing of `Homebrew::AbstractCommand' ( see https://docs.brew.sh/External-Commands )", | 
					
						
							| 
									
										
										
										
											2024-09-24 10:15:34 +01:00
										 |  |  |             disable_for_developers: false, | 
					
						
							|  |  |  |           ) | 
					
						
							| 
									
										
										
										
											2024-04-03 20:17:02 -07:00
										 |  |  |         end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         @constraints = T.let([], T::Array[[String, String]]) | 
					
						
							|  |  |  |         @conflicts = T.let([], T::Array[T::Array[String]]) | 
					
						
							|  |  |  |         @switch_sources = T.let({}, T::Hash[String, Symbol]) | 
					
						
							| 
									
										
										
										
											2024-08-18 20:30:58 -07:00
										 |  |  |         @processed_options = T.let([], Args::OptionsType) | 
					
						
							| 
									
										
										
										
											2024-04-03 20:17:02 -07:00
										 |  |  |         @non_global_processed_options = T.let([], T::Array[[String, ArgType]]) | 
					
						
							|  |  |  |         @named_args_type = T.let(nil, T.nilable(ArgType)) | 
					
						
							|  |  |  |         @max_named_args = T.let(nil, T.nilable(Integer)) | 
					
						
							|  |  |  |         @min_named_args = T.let(nil, T.nilable(Integer)) | 
					
						
							|  |  |  |         @named_args_without_api = T.let(false, T::Boolean) | 
					
						
							|  |  |  |         @description = T.let(nil, T.nilable(String)) | 
					
						
							|  |  |  |         @usage_banner = T.let(nil, T.nilable(String)) | 
					
						
							|  |  |  |         @hide_from_man_page = T.let(false, T::Boolean) | 
					
						
							|  |  |  |         @formula_options = T.let(false, T::Boolean) | 
					
						
							|  |  |  |         @cask_options = T.let(false, T::Boolean) | 
					
						
							| 
									
										
										
										
											2020-07-30 18:40:10 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |         self.class.global_options.each do |short, long, desc| | 
					
						
							| 
									
										
										
										
											2020-08-01 02:30:46 +02:00
										 |  |  |           switch short, long, description: desc, env: option_to_name(long), method: :on_tail | 
					
						
							| 
									
										
										
										
											2020-07-30 18:40:10 +02:00
										 |  |  |         end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-16 22:18:56 +01:00
										 |  |  |         instance_eval(&block) if block | 
					
						
							| 
									
										
										
										
											2021-01-15 14:47:51 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  |         generate_banner | 
					
						
							| 
									
										
										
										
											2018-06-28 09:28:19 +05:30
										 |  |  |       end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-04-03 20:17:02 -07:00
										 |  |  |       sig { | 
					
						
							|  |  |  |         params(names: String, description: T.nilable(String), replacement: T.untyped, env: T.untyped, | 
					
						
							|  |  |  |                depends_on: T.nilable(String), method: Symbol, hidden: T::Boolean, disable: T::Boolean).void | 
					
						
							|  |  |  |       } | 
					
						
							| 
									
										
										
										
											2022-06-08 12:45:28 -07:00
										 |  |  |       def switch(*names, description: nil, replacement: nil, env: nil, depends_on: nil, | 
					
						
							| 
									
										
										
										
											2023-03-29 20:49:29 +02:00
										 |  |  |                  method: :on, hidden: false, disable: false) | 
					
						
							| 
									
										
										
										
											2018-04-01 16:47:30 +05:30
										 |  |  |         global_switch = names.first.is_a?(Symbol) | 
					
						
							| 
									
										
										
										
											2020-07-30 18:40:10 +02:00
										 |  |  |         return if global_switch | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-03-07 16:20:20 +00:00
										 |  |  |         description = option_description(description, *names, hidden:) | 
					
						
							| 
									
										
										
										
											2025-06-03 11:06:35 -04:00
										 |  |  |         env, counterpart = env | 
					
						
							|  |  |  |         if env && @non_global_processed_options.any? | 
					
						
							|  |  |  |           affix = counterpart ? " and `#{counterpart}` is passed." : "." | 
					
						
							|  |  |  |           description += " Enabled by default if `$HOMEBREW_#{env.upcase}` is set#{affix}" | 
					
						
							|  |  |  |         end | 
					
						
							| 
									
										
										
										
											2023-12-05 19:12:14 +01:00
										 |  |  |         if replacement || disable | 
					
						
							|  |  |  |           description += " (#{disable ? "disabled" : "deprecated"}#{"; replaced by #{replacement}" if replacement})" | 
					
						
							| 
									
										
										
										
											2020-11-27 14:41:05 -05:00
										 |  |  |         end | 
					
						
							| 
									
										
										
										
											2025-06-03 11:06:35 -04:00
										 |  |  |         process_option(*names, description, type: :switch, hidden:) unless disable | 
					
						
							| 
									
										
										
										
											2023-12-05 19:12:14 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-01 02:30:46 +02:00
										 |  |  |         @parser.public_send(method, *names, *wrap_option_desc(description)) do |value| | 
					
						
							| 
									
										
										
										
											2023-07-06 16:47:09 +01:00
										 |  |  |           # This odeprecated should stick around indefinitely. | 
					
						
							| 
									
										
										
										
											2023-03-29 20:49:29 +02:00
										 |  |  |           odeprecated "the `#{names.first}` switch", replacement, disable: disable if !replacement.nil? || disable | 
					
						
							| 
									
										
										
										
											2021-08-13 13:49:52 +01:00
										 |  |  |           value = true if names.none? { |name| name.start_with?("--[no-]") } | 
					
						
							| 
									
										
										
										
											2020-08-14 20:03:15 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-03-07 16:20:20 +00:00
										 |  |  |           set_switch(*names, value:, from: :args) | 
					
						
							| 
									
										
										
										
											2018-02-04 22:09:35 +05:30
										 |  |  |         end | 
					
						
							| 
									
										
										
										
											2018-05-25 04:05:45 +05:30
										 |  |  | 
 | 
					
						
							|  |  |  |         names.each do |name| | 
					
						
							| 
									
										
										
										
											2024-03-07 16:20:20 +00:00
										 |  |  |           set_constraints(name, depends_on:) | 
					
						
							| 
									
										
										
										
											2018-05-25 04:05:45 +05:30
										 |  |  |         end | 
					
						
							| 
									
										
										
										
											2018-05-05 18:40:01 +05:30
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-12-14 02:52:30 +00:00
										 |  |  |         env_value = value_for_env(env) | 
					
						
							| 
									
										
										
										
											2020-08-01 02:30:46 +02:00
										 |  |  |         set_switch(*names, value: env_value, from: :env) unless env_value.nil? | 
					
						
							| 
									
										
										
										
											2018-02-04 22:09:35 +05:30
										 |  |  |       end | 
					
						
							| 
									
										
										
										
											2019-01-18 22:03:07 +05:30
										 |  |  |       alias switch_option switch | 
					
						
							| 
									
										
										
										
											2018-02-04 22:09:35 +05:30
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-04-03 20:17:02 -07:00
										 |  |  |       sig { params(text: T.nilable(String)).returns(T.nilable(String)) } | 
					
						
							| 
									
										
										
										
											2021-01-24 01:59:31 -05:00
										 |  |  |       def description(text = nil) | 
					
						
							|  |  |  |         return @description if text.blank? | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-01-15 14:47:51 -05:00
										 |  |  |         @description = text.chomp | 
					
						
							|  |  |  |       end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-04-03 20:17:02 -07:00
										 |  |  |       sig { params(text: String).void } | 
					
						
							| 
									
										
										
										
											2018-09-08 22:21:04 +05:30
										 |  |  |       def usage_banner(text) | 
					
						
							| 
									
										
										
										
											2021-01-15 14:47:51 -05:00
										 |  |  |         @usage_banner, @description = text.chomp.split("\n\n", 2) | 
					
						
							| 
									
										
										
										
											2018-06-28 09:28:19 +05:30
										 |  |  |       end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-04-03 20:17:02 -07:00
										 |  |  |       sig { returns(T.nilable(String)) } | 
					
						
							|  |  |  |       def usage_banner_text = @parser.banner | 
					
						
							| 
									
										
										
										
											2018-09-08 22:21:04 +05:30
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-04-03 20:17:02 -07:00
										 |  |  |       sig { params(name: String, description: T.nilable(String), hidden: T::Boolean).void } | 
					
						
							| 
									
										
										
										
											2021-06-08 22:02:32 -04:00
										 |  |  |       def comma_array(name, description: nil, hidden: false) | 
					
						
							| 
									
										
										
										
											2020-03-16 15:02:43 +01:00
										 |  |  |         name = name.chomp "=" | 
					
						
							| 
									
										
										
										
											2024-03-07 16:20:20 +00:00
										 |  |  |         description = option_description(description, name, hidden:) | 
					
						
							|  |  |  |         process_option(name, description, type: :comma_array, hidden:) | 
					
						
							| 
									
										
										
										
											2018-09-08 22:21:04 +05:30
										 |  |  |         @parser.on(name, OptionParser::REQUIRED_ARGUMENT, Array, *wrap_option_desc(description)) do |list| | 
					
						
							| 
									
										
										
										
											2024-12-06 14:14:02 -08:00
										 |  |  |           set_args_method(option_to_name(name).to_sym, list) | 
					
						
							| 
									
										
										
										
											2018-02-04 22:09:35 +05:30
										 |  |  |         end | 
					
						
							|  |  |  |       end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-04-03 20:17:02 -07:00
										 |  |  |       sig { | 
					
						
							| 
									
										
										
										
											2024-04-21 14:34:55 -07:00
										 |  |  |         params(names: String, description: T.nilable(String), replacement: T.any(Symbol, String, NilClass), | 
					
						
							|  |  |  |                depends_on: T.nilable(String), hidden: T::Boolean).void | 
					
						
							| 
									
										
										
										
											2024-04-03 20:17:02 -07:00
										 |  |  |       } | 
					
						
							| 
									
										
										
										
											2022-06-08 12:45:28 -07:00
										 |  |  |       def flag(*names, description: nil, replacement: nil, depends_on: nil, hidden: false) | 
					
						
							| 
									
										
										
										
											2021-01-18 11:03:23 -05:00
										 |  |  |         required, flag_type = if names.any? { |name| name.end_with? "=" } | 
					
						
							|  |  |  |           [OptionParser::REQUIRED_ARGUMENT, :required_flag] | 
					
						
							| 
									
										
										
										
											2018-02-04 22:09:35 +05:30
										 |  |  |         else | 
					
						
							| 
									
										
										
										
											2021-01-18 11:03:23 -05:00
										 |  |  |           [OptionParser::OPTIONAL_ARGUMENT, :optional_flag] | 
					
						
							| 
									
										
										
										
											2018-02-04 22:09:35 +05:30
										 |  |  |         end | 
					
						
							| 
									
										
										
										
											2018-10-25 21:43:49 +05:30
										 |  |  |         names.map! { |name| name.chomp "=" } | 
					
						
							| 
									
										
										
										
											2024-03-07 16:20:20 +00:00
										 |  |  |         description = option_description(description, *names, hidden:) | 
					
						
							| 
									
										
										
										
											2020-12-04 11:39:02 -05:00
										 |  |  |         if replacement.nil? | 
					
						
							| 
									
										
										
										
											2024-03-07 16:20:20 +00:00
										 |  |  |           process_option(*names, description, type: flag_type, hidden:) | 
					
						
							| 
									
										
										
										
											2020-12-04 11:39:02 -05:00
										 |  |  |         else | 
					
						
							|  |  |  |           description += " (disabled#{"; replaced by #{replacement}" if replacement.present?})" | 
					
						
							|  |  |  |         end | 
					
						
							| 
									
										
										
										
											2018-10-25 21:43:49 +05:30
										 |  |  |         @parser.on(*names, *wrap_option_desc(description), required) do |option_value| | 
					
						
							| 
									
										
										
										
											2023-07-06 16:47:09 +01:00
										 |  |  |           # This odisabled should stick around indefinitely. | 
					
						
							| 
									
										
										
										
											2020-12-04 11:39:02 -05:00
										 |  |  |           odisabled "the `#{names.first}` flag", replacement unless replacement.nil? | 
					
						
							| 
									
										
										
										
											2018-10-25 21:43:49 +05:30
										 |  |  |           names.each do |name| | 
					
						
							| 
									
										
										
										
											2024-12-06 14:14:02 -08:00
										 |  |  |             set_args_method(option_to_name(name).to_sym, option_value) | 
					
						
							| 
									
										
										
										
											2018-10-25 21:43:49 +05:30
										 |  |  |           end | 
					
						
							| 
									
										
										
										
											2018-02-04 22:09:35 +05:30
										 |  |  |         end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-10-25 21:43:49 +05:30
										 |  |  |         names.each do |name| | 
					
						
							| 
									
										
										
										
											2024-03-07 16:20:20 +00:00
										 |  |  |           set_constraints(name, depends_on:) | 
					
						
							| 
									
										
										
										
											2018-10-25 21:43:49 +05:30
										 |  |  |         end | 
					
						
							| 
									
										
										
										
											2018-04-01 22:01:06 +05:30
										 |  |  |       end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-12-06 14:14:02 -08:00
										 |  |  |       sig { params(name: Symbol, value: T.untyped).void } | 
					
						
							|  |  |  |       def set_args_method(name, value) | 
					
						
							| 
									
										
										
										
											2024-12-06 20:08:02 -08:00
										 |  |  |         @args.set_arg(name, value) | 
					
						
							| 
									
										
										
										
											2024-12-06 14:14:02 -08:00
										 |  |  |         return if @args.respond_to?(name) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         @args.define_singleton_method(name) do | 
					
						
							| 
									
										
										
										
											2024-12-06 20:08:02 -08:00
										 |  |  |           # We cannot reference the ivar directly due to https://github.com/sorbet/sorbet/issues/8106 | 
					
						
							|  |  |  |           instance_variable_get(:@table).fetch(name) | 
					
						
							| 
									
										
										
										
											2024-12-06 14:14:02 -08:00
										 |  |  |         end | 
					
						
							|  |  |  |       end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-04-03 20:17:02 -07:00
										 |  |  |       sig { params(options: String).returns(T::Array[T::Array[String]]) } | 
					
						
							| 
									
										
										
										
											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 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-04-03 20:17:02 -07:00
										 |  |  |       sig { params(option: String).returns(String) } | 
					
						
							| 
									
										
										
										
											2024-03-15 15:50:07 -07:00
										 |  |  |       def option_to_name(option) = self.class.option_to_name(option) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-04-03 20:17:02 -07:00
										 |  |  |       sig { params(name: String).returns(String) } | 
					
						
							| 
									
										
										
										
											2018-06-01 14:19:00 +02:00
										 |  |  |       def name_to_option(name) | 
					
						
							|  |  |  |         if name.length == 1
 | 
					
						
							|  |  |  |           "-#{name}" | 
					
						
							|  |  |  |         else | 
					
						
							| 
									
										
										
										
											2018-11-07 23:44:34 +05:30
										 |  |  |           "--#{name.tr("_", "-")}" | 
					
						
							| 
									
										
										
										
											2018-06-01 14:19:00 +02:00
										 |  |  |         end | 
					
						
							| 
									
										
										
										
											2018-02-04 22:09:35 +05:30
										 |  |  |       end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-04-03 20:17:02 -07:00
										 |  |  |       sig { params(names: String).returns(T.nilable(String)) } | 
					
						
							| 
									
										
										
										
											2018-02-04 22:09:35 +05:30
										 |  |  |       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 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-04-03 20:17:02 -07:00
										 |  |  |       sig { params(description: T.nilable(String), names: String, hidden: T::Boolean).returns(String) } | 
					
						
							| 
									
										
										
										
											2021-06-08 22:02:32 -04:00
										 |  |  |       def option_description(description, *names, hidden: false) | 
					
						
							|  |  |  |         return HIDDEN_DESC_PLACEHOLDER if hidden | 
					
						
							|  |  |  |         return description if description.present? | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         option_to_description(*names) | 
					
						
							|  |  |  |       end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-04-03 20:17:02 -07:00
										 |  |  |       sig { | 
					
						
							|  |  |  |         params(argv: T::Array[String], ignore_invalid_options: T::Boolean) | 
					
						
							|  |  |  |           .returns([T::Array[String], T::Array[String]]) | 
					
						
							|  |  |  |       } | 
					
						
							| 
									
										
										
										
											2020-07-31 15:07:08 +02:00
										 |  |  |       def parse_remaining(argv, ignore_invalid_options: false) | 
					
						
							| 
									
										
										
										
											2020-07-30 18:40:10 +02:00
										 |  |  |         i = 0
 | 
					
						
							|  |  |  |         remaining = [] | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-31 17:41:20 +02:00
										 |  |  |         argv, non_options = split_non_options(argv) | 
					
						
							| 
									
										
										
										
											2021-08-25 14:34:57 -07:00
										 |  |  |         allow_commands = Array(@named_args_type).include?(:command) | 
					
						
							| 
									
										
										
										
											2020-07-30 18:40:10 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |         while i < argv.count | 
					
						
							|  |  |  |           begin | 
					
						
							|  |  |  |             begin | 
					
						
							|  |  |  |               arg = argv[i] | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |               remaining << arg unless @parser.parse([arg]).empty? | 
					
						
							|  |  |  |             rescue OptionParser::MissingArgument | 
					
						
							|  |  |  |               raise if i + 1 >= argv.count | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |               args = argv[i..(i + 1)] | 
					
						
							|  |  |  |               @parser.parse(args) | 
					
						
							|  |  |  |               i += 1
 | 
					
						
							|  |  |  |             end | 
					
						
							|  |  |  |           rescue OptionParser::InvalidOption | 
					
						
							| 
									
										
										
										
											2021-08-25 14:34:57 -07:00
										 |  |  |             if ignore_invalid_options || (allow_commands && Commands.path(arg)) | 
					
						
							| 
									
										
										
										
											2020-07-30 18:40:10 +02:00
										 |  |  |               remaining << arg | 
					
						
							|  |  |  |             else | 
					
						
							|  |  |  |               $stderr.puts generate_help_text | 
					
						
							|  |  |  |               raise | 
					
						
							|  |  |  |             end | 
					
						
							|  |  |  |           end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |           i += 1
 | 
					
						
							|  |  |  |         end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-31 15:07:08 +02:00
										 |  |  |         [remaining, non_options] | 
					
						
							|  |  |  |       end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-04-03 20:17:02 -07:00
										 |  |  |       sig { params(argv: T::Array[String], ignore_invalid_options: T::Boolean).returns(Args) } | 
					
						
							| 
									
										
										
										
											2020-07-31 17:37:36 +02:00
										 |  |  |       def parse(argv = ARGV.freeze, ignore_invalid_options: false) | 
					
						
							| 
									
										
										
										
											2020-07-31 15:07:08 +02:00
										 |  |  |         raise "Arguments were already parsed!" if @args_parsed | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-01-27 20:00:34 +00:00
										 |  |  |         # If we accept formula options, but the command isn't scoped only | 
					
						
							|  |  |  |         # to casks, parse once allowing invalid options so we can get the | 
					
						
							|  |  |  |         # remaining list containing formula names. | 
					
						
							| 
									
										
										
										
											2023-01-28 12:54:48 +00:00
										 |  |  |         if @formula_options && !only_casks?(argv) | 
					
						
							| 
									
										
										
										
											2020-07-31 17:37:36 +02:00
										 |  |  |           remaining, non_options = parse_remaining(argv, ignore_invalid_options: true) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |           argv = [*remaining, "--", *non_options] | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |           formulae(argv).each do |f| | 
					
						
							|  |  |  |             next if f.options.empty? | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             f.options.each do |o| | 
					
						
							|  |  |  |               name = o.flag | 
					
						
							|  |  |  |               description = "`#{f.name}`: #{o.description}" | 
					
						
							|  |  |  |               if name.end_with? "=" | 
					
						
							| 
									
										
										
										
											2024-03-07 16:20:20 +00:00
										 |  |  |                 flag(name, description:) | 
					
						
							| 
									
										
										
										
											2020-07-31 17:37:36 +02:00
										 |  |  |               else | 
					
						
							| 
									
										
										
										
											2024-03-07 16:20:20 +00:00
										 |  |  |                 switch name, description: | 
					
						
							| 
									
										
										
										
											2020-07-31 17:37:36 +02:00
										 |  |  |               end | 
					
						
							| 
									
										
										
										
											2020-10-03 02:45:32 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |               conflicts "--cask", name | 
					
						
							| 
									
										
										
										
											2020-07-31 17:37:36 +02:00
										 |  |  |             end | 
					
						
							|  |  |  |           end | 
					
						
							|  |  |  |         end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-03-07 16:20:20 +00:00
										 |  |  |         remaining, non_options = parse_remaining(argv, ignore_invalid_options:) | 
					
						
							| 
									
										
										
										
											2020-07-31 15:07:08 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-30 18:40:10 +02:00
										 |  |  |         named_args = if ignore_invalid_options | 
					
						
							|  |  |  |           [] | 
					
						
							|  |  |  |         else | 
					
						
							|  |  |  |           remaining + non_options | 
					
						
							| 
									
										
										
										
											2019-01-12 18:52:07 +05:30
										 |  |  |         end | 
					
						
							| 
									
										
										
										
											2020-05-05 12:50:41 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-01 02:30:46 +02:00
										 |  |  |         unless ignore_invalid_options | 
					
						
							| 
									
										
										
										
											2024-12-06 18:09:56 -08:00
										 |  |  |           unless @is_dev_cmd | 
					
						
							|  |  |  |             set_default_options | 
					
						
							|  |  |  |             validate_options | 
					
						
							|  |  |  |           end | 
					
						
							| 
									
										
										
										
											2022-12-31 11:17:17 -08:00
										 |  |  |           check_constraint_violations | 
					
						
							| 
									
										
										
										
											2020-08-01 02:30:46 +02:00
										 |  |  |           check_named_args(named_args) | 
					
						
							|  |  |  |         end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-06-19 03:57:52 +01:00
										 |  |  |         @args.freeze_named_args!(named_args, cask_options: @cask_options, without_api: @named_args_without_api) | 
					
						
							| 
									
										
										
										
											2020-07-31 15:07:08 +02:00
										 |  |  |         @args.freeze_remaining_args!(non_options.empty? ? remaining : [*remaining, "--", non_options]) | 
					
						
							| 
									
										
										
										
											2019-12-11 00:23:51 +05:30
										 |  |  |         @args.freeze_processed_options!(@processed_options) | 
					
						
							| 
									
										
										
										
											2021-03-18 14:46:48 +00:00
										 |  |  |         @args.freeze | 
					
						
							| 
									
										
										
										
											2020-05-05 12:50:41 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-04-03 20:17:02 -07:00
										 |  |  |         @args_parsed = T.let(true, T.nilable(TrueClass)) | 
					
						
							| 
									
										
										
										
											2020-08-01 02:30:46 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-12-03 22:08:01 -08:00
										 |  |  |         if !ignore_invalid_options && @args.help? | 
					
						
							|  |  |  |           puts generate_help_text | 
					
						
							|  |  |  |           exit | 
					
						
							| 
									
										
										
										
											2020-08-01 02:30:46 +02:00
										 |  |  |         end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-23 01:22:25 +02:00
										 |  |  |         @args | 
					
						
							| 
									
										
										
										
											2018-02-04 22:09:35 +05:30
										 |  |  |       end | 
					
						
							| 
									
										
										
										
											2018-03-25 11:04:18 +05:30
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-12-06 18:09:56 -08:00
										 |  |  |       sig { void } | 
					
						
							|  |  |  |       def set_default_options; end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-04-03 20:17:02 -07:00
										 |  |  |       sig { void } | 
					
						
							| 
									
										
										
										
											2022-09-28 21:57:13 -07:00
										 |  |  |       def validate_options; end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-04-03 20:17:02 -07:00
										 |  |  |       sig { returns(String) } | 
					
						
							| 
									
										
										
										
											2018-10-03 21:12:44 +05:30
										 |  |  |       def generate_help_text | 
					
						
							| 
									
										
										
										
											2024-04-03 20:17:02 -07:00
										 |  |  |         Formatter.format_help_text(@parser.to_s, width: Formatter::COMMAND_DESC_WIDTH) | 
					
						
							| 
									
										
										
										
											2021-06-08 22:02:32 -04:00
										 |  |  |                  .gsub(/\n.*?@@HIDDEN@@.*?(?=\n)/, "") | 
					
						
							| 
									
										
										
										
											2020-08-01 02:30:46 +02:00
										 |  |  |                  .sub(/^/, "#{Tty.bold}Usage: brew#{Tty.reset} ") | 
					
						
							|  |  |  |                  .gsub(/`(.*?)`/m, "#{Tty.bold}\\1#{Tty.reset}") | 
					
						
							|  |  |  |                  .gsub(%r{<([^\s]+?://[^\s]+?)>}) { |url| Formatter.url(url) } | 
					
						
							| 
									
										
										
										
											2020-12-18 20:54:34 -08:00
										 |  |  |                  .gsub(/\*(.*?)\*|<(.*?)>/m) do |underlined| | 
					
						
							|  |  |  |                    underlined[1...-1].gsub(/^(\s*)(.*?)$/, "\\1#{Tty.underline}\\2#{Tty.reset}") | 
					
						
							|  |  |  |                  end | 
					
						
							| 
									
										
										
										
											2018-10-03 21:12:44 +05:30
										 |  |  |       end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-04-03 20:17:02 -07:00
										 |  |  |       sig { void } | 
					
						
							| 
									
										
										
										
											2020-10-03 02:45:32 +02:00
										 |  |  |       def cask_options | 
					
						
							| 
									
										
										
										
											2022-10-08 01:08:15 +01:00
										 |  |  |         self.class.global_cask_options.each do |args| | 
					
						
							| 
									
										
										
										
											2024-04-03 20:17:02 -07:00
										 |  |  |           options = T.cast(args.pop, T::Hash[Symbol, String]) | 
					
						
							| 
									
										
										
										
											2022-10-08 01:08:15 +01:00
										 |  |  |           send(*args, **options) | 
					
						
							| 
									
										
										
										
											2024-04-03 20:17:02 -07:00
										 |  |  |           conflicts "--formula", args[1] | 
					
						
							| 
									
										
										
										
											2020-10-03 02:45:32 +02:00
										 |  |  |         end | 
					
						
							| 
									
										
										
										
											2021-03-18 14:46:48 +00:00
										 |  |  |         @cask_options = true | 
					
						
							| 
									
										
										
										
											2020-10-03 02:45:32 +02:00
										 |  |  |       end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-10-20 12:03:48 +02:00
										 |  |  |       sig { void } | 
					
						
							| 
									
										
										
										
											2019-01-29 19:25:13 +00:00
										 |  |  |       def formula_options | 
					
						
							| 
									
										
										
										
											2020-07-31 17:37:36 +02:00
										 |  |  |         @formula_options = true | 
					
						
							| 
									
										
										
										
											2019-01-29 19:25:13 +00:00
										 |  |  |       end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-01-17 22:45:55 -08:00
										 |  |  |       sig { | 
					
						
							| 
									
										
										
										
											2021-01-10 14:26:40 -05:00
										 |  |  |         params( | 
					
						
							| 
									
										
										
										
											2024-04-03 20:17:02 -07:00
										 |  |  |           type:        ArgType, | 
					
						
							| 
									
										
										
										
											2023-06-19 03:57:52 +01:00
										 |  |  |           number:      T.nilable(Integer), | 
					
						
							|  |  |  |           min:         T.nilable(Integer), | 
					
						
							|  |  |  |           max:         T.nilable(Integer), | 
					
						
							|  |  |  |           without_api: T::Boolean, | 
					
						
							| 
									
										
										
										
											2021-01-10 14:26:40 -05:00
										 |  |  |         ).void | 
					
						
							| 
									
										
										
										
											2021-01-17 22:45:55 -08:00
										 |  |  |       } | 
					
						
							| 
									
										
										
										
											2023-06-19 03:57:52 +01:00
										 |  |  |       def named_args(type = nil, number: nil, min: nil, max: nil, without_api: false) | 
					
						
							| 
									
										
										
										
											2024-12-06 11:06:27 -08:00
										 |  |  |         raise ArgumentError, "Do not specify both `number` and `min` or `max`" if number && (min || max) | 
					
						
							| 
									
										
										
										
											2021-01-10 14:26:40 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-12-06 11:06:27 -08:00
										 |  |  |         if type == :none && (number || min || max) | 
					
						
							| 
									
										
										
										
											2021-01-10 14:26:40 -05:00
										 |  |  |           raise ArgumentError, "Do not specify both `number`, `min` or `max` with `named_args :none`" | 
					
						
							|  |  |  |         end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         @named_args_type = type | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if type == :none | 
					
						
							|  |  |  |           @max_named_args = 0
 | 
					
						
							| 
									
										
										
										
											2024-04-03 20:17:02 -07:00
										 |  |  |         elsif number | 
					
						
							| 
									
										
										
										
											2021-01-10 14:26:40 -05:00
										 |  |  |           @min_named_args = @max_named_args = number | 
					
						
							| 
									
										
										
										
											2024-04-03 20:17:02 -07:00
										 |  |  |         elsif min || max | 
					
						
							| 
									
										
										
										
											2021-01-10 14:26:40 -05:00
										 |  |  |           @min_named_args = min | 
					
						
							|  |  |  |           @max_named_args = max | 
					
						
							|  |  |  |         end | 
					
						
							| 
									
										
										
										
											2023-06-19 03:57:52 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |         @named_args_without_api = without_api | 
					
						
							| 
									
										
										
										
											2021-01-10 14:26:40 -05:00
										 |  |  |       end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-10-20 12:03:48 +02:00
										 |  |  |       sig { void } | 
					
						
							| 
									
										
										
										
											2019-01-30 21:34:10 +00:00
										 |  |  |       def hide_from_man_page! | 
					
						
							|  |  |  |         @hide_from_man_page = true | 
					
						
							|  |  |  |       end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-25 11:04:18 +05:30
										 |  |  |       private | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-04-03 20:17:02 -07:00
										 |  |  |       sig { returns(String) } | 
					
						
							| 
									
										
										
										
											2021-01-15 14:47:51 -05:00
										 |  |  |       def generate_usage_banner | 
					
						
							| 
									
										
										
										
											2021-01-18 12:57:35 -05:00
										 |  |  |         command_names = ["`#{@command_name}`"] | 
					
						
							| 
									
										
										
										
											2021-01-15 14:47:51 -05:00
										 |  |  |         aliases_to_skip = %w[instal uninstal] | 
					
						
							| 
									
										
										
										
											2024-02-22 23:29:55 +00:00
										 |  |  |         command_names += Commands::HOMEBREW_INTERNAL_COMMAND_ALIASES.filter_map do |command_alias, command| | 
					
						
							| 
									
										
										
										
											2021-01-15 14:47:51 -05:00
										 |  |  |           next if aliases_to_skip.include? command_alias | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-01-18 12:57:35 -05:00
										 |  |  |           "`#{command_alias}`" if command == @command_name | 
					
						
							| 
									
										
										
										
											2024-02-22 23:29:55 +00:00
										 |  |  |         end.sort | 
					
						
							| 
									
										
										
										
											2021-01-15 14:47:51 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-01-16 00:35:43 -05:00
										 |  |  |         options = if @non_global_processed_options.empty? | 
					
						
							|  |  |  |           "" | 
					
						
							|  |  |  |         elsif @non_global_processed_options.count > 2
 | 
					
						
							| 
									
										
										
										
											2021-01-15 14:47:51 -05:00
										 |  |  |           " [<options>]" | 
					
						
							|  |  |  |         else | 
					
						
							| 
									
										
										
										
											2021-01-18 11:03:23 -05:00
										 |  |  |           required_argument_types = [:required_flag, :comma_array] | 
					
						
							| 
									
										
										
										
											2021-01-16 00:35:43 -05:00
										 |  |  |           @non_global_processed_options.map do |option, type| | 
					
						
							| 
									
										
										
										
											2023-02-10 23:15:40 -05:00
										 |  |  |             next " [`#{option}=`]" if required_argument_types.include? type | 
					
						
							| 
									
										
										
										
											2021-01-16 00:35:43 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-02-10 23:15:40 -05:00
										 |  |  |             " [`#{option}`]" | 
					
						
							| 
									
										
										
										
											2021-01-16 00:35:43 -05:00
										 |  |  |           end.join | 
					
						
							| 
									
										
										
										
											2021-01-15 14:47:51 -05:00
										 |  |  |         end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         named_args = "" | 
					
						
							|  |  |  |         if @named_args_type.present? && @named_args_type != :none | 
					
						
							|  |  |  |           arg_type = if @named_args_type.is_a? Array | 
					
						
							| 
									
										
										
										
											2024-02-22 23:29:55 +00:00
										 |  |  |             types = @named_args_type.filter_map do |type| | 
					
						
							| 
									
										
										
										
											2021-01-15 14:47:51 -05:00
										 |  |  |               next unless type.is_a? Symbol | 
					
						
							|  |  |  |               next SYMBOL_TO_USAGE_MAPPING[type] if SYMBOL_TO_USAGE_MAPPING.key?(type) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |               "<#{type}>" | 
					
						
							| 
									
										
										
										
											2024-02-22 23:29:55 +00:00
										 |  |  |             end | 
					
						
							| 
									
										
										
										
											2021-03-01 13:43:47 +00:00
										 |  |  |             types << "<subcommand>" if @named_args_type.any?(String) | 
					
						
							| 
									
										
										
										
											2021-01-15 14:47:51 -05:00
										 |  |  |             types.join("|") | 
					
						
							|  |  |  |           elsif SYMBOL_TO_USAGE_MAPPING.key? @named_args_type | 
					
						
							|  |  |  |             SYMBOL_TO_USAGE_MAPPING[@named_args_type] | 
					
						
							|  |  |  |           else | 
					
						
							|  |  |  |             "<#{@named_args_type}>" | 
					
						
							|  |  |  |           end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-12-06 11:06:27 -08:00
										 |  |  |           named_args = if @min_named_args.nil? && @max_named_args == 1
 | 
					
						
							| 
									
										
										
										
											2021-01-15 14:47:51 -05:00
										 |  |  |             " [#{arg_type}]" | 
					
						
							| 
									
										
										
										
											2024-12-06 11:06:27 -08:00
										 |  |  |           elsif @min_named_args.nil? | 
					
						
							| 
									
										
										
										
											2021-01-15 14:47:51 -05:00
										 |  |  |             " [#{arg_type} ...]" | 
					
						
							|  |  |  |           elsif @min_named_args == 1 && @max_named_args == 1
 | 
					
						
							|  |  |  |             " #{arg_type}" | 
					
						
							|  |  |  |           elsif @min_named_args == 1
 | 
					
						
							|  |  |  |             " #{arg_type} [...]" | 
					
						
							|  |  |  |           else | 
					
						
							|  |  |  |             " #{arg_type} ..." | 
					
						
							|  |  |  |           end | 
					
						
							|  |  |  |         end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         "#{command_names.join(", ")}#{options}#{named_args}" | 
					
						
							|  |  |  |       end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-04-03 20:17:02 -07:00
										 |  |  |       sig { returns(String) } | 
					
						
							| 
									
										
										
										
											2021-01-15 14:47:51 -05:00
										 |  |  |       def generate_banner | 
					
						
							|  |  |  |         @usage_banner ||= generate_usage_banner | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         @parser.banner = <<~BANNER | 
					
						
							|  |  |  |           #{@usage_banner} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |           #{@description} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         BANNER | 
					
						
							|  |  |  |       end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-04-03 20:17:02 -07:00
										 |  |  |       sig { params(names: String, value: T.untyped, from: Symbol).void } | 
					
						
							| 
									
										
										
										
											2020-08-01 02:30:46 +02:00
										 |  |  |       def set_switch(*names, value:, from:) | 
					
						
							| 
									
										
										
										
											2018-03-25 11:04:18 +05:30
										 |  |  |         names.each do |name| | 
					
						
							| 
									
										
										
										
											2019-02-15 11:01:03 -05:00
										 |  |  |           @switch_sources[option_to_name(name)] = from | 
					
						
							| 
									
										
										
										
											2024-12-06 14:14:02 -08:00
										 |  |  |           set_args_method(:"#{option_to_name(name)}?", value) | 
					
						
							| 
									
										
										
										
											2018-03-25 11:04:18 +05:30
										 |  |  |         end | 
					
						
							|  |  |  |       end | 
					
						
							| 
									
										
										
										
											2018-03-29 03:20:14 +05:30
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-04-03 20:17:02 -07:00
										 |  |  |       sig { params(args: String).void } | 
					
						
							| 
									
										
										
										
											2023-10-07 23:47:00 -04:00
										 |  |  |       def disable_switch(*args) | 
					
						
							|  |  |  |         args.each do |name| | 
					
						
							| 
									
										
										
										
											2024-11-30 13:52:46 -08:00
										 |  |  |           result = if name.start_with?("--[no-]") | 
					
						
							| 
									
										
										
										
											2021-03-18 14:46:48 +00:00
										 |  |  |             nil | 
					
						
							|  |  |  |           else | 
					
						
							|  |  |  |             false | 
					
						
							|  |  |  |           end | 
					
						
							| 
									
										
										
										
											2024-12-06 14:14:02 -08:00
										 |  |  |           set_args_method(:"#{option_to_name(name)}?", result) | 
					
						
							| 
									
										
										
										
											2019-02-15 00:31:13 -05:00
										 |  |  |         end | 
					
						
							|  |  |  |       end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-04-03 20:17:02 -07:00
										 |  |  |       sig { params(name: String).returns(T::Boolean) } | 
					
						
							| 
									
										
										
										
											2018-04-01 22:01:06 +05:30
										 |  |  |       def option_passed?(name) | 
					
						
							| 
									
										
										
										
											2024-11-30 13:52:46 -08:00
										 |  |  |         [name.to_sym, :"#{name}?"].any? do |method| | 
					
						
							|  |  |  |           @args.public_send(method) if @args.respond_to?(method) | 
					
						
							|  |  |  |         end | 
					
						
							| 
									
										
										
										
											2018-04-01 22:01:06 +05:30
										 |  |  |       end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-04-03 20:17:02 -07:00
										 |  |  |       sig { params(desc: String).returns(T::Array[String]) } | 
					
						
							| 
									
										
										
										
											2018-10-02 14:44:38 +05:30
										 |  |  |       def wrap_option_desc(desc) | 
					
						
							| 
									
										
										
										
											2024-04-03 20:17:02 -07:00
										 |  |  |         Formatter.format_help_text(desc, width: Formatter::OPTION_DESC_WIDTH).split("\n") | 
					
						
							| 
									
										
										
										
											2018-10-02 14:44:38 +05:30
										 |  |  |       end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-04-03 20:17:02 -07:00
										 |  |  |       sig { params(name: String, depends_on: T.nilable(String)).returns(T.nilable(T::Array[[String, String]])) } | 
					
						
							| 
									
										
										
										
											2022-06-08 12:45:28 -07:00
										 |  |  |       def set_constraints(name, depends_on:) | 
					
						
							| 
									
										
										
										
											2018-04-14 16:17:14 +05:30
										 |  |  |         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) | 
					
						
							| 
									
										
										
										
											2022-06-08 12:45:28 -07:00
										 |  |  |         secondary = option_to_name(name) | 
					
						
							|  |  |  |         @constraints << [primary, secondary] | 
					
						
							| 
									
										
										
										
											2018-04-14 16:17:14 +05:30
										 |  |  |       end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-04-03 20:17:02 -07:00
										 |  |  |       sig { void } | 
					
						
							| 
									
										
										
										
											2018-04-14 16:17:14 +05:30
										 |  |  |       def check_constraints | 
					
						
							| 
									
										
										
										
											2022-06-08 12:45:28 -07:00
										 |  |  |         @constraints.each do |primary, secondary| | 
					
						
							| 
									
										
										
										
											2018-04-01 22:01:06 +05:30
										 |  |  |           primary_passed = option_passed?(primary) | 
					
						
							|  |  |  |           secondary_passed = option_passed?(secondary) | 
					
						
							| 
									
										
										
										
											2020-12-24 16:29:44 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-06-08 12:45:28 -07:00
										 |  |  |           next if !secondary_passed || (primary_passed && secondary_passed) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-12-24 16:29:44 +00:00
										 |  |  |           primary = name_to_option(primary) | 
					
						
							|  |  |  |           secondary = name_to_option(secondary) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-06-08 12:45:28 -07:00
										 |  |  |           raise OptionConstraintError.new(primary, secondary, missing: true) | 
					
						
							| 
									
										
										
										
											2018-04-01 22:01:06 +05:30
										 |  |  |         end | 
					
						
							|  |  |  |       end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-04-03 20:17:02 -07:00
										 |  |  |       sig { void } | 
					
						
							| 
									
										
										
										
											2018-04-01 22:01:06 +05:30
										 |  |  |       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
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-02-15 00:31:13 -05:00
										 |  |  |           env_var_options = violations.select do |option| | 
					
						
							| 
									
										
										
										
											2019-02-25 14:54:31 -05:00
										 |  |  |             @switch_sources[option_to_name(option)] == :env | 
					
						
							| 
									
										
										
										
											2019-02-15 00:31:13 -05:00
										 |  |  |           end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-02-15 01:16:07 -05:00
										 |  |  |           select_cli_arg = violations.count - env_var_options.count == 1
 | 
					
						
							| 
									
										
										
										
											2024-04-08 09:47:06 -07:00
										 |  |  |           raise OptionConflictError, violations.map { name_to_option(_1) } unless select_cli_arg | 
					
						
							| 
									
										
										
										
											2019-02-19 13:12:52 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-04-08 09:47:06 -07:00
										 |  |  |           env_var_options.each { disable_switch(_1) } | 
					
						
							| 
									
										
										
										
											2018-04-14 16:17:14 +05:30
										 |  |  |         end | 
					
						
							|  |  |  |       end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-04-03 20:17:02 -07:00
										 |  |  |       sig { void } | 
					
						
							| 
									
										
										
										
											2018-04-14 16:17:14 +05:30
										 |  |  |       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 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-04-03 20:17:02 -07:00
										 |  |  |       sig { void } | 
					
						
							| 
									
										
										
										
											2018-04-01 22:01:06 +05:30
										 |  |  |       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 | 
					
						
							| 
									
										
										
										
											2018-09-08 22:21:04 +05:30
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-04-03 20:17:02 -07:00
										 |  |  |       sig { params(args: T::Array[String]).void } | 
					
						
							| 
									
										
										
										
											2020-07-30 18:40:10 +02:00
										 |  |  |       def check_named_args(args) | 
					
						
							| 
									
										
										
										
											2024-02-22 23:29:55 +00:00
										 |  |  |         types = Array(@named_args_type).filter_map do |type| | 
					
						
							| 
									
										
										
										
											2021-01-23 15:06:44 -05:00
										 |  |  |           next type if type.is_a? Symbol | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |           :subcommand | 
					
						
							| 
									
										
										
										
											2024-02-22 23:29:55 +00:00
										 |  |  |         end.uniq | 
					
						
							| 
									
										
										
										
											2021-01-23 15:06:44 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-01-23 15:25:56 -05:00
										 |  |  |         exception = if @min_named_args && @max_named_args && @min_named_args == @max_named_args && | 
					
						
							|  |  |  |                        args.size != @max_named_args | 
					
						
							| 
									
										
										
										
											2024-03-07 16:20:20 +00:00
										 |  |  |           NumberOfNamedArgumentsError.new(@min_named_args, types:) | 
					
						
							| 
									
										
										
										
											2021-01-23 15:25:56 -05:00
										 |  |  |         elsif @min_named_args && args.size < @min_named_args | 
					
						
							| 
									
										
										
										
											2024-03-07 16:20:20 +00:00
										 |  |  |           MinNamedArgumentsError.new(@min_named_args, types:) | 
					
						
							| 
									
										
										
										
											2020-10-20 12:03:48 +02:00
										 |  |  |         elsif @max_named_args && args.size > @max_named_args | 
					
						
							| 
									
										
										
										
											2024-03-07 16:20:20 +00:00
										 |  |  |           MaxNamedArgumentsError.new(@max_named_args, types:) | 
					
						
							| 
									
										
										
										
											2020-03-04 17:27:25 +00:00
										 |  |  |         end | 
					
						
							| 
									
										
										
										
											2020-10-20 12:03:48 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |         raise exception if exception | 
					
						
							| 
									
										
										
										
											2019-12-13 16:50:54 -05:00
										 |  |  |       end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-04-03 20:17:02 -07:00
										 |  |  |       sig { params(args: String, type: Symbol, hidden: T::Boolean).void } | 
					
						
							| 
									
										
										
										
											2021-04-09 09:30:36 +01:00
										 |  |  |       def process_option(*args, type:, hidden: false) | 
					
						
							| 
									
										
										
										
											2018-09-08 22:21:04 +05:30
										 |  |  |         option, = @parser.make_switch(args) | 
					
						
							| 
									
										
										
										
											2021-01-15 00:13:10 -05:00
										 |  |  |         @processed_options.reject! { |existing| existing.second == option.long.first } if option.long.first.present? | 
					
						
							| 
									
										
										
										
											2024-08-19 13:15:34 -07:00
										 |  |  |         @processed_options << [option.short.first, option.long.first, option.desc.first, hidden] | 
					
						
							| 
									
										
										
										
											2021-01-16 00:35:43 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-10-08 19:12:43 +01:00
										 |  |  |         args.pop # last argument is the description | 
					
						
							| 
									
										
										
										
											2021-03-18 14:46:48 +00:00
										 |  |  |         if type == :switch | 
					
						
							|  |  |  |           disable_switch(*args) | 
					
						
							|  |  |  |         else | 
					
						
							|  |  |  |           args.each do |name| | 
					
						
							| 
									
										
										
										
											2024-12-06 14:14:02 -08:00
										 |  |  |             set_args_method(option_to_name(name).to_sym, nil) | 
					
						
							| 
									
										
										
										
											2021-03-18 14:46:48 +00:00
										 |  |  |           end | 
					
						
							|  |  |  |         end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-06-08 22:02:32 -04:00
										 |  |  |         return if hidden | 
					
						
							| 
									
										
										
										
											2021-01-16 00:35:43 -05:00
										 |  |  |         return if self.class.global_options.include? [option.short.first, option.long.first, option.desc.first] | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         @non_global_processed_options << [option.long.first || option.short.first, type] | 
					
						
							| 
									
										
										
										
											2018-09-08 22:21:04 +05:30
										 |  |  |       end | 
					
						
							| 
									
										
										
										
											2020-05-10 15:10:36 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-04-03 20:17:02 -07:00
										 |  |  |       sig { params(argv: T::Array[String]).returns([T::Array[String], T::Array[String]]) } | 
					
						
							| 
									
										
										
										
											2020-07-31 17:41:20 +02:00
										 |  |  |       def split_non_options(argv) | 
					
						
							| 
									
										
										
										
											2021-02-12 18:33:37 +05:30
										 |  |  |         if (sep = argv.index("--")) | 
					
						
							| 
									
										
										
										
											2020-07-30 18:40:10 +02:00
										 |  |  |           [argv.take(sep), argv.drop(sep + 1)] | 
					
						
							|  |  |  |         else | 
					
						
							|  |  |  |           [argv, []] | 
					
						
							|  |  |  |         end | 
					
						
							|  |  |  |       end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-04-03 20:17:02 -07:00
										 |  |  |       sig { params(argv: T::Array[String]).returns(T::Array[Formula]) } | 
					
						
							| 
									
										
										
										
											2020-07-30 18:40:10 +02:00
										 |  |  |       def formulae(argv) | 
					
						
							| 
									
										
										
										
											2020-07-31 17:41:20 +02:00
										 |  |  |         argv, non_options = split_non_options(argv) | 
					
						
							| 
									
										
										
										
											2020-07-30 18:40:10 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-31 17:37:36 +02:00
										 |  |  |         named_args = argv.reject { |arg| arg.start_with?("-") } + non_options | 
					
						
							| 
									
										
										
										
											2020-07-30 18:40:10 +02:00
										 |  |  |         spec = if argv.include?("--HEAD") | 
					
						
							| 
									
										
										
										
											2020-05-10 15:10:36 +01:00
										 |  |  |           :head | 
					
						
							|  |  |  |         else | 
					
						
							|  |  |  |           :stable | 
					
						
							|  |  |  |         end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         # Only lowercase names, not paths, bottle filenames or URLs | 
					
						
							| 
									
										
										
										
											2024-02-22 23:29:55 +00:00
										 |  |  |         named_args.filter_map do |arg| | 
					
						
							| 
									
										
										
										
											2020-05-10 15:10:36 +01:00
										 |  |  |           next if arg.match?(HOMEBREW_CASK_TAP_CASK_REGEX) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-31 17:37:36 +02:00
										 |  |  |           begin | 
					
						
							|  |  |  |             Formulary.factory(arg, spec, flags: argv.select { |a| a.start_with?("--") }) | 
					
						
							| 
									
										
										
										
											2023-10-21 17:15:26 -07:00
										 |  |  |           rescue FormulaUnavailableError, FormulaSpecificationError | 
					
						
							| 
									
										
										
										
											2020-07-31 17:37:36 +02:00
										 |  |  |             nil | 
					
						
							|  |  |  |           end | 
					
						
							| 
									
										
										
										
											2024-02-22 23:29:55 +00:00
										 |  |  |         end.uniq(&:name) | 
					
						
							| 
									
										
										
										
											2020-05-10 15:10:36 +01:00
										 |  |  |       end | 
					
						
							| 
									
										
										
										
											2023-01-28 12:54:48 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-04-03 20:17:02 -07:00
										 |  |  |       sig { params(argv: T::Array[String]).returns(T::Boolean) } | 
					
						
							| 
									
										
										
										
											2023-01-28 12:54:48 +00:00
										 |  |  |       def only_casks?(argv) | 
					
						
							|  |  |  |         argv.include?("--casks") || argv.include?("--cask") | 
					
						
							|  |  |  |       end | 
					
						
							| 
									
										
										
										
											2024-04-03 20:17:02 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |       sig { params(env: T.any(NilClass, String, Symbol)).returns(T.untyped) } | 
					
						
							|  |  |  |       def value_for_env(env) | 
					
						
							|  |  |  |         return if env.blank? | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         method_name = :"#{env}?" | 
					
						
							|  |  |  |         if Homebrew::EnvConfig.respond_to?(method_name) | 
					
						
							|  |  |  |           Homebrew::EnvConfig.public_send(method_name) | 
					
						
							|  |  |  |         else | 
					
						
							|  |  |  |           ENV.fetch("HOMEBREW_#{env.upcase}", nil) | 
					
						
							|  |  |  |         end | 
					
						
							|  |  |  |       end | 
					
						
							| 
									
										
										
										
											2018-04-01 22:01:06 +05:30
										 |  |  |     end | 
					
						
							| 
									
										
										
										
											2018-02-04 22:09:35 +05:30
										 |  |  |   end | 
					
						
							|  |  |  | end | 
					
						
							| 
									
										
										
										
											2022-09-28 21:57:13 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | require "extend/os/parser" |