Avoid duplicating global options

This commit is contained in:
Douglas Eichelberger 2024-03-15 15:50:07 -07:00
parent dfa01a5a84
commit 625206b0bd
4 changed files with 19 additions and 56 deletions

View File

@ -248,12 +248,14 @@ module Homebrew
@conflicts << options.map { |option| option_to_name(option) } @conflicts << options.map { |option| option_to_name(option) }
end end
def option_to_name(option) def self.option_to_name(option)
option.sub(/\A--?(\[no-\])?/, "") option.sub(/\A--?(\[no-\])?/, "")
.tr("-", "_") .tr("-", "_")
.delete("=") .delete("=")
end end
def option_to_name(option) = self.class.option_to_name(option)
def name_to_option(name) def name_to_option(name)
if name.length == 1 if name.length == 1
"-#{name}" "-#{name}"

View File

@ -11,12 +11,6 @@ class Homebrew::CLI::Args
sig { returns(T::Boolean) } sig { returns(T::Boolean) }
def casks?; end def casks?; end
sig { returns(T::Boolean) }
def d?; end
sig { returns(T::Boolean) }
def debug?; end
sig { returns(T::Boolean) } sig { returns(T::Boolean) }
def formula?; end def formula?; end
@ -26,12 +20,6 @@ class Homebrew::CLI::Args
sig { returns(T::Boolean) } sig { returns(T::Boolean) }
def full_name?; end def full_name?; end
sig { returns(T::Boolean) }
def h?; end
sig { returns(T::Boolean) }
def help?; end
sig { returns(T::Boolean) } sig { returns(T::Boolean) }
def l?; end def l?; end
@ -41,24 +29,12 @@ class Homebrew::CLI::Args
sig { returns(T::Boolean) } sig { returns(T::Boolean) }
def pinned?; end def pinned?; end
sig { returns(T::Boolean) }
def q?; end
sig { returns(T::Boolean) }
def quiet?; end
sig { returns(T::Boolean) } sig { returns(T::Boolean) }
def r?; end def r?; end
sig { returns(T::Boolean) } sig { returns(T::Boolean) }
def t?; end def t?; end
sig { returns(T::Boolean) }
def v?; end
sig { returns(T::Boolean) }
def verbose?; end
sig { returns(T::Boolean) } sig { returns(T::Boolean) }
def versions?; end def versions?; end
end end

View File

@ -5,30 +5,6 @@
# Please instead update this file by running `bin/tapioca dsl Homebrew::DevCmd::Prof`. # Please instead update this file by running `bin/tapioca dsl Homebrew::DevCmd::Prof`.
class Homebrew::CLI::Args class Homebrew::CLI::Args
sig { returns(T::Boolean) }
def d?; end
sig { returns(T::Boolean) }
def debug?; end
sig { returns(T::Boolean) }
def h?; end
sig { returns(T::Boolean) }
def help?; end
sig { returns(T::Boolean) }
def q?; end
sig { returns(T::Boolean) }
def quiet?; end
sig { returns(T::Boolean) } sig { returns(T::Boolean) }
def stackprof?; end def stackprof?; end
sig { returns(T::Boolean) }
def v?; end
sig { returns(T::Boolean) }
def verbose?; end
end end

View File

@ -2,10 +2,17 @@
# frozen_string_literal: true # frozen_string_literal: true
require_relative "../../../global" require_relative "../../../global"
require "cli/parser"
module Tapioca module Tapioca
module Compilers module Compilers
class Args < Tapioca::Dsl::Compiler class Args < Tapioca::Dsl::Compiler
GLOBAL_OPTIONS = T.let(
Homebrew::CLI::Parser.global_options.map { _1.slice(0, 2) }.flatten
.map { "#{Homebrew::CLI::Parser.option_to_name(_1)}?" }.freeze,
T::Array[String],
)
# This is ugly, but we're moving to a new interface that will use a consistent DSL # This is ugly, but we're moving to a new interface that will use a consistent DSL
# These are cmd/dev-cmd methods that end in `_args` but are not parsers # These are cmd/dev-cmd methods that end in `_args` but are not parsers
NON_PARSER_ARGS_METHODS = T.let([ NON_PARSER_ARGS_METHODS = T.let([
@ -17,7 +24,7 @@ module Tapioca
# FIXME: Enable cop again when https://github.com/sorbet/sorbet/issues/3532 is fixed. # FIXME: Enable cop again when https://github.com/sorbet/sorbet/issues/3532 is fixed.
# rubocop:disable Style/MutableConstant # rubocop:disable Style/MutableConstant
Parsable = T.type_alias { T.any(T.class_of(Homebrew::CLI::Args), T.class_of(Homebrew::AbstractCommand)) } Parsable = T.type_alias { T.any(T.class_of(Homebrew::CLI::Args), T.class_of(Homebrew::AbstractCommand)) }
ConstantType = type_member { { fixed: T.class_of(Homebrew::CLI::Args) } } ConstantType = type_member { { fixed: Parsable } }
# rubocop:enable Style/MutableConstant # rubocop:enable Style/MutableConstant
sig { override.returns(T::Enumerable[Parsable]) } sig { override.returns(T::Enumerable[Parsable]) }
@ -37,12 +44,12 @@ module Tapioca
next if NON_PARSER_ARGS_METHODS.include?(args_method_name) next if NON_PARSER_ARGS_METHODS.include?(args_method_name)
parser = Homebrew.method(args_method_name).call parser = Homebrew.method(args_method_name).call
create_args_methods(klass, parser) create_args_methods(klass, parser, include_global: true)
end end
end end
else else
root.create_path(Homebrew::CLI::Args) do |klass| root.create_path(Homebrew::CLI::Args) do |klass|
create_args_methods(klass, constant.parser) create_args_methods(klass, T.must(T.cast(constant, T.class_of(Homebrew::AbstractCommand)).parser))
end end
end end
end end
@ -72,15 +79,17 @@ module Tapioca
private private
sig { params(klass: RBI::Scope, parser: Homebrew::CLI::Parser).void } sig { params(klass: RBI::Scope, parser: Homebrew::CLI::Parser, include_global: T::Boolean).void }
def create_args_methods(klass, parser) def create_args_methods(klass, parser, include_global: false)
comma_array_methods = comma_arrays(parser) comma_array_methods = comma_arrays(parser)
args_table(parser).each do |method_name, value| args_table(parser).each do |method_name, value|
method_name_str = method_name.to_s
next if GLOBAL_OPTIONS.include?(method_name_str) && !include_global
# some args are used in multiple commands (this is ok as long as they have the same type) # some args are used in multiple commands (this is ok as long as they have the same type)
next if klass.nodes.any? { T.cast(_1, RBI::Method).name == method_name } next if klass.nodes.any? { T.cast(_1, RBI::Method).name == method_name_str }
return_type = get_return_type(method_name, value, comma_array_methods) return_type = get_return_type(method_name, value, comma_array_methods)
klass.create_method(method_name, return_type:) klass.create_method(method_name_str, return_type:)
end end
end end
end end