2018-02-04 22:09:35 +05:30
|
|
|
require "optparse"
|
|
|
|
require "ostruct"
|
2018-04-14 16:17:14 +05:30
|
|
|
require "set"
|
2018-02-04 22:09:35 +05:30
|
|
|
|
|
|
|
module Homebrew
|
|
|
|
module CLI
|
|
|
|
class Parser
|
2018-09-08 22:21:04 +05:30
|
|
|
attr_reader :processed_options
|
|
|
|
|
2018-06-01 14:19:00 +02:00
|
|
|
def self.parse(args = ARGV, &block)
|
|
|
|
new(&block).parse(args)
|
2018-03-25 17:48:22 +05:30
|
|
|
end
|
|
|
|
|
2018-10-02 14:44:38 +05:30
|
|
|
def self.global_options
|
|
|
|
{
|
2018-11-02 17:18:07 +00:00
|
|
|
quiet: [["-q", "--quiet"], :quiet, "Suppress any warnings."],
|
2018-10-02 14:44:38 +05:30
|
|
|
verbose: [["-v", "--verbose"], :verbose, "Make some output more verbose."],
|
2018-11-02 17:18:07 +00:00
|
|
|
debug: [["-d", "--debug"], :debug, "Display any debugging information."],
|
|
|
|
force: [["-f", "--force"], :force, "Override warnings and enable potentially unsafe operations."],
|
2018-10-02 14:44:38 +05:30
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2018-02-04 22:09:35 +05:30
|
|
|
def initialize(&block)
|
|
|
|
@parser = OptionParser.new
|
2018-05-05 18:40:01 +05:30
|
|
|
Homebrew.args = OpenStruct.new
|
2018-04-08 16:40:02 -07:00
|
|
|
# undefine tap to allow --tap argument
|
2018-05-05 18:40:01 +05:30
|
|
|
Homebrew.args.instance_eval { undef tap }
|
2018-04-14 16:17:14 +05:30
|
|
|
@constraints = []
|
2018-04-01 22:01:06 +05:30
|
|
|
@conflicts = []
|
2018-09-08 22:21:04 +05:30
|
|
|
@processed_options = []
|
2018-10-06 22:52:47 -04:00
|
|
|
@desc_line_length = 43
|
2018-02-04 22:09:35 +05:30
|
|
|
instance_eval(&block)
|
2018-06-28 09:28:19 +05:30
|
|
|
post_initialize
|
|
|
|
end
|
|
|
|
|
|
|
|
def post_initialize
|
2018-10-06 22:52:47 -04:00
|
|
|
@parser.on_tail("-h", "--help", "Show this message.") do
|
2018-10-03 21:12:44 +05:30
|
|
|
puts generate_help_text
|
|
|
|
exit 0
|
2018-06-28 09:28:19 +05:30
|
|
|
end
|
2018-02-04 22:09:35 +05:30
|
|
|
end
|
|
|
|
|
2018-05-25 04:05:45 +05:30
|
|
|
def switch(*names, description: nil, env: nil, required_for: nil, depends_on: nil)
|
2018-04-01 16:47:30 +05:30
|
|
|
global_switch = names.first.is_a?(Symbol)
|
2018-10-24 17:10:13 +05:30
|
|
|
names, env, default_description = common_switch(*names) if global_switch
|
|
|
|
if description.nil? && global_switch
|
|
|
|
description = default_description
|
|
|
|
elsif description.nil?
|
|
|
|
description = option_to_description(*names)
|
|
|
|
end
|
2018-09-08 22:21:04 +05:30
|
|
|
process_option(*names, description)
|
|
|
|
@parser.on(*names, *wrap_option_desc(description)) do
|
2018-05-05 18:40:01 +05:30
|
|
|
enable_switch(*names)
|
2018-02-04 22:09:35 +05:30
|
|
|
end
|
2018-05-25 04:05:45 +05:30
|
|
|
|
|
|
|
names.each do |name|
|
|
|
|
set_constraints(name, required_for: required_for, depends_on: depends_on)
|
|
|
|
end
|
2018-05-05 18:40:01 +05:30
|
|
|
|
|
|
|
enable_switch(*names) if !env.nil? && !ENV["HOMEBREW_#{env.to_s.upcase}"].nil?
|
2018-02-04 22:09:35 +05:30
|
|
|
end
|
2019-01-18 22:03:07 +05:30
|
|
|
alias switch_option switch
|
2018-02-04 22:09:35 +05:30
|
|
|
|
2018-09-08 22:21:04 +05:30
|
|
|
def usage_banner(text)
|
2018-09-22 09:31:30 +05:30
|
|
|
@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
|
|
|
|
end
|
|
|
|
|
2018-02-04 22:09:35 +05:30
|
|
|
def comma_array(name, description: nil)
|
|
|
|
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|
|
2018-05-05 18:40:01 +05:30
|
|
|
Homebrew.args[option_to_name(name)] = list
|
2018-02-04 22:09:35 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-10-25 21:43:49 +05:30
|
|
|
def flag(*names, description: nil, required_for: nil, depends_on: nil)
|
|
|
|
if names.any? { |name| name.end_with? "=" }
|
2018-04-14 19:04:24 +05:30
|
|
|
required = OptionParser::REQUIRED_ARGUMENT
|
2018-02-04 22:09:35 +05:30
|
|
|
else
|
2018-04-14 19:04:24 +05:30
|
|
|
required = OptionParser::OPTIONAL_ARGUMENT
|
2018-02-04 22:09:35 +05:30
|
|
|
end
|
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|
|
|
|
|
Homebrew.args[option_to_name(name)] = option_value
|
|
|
|
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)
|
|
|
|
option.sub(/\A--?/, "")
|
|
|
|
.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
|
|
|
|
|
2018-06-28 09:28:19 +05:30
|
|
|
def summary
|
|
|
|
@parser.to_s
|
|
|
|
end
|
|
|
|
|
2018-07-30 18:25:38 +05:30
|
|
|
def parse(cmdline_args = ARGV)
|
2019-01-12 18:52:07 +05:30
|
|
|
begin
|
|
|
|
remaining_args = @parser.parse(cmdline_args)
|
|
|
|
rescue OptionParser::InvalidOption => e
|
2019-01-12 19:16:44 +00:00
|
|
|
$stderr.puts generate_help_text
|
2019-01-12 18:52:07 +05:30
|
|
|
raise e
|
|
|
|
end
|
2018-04-01 22:01:06 +05:30
|
|
|
check_constraint_violations
|
2018-06-01 14:19:00 +02:00
|
|
|
Homebrew.args[:remaining] = remaining_args
|
2018-10-29 02:27:00 +05:30
|
|
|
Homebrew.args.freeze
|
2018-07-30 18:25:38 +05:30
|
|
|
@parser
|
2018-02-04 22:09:35 +05:30
|
|
|
end
|
2018-03-25 11:04:18 +05:30
|
|
|
|
2018-10-02 14:44:38 +05:30
|
|
|
def global_option?(name)
|
2018-10-02 19:54:22 +05:30
|
|
|
Homebrew::CLI::Parser.global_options.key?(name.to_sym)
|
2018-10-02 14:44:38 +05:30
|
|
|
end
|
|
|
|
|
2018-10-03 21:12:44 +05:30
|
|
|
def generate_help_text
|
|
|
|
@parser.to_s.sub(/^/, "#{Tty.bold}Usage: brew#{Tty.reset} ")
|
2018-10-06 22:52:47 -04:00
|
|
|
.gsub(/`(.*?)`/m, "#{Tty.bold}\\1#{Tty.reset}")
|
2018-10-03 21:12:44 +05:30
|
|
|
.gsub(%r{<([^\s]+?://[^\s]+?)>}) { |url| Formatter.url(url) }
|
2018-10-06 22:52:47 -04:00
|
|
|
.gsub(/<(.*?)>/m, "#{Tty.underline}\\1#{Tty.reset}")
|
|
|
|
.gsub(/\*(.*?)\*/m, "#{Tty.underline}\\1#{Tty.reset}")
|
2018-10-03 21:12:44 +05:30
|
|
|
end
|
|
|
|
|
2019-01-29 19:25:13 +00:00
|
|
|
def formula_options
|
|
|
|
ARGV.formulae.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
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-03-25 11:04:18 +05:30
|
|
|
private
|
|
|
|
|
2018-05-05 18:40:01 +05:30
|
|
|
def enable_switch(*names)
|
2018-03-25 11:04:18 +05:30
|
|
|
names.each do |name|
|
2018-05-05 18:40:01 +05:30
|
|
|
Homebrew.args["#{option_to_name(name)}?"] = true
|
2018-03-25 11:04:18 +05:30
|
|
|
end
|
|
|
|
end
|
2018-03-29 03:20:14 +05:30
|
|
|
|
2018-04-01 16:47:30 +05:30
|
|
|
# These are common/global switches accessible throughout Homebrew
|
2018-03-29 03:20:14 +05:30
|
|
|
def common_switch(name)
|
2018-10-02 14:44:38 +05:30
|
|
|
Homebrew::CLI::Parser.global_options.fetch(name, name)
|
2018-03-29 03:20:14 +05:30
|
|
|
end
|
2018-04-01 22:01:06 +05:30
|
|
|
|
|
|
|
def option_passed?(name)
|
2018-05-05 18:40:01 +05:30
|
|
|
Homebrew.args.respond_to?(name) || Homebrew.args.respond_to?("#{name}?")
|
2018-04-01 22:01:06 +05:30
|
|
|
end
|
|
|
|
|
2018-10-02 14:44:38 +05:30
|
|
|
def wrap_option_desc(desc)
|
|
|
|
Formatter.wrap(desc, @desc_line_length).split("\n")
|
|
|
|
end
|
|
|
|
|
2018-04-14 16:17:14 +05:30
|
|
|
def set_constraints(name, depends_on:, required_for:)
|
|
|
|
secondary = option_to_name(name)
|
|
|
|
unless required_for.nil?
|
|
|
|
primary = option_to_name(required_for)
|
|
|
|
@constraints << [primary, secondary, :mandatory]
|
|
|
|
end
|
|
|
|
|
|
|
|
return if depends_on.nil?
|
2018-09-17 02:45:00 +02:00
|
|
|
|
2018-04-14 16:17:14 +05:30
|
|
|
primary = option_to_name(depends_on)
|
|
|
|
@constraints << [primary, secondary, :optional]
|
|
|
|
end
|
|
|
|
|
|
|
|
def check_constraints
|
|
|
|
@constraints.each do |primary, secondary, constraint_type|
|
2018-04-01 22:01:06 +05:30
|
|
|
primary_passed = option_passed?(primary)
|
|
|
|
secondary_passed = option_passed?(secondary)
|
2018-04-14 16:17:14 +05:30
|
|
|
if :mandatory.equal?(constraint_type) && primary_passed && !secondary_passed
|
|
|
|
raise OptionConstraintError.new(primary, secondary)
|
|
|
|
end
|
|
|
|
if secondary_passed && !primary_passed
|
|
|
|
raise OptionConstraintError.new(primary, secondary, missing: true)
|
|
|
|
end
|
2018-04-01 22:01:06 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def check_conflicts
|
2018-04-14 16:17:14 +05:30
|
|
|
@conflicts.each do |mutually_exclusive_options_group|
|
|
|
|
violations = mutually_exclusive_options_group.select do |option|
|
|
|
|
option_passed? option
|
|
|
|
end
|
2018-06-01 14:19:00 +02:00
|
|
|
|
|
|
|
next if violations.count < 2
|
2018-09-17 02:45:00 +02:00
|
|
|
|
2018-06-01 14:19:00 +02:00
|
|
|
raise OptionConflictError, violations.map(&method(:name_to_option))
|
2018-04-14 16:17:14 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def check_invalid_constraints
|
|
|
|
@conflicts.each do |mutually_exclusive_options_group|
|
|
|
|
@constraints.each do |p, s|
|
|
|
|
next unless Set[p, s].subset?(Set[*mutually_exclusive_options_group])
|
2018-09-17 02:45:00 +02:00
|
|
|
|
2018-04-14 16:17:14 +05:30
|
|
|
raise InvalidConstraintError.new(p, s)
|
|
|
|
end
|
2018-04-01 22:01:06 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def check_constraint_violations
|
2018-04-14 16:17:14 +05:30
|
|
|
check_invalid_constraints
|
2018-04-01 22:01:06 +05:30
|
|
|
check_conflicts
|
2018-04-14 16:17:14 +05:30
|
|
|
check_constraints
|
2018-04-01 22:01:06 +05:30
|
|
|
end
|
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
|
2018-04-01 22:01:06 +05:30
|
|
|
end
|
|
|
|
|
2018-04-14 16:17:14 +05:30
|
|
|
class OptionConstraintError < RuntimeError
|
2018-04-01 22:01:06 +05:30
|
|
|
def initialize(arg1, arg2, missing: false)
|
|
|
|
if !missing
|
|
|
|
message = <<~EOS
|
|
|
|
`#{arg1}` and `#{arg2}` should be passed together
|
|
|
|
EOS
|
|
|
|
else
|
|
|
|
message = <<~EOS
|
|
|
|
`#{arg2}` cannot be passed without `#{arg1}`
|
|
|
|
EOS
|
|
|
|
end
|
|
|
|
super message
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
class OptionConflictError < RuntimeError
|
2018-04-14 16:17:14 +05:30
|
|
|
def initialize(args)
|
2018-06-01 14:19:00 +02:00
|
|
|
args_list = args.map(&Formatter.public_method(:option))
|
|
|
|
.join(" and ")
|
2018-04-14 16:17:14 +05:30
|
|
|
super <<~EOS
|
2018-06-01 14:19:00 +02:00
|
|
|
Options #{args_list} are mutually exclusive.
|
2018-04-14 16:17:14 +05:30
|
|
|
EOS
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
class InvalidConstraintError < RuntimeError
|
2018-04-01 22:01:06 +05:30
|
|
|
def initialize(arg1, arg2)
|
|
|
|
super <<~EOS
|
2018-04-14 16:17:14 +05:30
|
|
|
`#{arg1}` and `#{arg2}` cannot be mutually exclusive and mutually dependent simultaneously
|
2018-04-01 22:01:06 +05:30
|
|
|
EOS
|
|
|
|
end
|
2018-02-04 22:09:35 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|