Use method result table

foo
This commit is contained in:
Douglas Eichelberger 2024-12-06 14:14:02 -08:00
parent 3ea76b8498
commit fef1211c19
4 changed files with 32 additions and 24 deletions

View File

@ -14,6 +14,9 @@ module Homebrew
sig { returns(T::Array[String]) }
attr_reader :options_only, :flags_only, :remaining
sig { returns(T::Hash[Symbol, T.untyped]) }
attr_accessor :table
sig { void }
def initialize
require "cli/named_args"
@ -55,15 +58,9 @@ module Homebrew
sig { returns(T::Boolean) }
def build_from_source? = false
sig { returns(T::Boolean) }
def cask? = false
sig { returns(T::Boolean) }
def force_bottle? = false
# Defined in extend/os:
# def formula; end
sig { returns(T::Boolean) }
def HEAD? = false
@ -128,9 +125,11 @@ module Homebrew
sig { returns(T.nilable(Symbol)) }
def only_formula_or_cask
if formula? && !cask?
return if !respond_to?(:formula?) && !respond_to?(:cask?)
if T.unsafe(self).formula? && !T.unsafe(self).cask?
:formula
elsif cask? && !formula?
elsif T.unsafe(self).cask? && !T.unsafe(self).formula?
:cask
end
end
@ -191,7 +190,8 @@ module Homebrew
elsif @table[flag].instance_of? Array
"#{option}=#{@table[flag].join(",")}"
end
end.freeze
end
@cli_args.freeze
end
end
end

View File

@ -252,7 +252,7 @@ module Homebrew
description = option_description(description, name, hidden:)
process_option(name, description, type: :comma_array, hidden:)
@parser.on(name, OptionParser::REQUIRED_ARGUMENT, Array, *wrap_option_desc(description)) do |list|
@args.define_singleton_method(option_to_name(name)) { list }
set_args_method(option_to_name(name).to_sym, list)
end
end
@ -277,7 +277,7 @@ module Homebrew
# This odisabled should stick around indefinitely.
odisabled "the `#{names.first}` flag", replacement unless replacement.nil?
names.each do |name|
@args.define_singleton_method(option_to_name(name)) { option_value }
set_args_method(option_to_name(name).to_sym, option_value)
end
end
@ -286,6 +286,17 @@ module Homebrew
end
end
sig { params(name: Symbol, value: T.untyped).void }
def set_args_method(name, value)
@args.table[name] = value
return if @args.respond_to?(name)
@args.define_singleton_method(name) do
T.bind(self, Args)
table.fetch(name)
end
end
sig { params(options: String).returns(T::Array[T::Array[String]]) }
def conflicts(*options)
@conflicts << options.map { |option| option_to_name(option) }
@ -552,7 +563,7 @@ module Homebrew
def set_switch(*names, value:, from:)
names.each do |name|
@switch_sources[option_to_name(name)] = from
@args.define_singleton_method(:"#{option_to_name(name)}?") { value }
set_args_method(:"#{option_to_name(name)}?", value)
end
end
@ -564,7 +575,7 @@ module Homebrew
else
false
end
@args.define_singleton_method(:"#{option_to_name(name)}?") { result }
set_args_method(:"#{option_to_name(name)}?", result)
end
end
@ -673,7 +684,7 @@ module Homebrew
disable_switch(*args)
else
args.each do |name|
@args.define_singleton_method(option_to_name(name)) { nil }
set_args_method(option_to_name(name).to_sym, nil)
end
end

View File

@ -9,9 +9,15 @@ module OS
requires_ancestor { Homebrew::CLI::Parser }
sig { void }
def set_default_options
args.table[:formula?] = true if args.respond_to?(:formula?)
end
sig { void }
def validate_options
return unless args.cask?
return unless args.respond_to?(:cask?)
return unless T.unsafe(self).args.cask?
# NOTE: We don't raise an error here because we don't want
# to print the help page or a stack trace.

View File

@ -2,12 +2,3 @@
# frozen_string_literal: true
require "extend/os/linux/cli/parser" if OS.linux?
module Homebrew
module CLI
class Args
sig { returns(T::Boolean) }
def formula? = OS.linux?
end
end
end