2020-10-10 14:16:11 +02:00
|
|
|
# typed: false
|
2019-04-19 15:38:03 +09:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
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"
|
2018-02-04 22:09:35 +05:30
|
|
|
require "optparse"
|
2018-04-14 16:17:14 +05:30
|
|
|
require "set"
|
2020-08-01 02:30:46 +02:00
|
|
|
require "utils/tty"
|
2018-02-04 22:09:35 +05:30
|
|
|
|
2019-03-09 13:00:15 -05:00
|
|
|
COMMAND_DESC_WIDTH = 80
|
|
|
|
OPTION_DESC_WIDTH = 43
|
|
|
|
|
2018-02-04 22:09:35 +05:30
|
|
|
module Homebrew
|
|
|
|
module CLI
|
|
|
|
class Parser
|
2020-10-20 12:03:48 +02:00
|
|
|
extend T::Sig
|
|
|
|
|
2019-09-13 09:34:43 +01:00
|
|
|
attr_reader :processed_options, :hide_from_man_page
|
2018-09-08 22:21:04 +05:30
|
|
|
|
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)
|
2020-02-02 11:32:50 +01:00
|
|
|
|
|
|
|
begin
|
|
|
|
Homebrew.send(cmd_args_method_name) if require?(cmd_path)
|
|
|
|
rescue NoMethodError => e
|
|
|
|
raise if e.name != cmd_args_method_name
|
|
|
|
|
|
|
|
nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-10-03 02:45:32 +02:00
|
|
|
def self.global_cask_options
|
|
|
|
[
|
|
|
|
[:flag, "--appdir=", {
|
|
|
|
description: "Target location for Applications. " \
|
|
|
|
"Default: `#{Cask::Config::DEFAULT_DIRS[:appdir]}`",
|
|
|
|
}],
|
|
|
|
[:flag, "--colorpickerdir=", {
|
|
|
|
description: "Target location for Color Pickers. " \
|
|
|
|
"Default: `#{Cask::Config::DEFAULT_DIRS[:colorpickerdir]}`",
|
|
|
|
}],
|
|
|
|
[:flag, "--prefpanedir=", {
|
|
|
|
description: "Target location for Preference Panes. " \
|
|
|
|
"Default: `#{Cask::Config::DEFAULT_DIRS[:prefpanedir]}`",
|
|
|
|
}],
|
|
|
|
[:flag, "--qlplugindir=", {
|
|
|
|
description: "Target location for QuickLook Plugins. " \
|
|
|
|
"Default: `#{Cask::Config::DEFAULT_DIRS[:qlplugindir]}`",
|
|
|
|
}],
|
|
|
|
[:flag, "--mdimporterdir=", {
|
|
|
|
description: "Target location for Spotlight Plugins. " \
|
|
|
|
"Default: `#{Cask::Config::DEFAULT_DIRS[:mdimporterdir]}`",
|
|
|
|
}],
|
|
|
|
[:flag, "--dictionarydir=", {
|
|
|
|
description: "Target location for Dictionaries. " \
|
|
|
|
"Default: `#{Cask::Config::DEFAULT_DIRS[:dictionarydir]}`",
|
|
|
|
}],
|
|
|
|
[:flag, "--fontdir=", {
|
|
|
|
description: "Target location for Fonts. " \
|
|
|
|
"Default: `#{Cask::Config::DEFAULT_DIRS[:fontdir]}`",
|
|
|
|
}],
|
|
|
|
[:flag, "--servicedir=", {
|
|
|
|
description: "Target location for Services. " \
|
|
|
|
"Default: `#{Cask::Config::DEFAULT_DIRS[:servicedir]}`",
|
|
|
|
}],
|
|
|
|
[:flag, "--input_methoddir=", {
|
|
|
|
description: "Target location for Input Methods. " \
|
|
|
|
"Default: `#{Cask::Config::DEFAULT_DIRS[:input_methoddir]}`",
|
|
|
|
}],
|
|
|
|
[:flag, "--internet_plugindir=", {
|
|
|
|
description: "Target location for Internet Plugins. " \
|
|
|
|
"Default: `#{Cask::Config::DEFAULT_DIRS[:internet_plugindir]}`",
|
|
|
|
}],
|
|
|
|
[:flag, "--audio_unit_plugindir=", {
|
|
|
|
description: "Target location for Audio Unit Plugins. " \
|
|
|
|
"Default: `#{Cask::Config::DEFAULT_DIRS[:audio_unit_plugindir]}`",
|
|
|
|
}],
|
|
|
|
[:flag, "--vst_plugindir=", {
|
|
|
|
description: "Target location for VST Plugins. " \
|
|
|
|
"Default: `#{Cask::Config::DEFAULT_DIRS[:vst_plugindir]}`",
|
|
|
|
}],
|
|
|
|
[:flag, "--vst3_plugindir=", {
|
|
|
|
description: "Target location for VST3 Plugins. " \
|
|
|
|
"Default: `#{Cask::Config::DEFAULT_DIRS[:vst3_plugindir]}`",
|
|
|
|
}],
|
|
|
|
[:flag, "--screen_saverdir=", {
|
|
|
|
description: "Target location for Screen Savers. " \
|
|
|
|
"Default: `#{Cask::Config::DEFAULT_DIRS[:screen_saverdir]}`",
|
|
|
|
}],
|
|
|
|
[:comma_array, "--language", {
|
|
|
|
description: "Set language of the Cask to install. The first matching " \
|
|
|
|
"language is used, otherwise the default language on the Cask. " \
|
|
|
|
"The default value is the `language of your system`",
|
|
|
|
}],
|
|
|
|
]
|
|
|
|
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."],
|
|
|
|
["-q", "--quiet", "Suppress any warnings."],
|
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
|
|
|
|
|
2020-10-20 12:03:48 +02:00
|
|
|
# FIXME: Block should be `T.nilable(T.proc.bind(Parser).void)`.
|
|
|
|
# See https://github.com/sorbet/sorbet/issues/498.
|
|
|
|
sig { params(block: T.proc.bind(Parser).void).void.checked(:never) }
|
2020-07-31 17:37:36 +02:00
|
|
|
def initialize(&block)
|
2018-02-04 22:09:35 +05:30
|
|
|
@parser = OptionParser.new
|
2020-07-30 18:40:10 +02:00
|
|
|
|
2020-08-01 02:30:46 +02:00
|
|
|
@parser.summary_indent = " " * 2
|
|
|
|
|
|
|
|
# Disable default handling of `--version` switch.
|
|
|
|
@parser.base.long.delete("version")
|
|
|
|
|
|
|
|
# Disable default handling of `--help` switch.
|
|
|
|
@parser.base.long.delete("help")
|
|
|
|
|
2020-07-31 17:37:36 +02:00
|
|
|
@args = Homebrew::CLI::Args.new
|
2020-04-18 21:14:35 +01:00
|
|
|
|
2018-04-14 16:17:14 +05:30
|
|
|
@constraints = []
|
2018-04-01 22:01:06 +05:30
|
|
|
@conflicts = []
|
2019-02-15 00:31:13 -05:00
|
|
|
@switch_sources = {}
|
2018-09-08 22:21:04 +05:30
|
|
|
@processed_options = []
|
2019-12-13 16:50:54 -05:00
|
|
|
@max_named_args = nil
|
2020-03-04 17:27:25 +00:00
|
|
|
@min_named_args = nil
|
|
|
|
@min_named_type = nil
|
2019-01-30 21:34:10 +00:00
|
|
|
@hide_from_man_page = false
|
2020-07-31 17:37:36 +02:00
|
|
|
@formula_options = false
|
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
|
|
|
|
|
|
|
|
instance_eval(&block) if block_given?
|
2018-06-28 09:28:19 +05:30
|
|
|
end
|
|
|
|
|
2020-08-01 02:30:46 +02:00
|
|
|
def switch(*names, description: nil, env: nil, required_for: nil, depends_on: nil, method: :on)
|
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
|
|
|
|
|
|
|
|
description = option_to_description(*names) if description.nil?
|
2018-09-08 22:21:04 +05:30
|
|
|
process_option(*names, description)
|
2020-08-01 02:30:46 +02:00
|
|
|
@parser.public_send(method, *names, *wrap_option_desc(description)) do |value|
|
2020-08-14 20:03:15 +02:00
|
|
|
value = if names.any? { |name| name.start_with?("--[no-]") }
|
|
|
|
value
|
|
|
|
else
|
|
|
|
true
|
|
|
|
end
|
|
|
|
|
2020-08-01 02:30:46 +02:00
|
|
|
set_switch(*names, value: value, from: :args)
|
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
|
|
|
|
2020-08-01 02:30:46 +02:00
|
|
|
env_value = env?(env)
|
|
|
|
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
|
|
|
|
2020-02-01 13:32:39 +01:00
|
|
|
def env?(env)
|
2020-08-01 02:30:46 +02:00
|
|
|
return if env.blank?
|
2020-04-05 15:44:50 +01:00
|
|
|
|
2020-08-02 14:07:49 +02:00
|
|
|
Homebrew::EnvConfig.try(:"#{env}?")
|
2020-02-01 13:32:39 +01:00
|
|
|
end
|
|
|
|
|
2018-09-08 22:21:04 +05:30
|
|
|
def usage_banner(text)
|
2020-08-01 02:30:46 +02:00
|
|
|
@parser.banner = "#{text}\n"
|
2018-06-28 09:28:19 +05:30
|
|
|
end
|
|
|
|
|
2018-09-08 22:21:04 +05:30
|
|
|
def usage_banner_text
|
|
|
|
@parser.banner
|
2020-11-11 16:12:38 -05:00
|
|
|
.gsub(/^ - (`[^`]+`)\s+/, "\n- \\1:\n <br>") # Format `cask` subcommands as Markdown list.
|
2018-09-08 22:21:04 +05:30
|
|
|
end
|
|
|
|
|
2018-02-04 22:09:35 +05:30
|
|
|
def comma_array(name, description: nil)
|
2020-03-16 15:02:43 +01:00
|
|
|
name = name.chomp "="
|
2018-02-04 22:09:35 +05:30
|
|
|
description = option_to_description(name) if description.nil?
|
2018-09-08 22:21:04 +05:30
|
|
|
process_option(name, description)
|
|
|
|
@parser.on(name, OptionParser::REQUIRED_ARGUMENT, Array, *wrap_option_desc(description)) do |list|
|
2019-04-17 19:43:06 +09:00
|
|
|
@args[option_to_name(name)] = list
|
2018-02-04 22:09:35 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-10-25 21:43:49 +05:30
|
|
|
def flag(*names, description: nil, required_for: nil, depends_on: nil)
|
2020-03-13 21:15:06 +00:00
|
|
|
required = if names.any? { |name| name.end_with? "=" }
|
|
|
|
OptionParser::REQUIRED_ARGUMENT
|
2018-02-04 22:09:35 +05:30
|
|
|
else
|
2020-03-13 21:15:06 +00:00
|
|
|
OptionParser::OPTIONAL_ARGUMENT
|
2018-02-04 22:09:35 +05:30
|
|
|
end
|
2018-10-25 21:43:49 +05:30
|
|
|
names.map! { |name| name.chomp "=" }
|
|
|
|
description = option_to_description(*names) if description.nil?
|
|
|
|
process_option(*names, description)
|
|
|
|
@parser.on(*names, *wrap_option_desc(description), required) do |option_value|
|
|
|
|
names.each do |name|
|
2019-04-17 19:43:06 +09:00
|
|
|
@args[option_to_name(name)] = 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|
|
|
|
|
set_constraints(name, required_for: required_for, depends_on: depends_on)
|
|
|
|
end
|
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)
|
2020-08-01 02:30:46 +02:00
|
|
|
option.sub(/\A--?(\[no-\])?/, "")
|
2018-06-01 14:19:00 +02:00
|
|
|
.tr("-", "_")
|
|
|
|
.delete("=")
|
|
|
|
end
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
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
|
|
|
|
|
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)
|
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
|
|
|
|
if ignore_invalid_options
|
|
|
|
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
|
|
|
|
|
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
|
|
|
|
|
2020-07-31 17:37:36 +02:00
|
|
|
# If we accept formula options, parse once allowing invalid options
|
|
|
|
# so we can get the remaining list containing formula names.
|
|
|
|
if @formula_options
|
|
|
|
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? "="
|
|
|
|
flag name, description: description
|
|
|
|
else
|
|
|
|
switch name, description: description
|
|
|
|
end
|
2020-10-03 02:45:32 +02:00
|
|
|
|
|
|
|
conflicts "--cask", name
|
2020-07-31 17:37:36 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-07-31 15:07:08 +02:00
|
|
|
remaining, non_options = parse_remaining(argv, ignore_invalid_options: ignore_invalid_options)
|
|
|
|
|
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
|
|
|
|
check_constraint_violations
|
|
|
|
check_named_args(named_args)
|
|
|
|
end
|
|
|
|
|
2020-05-05 12:50:41 +01:00
|
|
|
@args.freeze_named_args!(named_args)
|
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)
|
2020-05-05 12:50:41 +01:00
|
|
|
|
2019-12-11 00:23:51 +05:30
|
|
|
@args_parsed = true
|
2020-08-01 02:30:46 +02:00
|
|
|
|
|
|
|
if !ignore_invalid_options && @args.help?
|
|
|
|
puts generate_help_text
|
|
|
|
exit
|
|
|
|
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
|
|
|
|
2018-10-03 21:12:44 +05:30
|
|
|
def generate_help_text
|
2020-08-01 02:30:46 +02:00
|
|
|
Formatter.wrap(
|
|
|
|
@parser.to_s.gsub(/^ - (`[^`]+`\s+)/, " \\1"), # Remove `-` from `cask` subcommand listing.
|
|
|
|
COMMAND_DESC_WIDTH,
|
|
|
|
)
|
|
|
|
.sub(/^/, "#{Tty.bold}Usage: brew#{Tty.reset} ")
|
|
|
|
.gsub(/`(.*?)`/m, "#{Tty.bold}\\1#{Tty.reset}")
|
|
|
|
.gsub(%r{<([^\s]+?://[^\s]+?)>}) { |url| Formatter.url(url) }
|
|
|
|
.gsub(/<(.*?)>/m, "#{Tty.underline}\\1#{Tty.reset}")
|
|
|
|
.gsub(/\*(.*?)\*/m, "#{Tty.underline}\\1#{Tty.reset}")
|
2018-10-03 21:12:44 +05:30
|
|
|
end
|
|
|
|
|
2020-10-03 02:45:32 +02:00
|
|
|
def cask_options
|
|
|
|
self.class.global_cask_options.each do |method, *args, **options|
|
|
|
|
send(method, *args, **options)
|
|
|
|
conflicts "--formula", args.last
|
|
|
|
end
|
|
|
|
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
|
|
|
|
|
2019-12-13 16:50:54 -05:00
|
|
|
def max_named(count)
|
2020-03-04 17:27:25 +00:00
|
|
|
raise TypeError, "Unsupported type #{count.class.name} for max_named" unless count.is_a?(Integer)
|
|
|
|
|
2019-12-13 16:50:54 -05:00
|
|
|
@max_named_args = count
|
|
|
|
end
|
|
|
|
|
2020-03-04 17:27:25 +00:00
|
|
|
def min_named(count_or_type)
|
2020-07-13 22:48:53 +10:00
|
|
|
case count_or_type
|
|
|
|
when Integer
|
2020-03-04 17:27:25 +00:00
|
|
|
@min_named_args = count_or_type
|
|
|
|
@min_named_type = nil
|
2020-07-13 22:48:53 +10:00
|
|
|
when Symbol
|
2020-03-04 17:27:25 +00:00
|
|
|
@min_named_args = 1
|
|
|
|
@min_named_type = count_or_type
|
|
|
|
else
|
|
|
|
raise TypeError, "Unsupported type #{count_or_type.class.name} for min_named"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def named(count_or_type)
|
2020-07-13 22:48:53 +10:00
|
|
|
case count_or_type
|
|
|
|
when Integer
|
2020-03-04 17:27:25 +00:00
|
|
|
@max_named_args = @min_named_args = count_or_type
|
|
|
|
@min_named_type = nil
|
2020-07-13 22:48:53 +10:00
|
|
|
when Symbol
|
2020-03-04 17:27:25 +00:00
|
|
|
@max_named_args = @min_named_args = 1
|
|
|
|
@min_named_type = count_or_type
|
|
|
|
else
|
|
|
|
raise TypeError, "Unsupported type #{count_or_type.class.name} for named"
|
|
|
|
end
|
|
|
|
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
|
|
|
|
|
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
|
2020-08-01 02:30:46 +02:00
|
|
|
@args["#{option_to_name(name)}?"] = value
|
2018-03-25 11:04:18 +05:30
|
|
|
end
|
|
|
|
end
|
2018-03-29 03:20:14 +05:30
|
|
|
|
2019-02-15 00:31:13 -05:00
|
|
|
def disable_switch(*names)
|
|
|
|
names.each do |name|
|
2019-04-17 19:43:06 +09:00
|
|
|
@args.delete_field("#{option_to_name(name)}?")
|
2019-02-15 00:31:13 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-04-01 22:01:06 +05:30
|
|
|
def option_passed?(name)
|
2020-04-12 19:12:16 +05:30
|
|
|
@args[name.to_sym] || @args["#{name}?".to_sym]
|
2018-04-01 22:01:06 +05:30
|
|
|
end
|
|
|
|
|
2018-10-02 14:44:38 +05:30
|
|
|
def wrap_option_desc(desc)
|
2019-03-09 13:00:15 -05:00
|
|
|
Formatter.wrap(desc, OPTION_DESC_WIDTH).split("\n")
|
2018-10-02 14:44:38 +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
|
2019-02-19 13:11:32 +00:00
|
|
|
raise OptionConstraintError.new(primary, secondary, missing: true) if secondary_passed && !primary_passed
|
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
|
|
|
|
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
|
|
|
|
raise OptionConflictError, violations.map(&method(:name_to_option)) unless select_cli_arg
|
2019-02-19 13:12:52 +00:00
|
|
|
|
2019-02-15 01:16:07 -05:00
|
|
|
env_var_options.each(&method(:disable_switch))
|
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
|
2018-09-08 22:21:04 +05:30
|
|
|
|
2020-07-30 18:40:10 +02:00
|
|
|
def check_named_args(args)
|
2020-10-20 12:03:48 +02:00
|
|
|
exception = if @min_named_args && args.size < @min_named_args
|
|
|
|
case @min_named_type
|
|
|
|
when :cask
|
|
|
|
Cask::CaskUnspecifiedError
|
|
|
|
when :formula
|
|
|
|
FormulaUnspecifiedError
|
|
|
|
when :formula_or_cask
|
|
|
|
FormulaOrCaskUnspecifiedError
|
|
|
|
when :keg
|
|
|
|
KegUnspecifiedError
|
|
|
|
else
|
|
|
|
MinNamedArgumentsError.new(@min_named_args)
|
|
|
|
end
|
|
|
|
elsif @max_named_args && args.size > @max_named_args
|
|
|
|
MaxNamedArgumentsError.new(@max_named_args)
|
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
|
|
|
|
|
2018-09-08 22:21:04 +05:30
|
|
|
def process_option(*args)
|
|
|
|
option, = @parser.make_switch(args)
|
|
|
|
@processed_options << [option.short.first, option.long.first, option.arg, option.desc.first]
|
|
|
|
end
|
2020-05-10 15:10:36 +01:00
|
|
|
|
2020-07-31 17:41:20 +02:00
|
|
|
def split_non_options(argv)
|
2020-07-30 18:40:10 +02:00
|
|
|
if sep = argv.index("--")
|
|
|
|
[argv.take(sep), argv.drop(sep + 1)]
|
|
|
|
else
|
|
|
|
[argv, []]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
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
|
|
|
|
named_args.map do |arg|
|
|
|
|
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?("--") })
|
|
|
|
rescue FormulaUnavailableError
|
|
|
|
nil
|
|
|
|
end
|
2020-05-10 15:10:36 +01:00
|
|
|
end.compact.uniq(&:name)
|
|
|
|
end
|
2018-04-01 22:01:06 +05:30
|
|
|
end
|
|
|
|
|
2020-11-03 12:29:58 +00:00
|
|
|
class OptionConstraintError < UsageError
|
2018-04-01 22:01:06 +05:30
|
|
|
def initialize(arg1, arg2, missing: false)
|
2020-11-03 16:48:01 +00:00
|
|
|
arg1 = "--#{arg1.tr("_", "-")}"
|
|
|
|
arg2 = "--#{arg2.tr("_", "-")}"
|
|
|
|
|
2020-11-09 20:09:16 +11:00
|
|
|
message = if missing
|
2019-12-13 16:50:54 -05:00
|
|
|
"`#{arg2}` cannot be passed without `#{arg1}`."
|
2020-11-09 20:09:16 +11:00
|
|
|
else
|
|
|
|
"`#{arg1}` and `#{arg2}` should be passed together."
|
2018-04-01 22:01:06 +05:30
|
|
|
end
|
|
|
|
super message
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-11-03 12:29:58 +00:00
|
|
|
class OptionConflictError < UsageError
|
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 ")
|
2019-12-13 16:50:54 -05:00
|
|
|
super "Options #{args_list} are mutually exclusive."
|
2018-04-14 16:17:14 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-11-03 12:29:58 +00:00
|
|
|
class InvalidConstraintError < UsageError
|
2018-04-01 22:01:06 +05:30
|
|
|
def initialize(arg1, arg2)
|
2019-12-13 16:50:54 -05:00
|
|
|
super "`#{arg1}` and `#{arg2}` cannot be mutually exclusive and mutually dependent simultaneously."
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-03-04 17:27:25 +00:00
|
|
|
class MaxNamedArgumentsError < UsageError
|
2020-10-20 12:03:48 +02:00
|
|
|
extend T::Sig
|
|
|
|
|
|
|
|
sig { params(maximum: Integer).void }
|
2019-12-13 16:50:54 -05:00
|
|
|
def initialize(maximum)
|
2020-09-28 01:24:55 +02:00
|
|
|
super case maximum
|
2019-12-13 16:50:54 -05:00
|
|
|
when 0
|
2020-09-28 01:24:55 +02:00
|
|
|
"This command does not take named arguments."
|
2020-03-04 17:27:25 +00:00
|
|
|
else
|
2020-09-28 01:24:55 +02:00
|
|
|
"This command does not take more than #{maximum} named #{"argument".pluralize(maximum)}"
|
2020-03-04 17:27:25 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
class MinNamedArgumentsError < UsageError
|
2020-10-20 12:03:48 +02:00
|
|
|
extend T::Sig
|
|
|
|
|
|
|
|
sig { params(minimum: Integer).void }
|
2020-03-04 17:27:25 +00:00
|
|
|
def initialize(minimum)
|
2020-09-28 01:24:55 +02:00
|
|
|
super "This command requires at least #{minimum} named #{"argument".pluralize(minimum)}."
|
2018-04-01 22:01:06 +05:30
|
|
|
end
|
2018-02-04 22:09:35 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|