diff --git a/Library/Homebrew/cli/parser.rb b/Library/Homebrew/cli/parser.rb index b9dcb1a84f..30d0938843 100644 --- a/Library/Homebrew/cli/parser.rb +++ b/Library/Homebrew/cli/parser.rb @@ -248,12 +248,14 @@ module Homebrew @conflicts << options.map { |option| option_to_name(option) } end - def option_to_name(option) + def self.option_to_name(option) option.sub(/\A--?(\[no-\])?/, "") .tr("-", "_") .delete("=") end + def option_to_name(option) = self.class.option_to_name(option) + def name_to_option(name) if name.length == 1 "-#{name}" diff --git a/Library/Homebrew/sorbet/rbi/dsl/homebrew/cmd/list.rbi b/Library/Homebrew/sorbet/rbi/dsl/homebrew/cmd/list.rbi index d9fc1c041d..a5b8ec328e 100644 --- a/Library/Homebrew/sorbet/rbi/dsl/homebrew/cmd/list.rbi +++ b/Library/Homebrew/sorbet/rbi/dsl/homebrew/cmd/list.rbi @@ -11,12 +11,6 @@ class Homebrew::CLI::Args sig { returns(T::Boolean) } def casks?; end - sig { returns(T::Boolean) } - def d?; end - - sig { returns(T::Boolean) } - def debug?; end - sig { returns(T::Boolean) } def formula?; end @@ -26,12 +20,6 @@ class Homebrew::CLI::Args sig { returns(T::Boolean) } def full_name?; end - sig { returns(T::Boolean) } - def h?; end - - sig { returns(T::Boolean) } - def help?; end - sig { returns(T::Boolean) } def l?; end @@ -41,24 +29,12 @@ class Homebrew::CLI::Args sig { returns(T::Boolean) } def pinned?; end - sig { returns(T::Boolean) } - def q?; end - - sig { returns(T::Boolean) } - def quiet?; end - sig { returns(T::Boolean) } def r?; end sig { returns(T::Boolean) } def t?; end - sig { returns(T::Boolean) } - def v?; end - - sig { returns(T::Boolean) } - def verbose?; end - sig { returns(T::Boolean) } def versions?; end end diff --git a/Library/Homebrew/sorbet/rbi/dsl/homebrew/dev_cmd/prof.rbi b/Library/Homebrew/sorbet/rbi/dsl/homebrew/dev_cmd/prof.rbi index 9083195ef5..27f3a84c69 100644 --- a/Library/Homebrew/sorbet/rbi/dsl/homebrew/dev_cmd/prof.rbi +++ b/Library/Homebrew/sorbet/rbi/dsl/homebrew/dev_cmd/prof.rbi @@ -5,30 +5,6 @@ # Please instead update this file by running `bin/tapioca dsl Homebrew::DevCmd::Prof`. 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) } def stackprof?; end - - sig { returns(T::Boolean) } - def v?; end - - sig { returns(T::Boolean) } - def verbose?; end end diff --git a/Library/Homebrew/sorbet/tapioca/compilers/args.rb b/Library/Homebrew/sorbet/tapioca/compilers/args.rb index df47ba41ee..8679b4415f 100644 --- a/Library/Homebrew/sorbet/tapioca/compilers/args.rb +++ b/Library/Homebrew/sorbet/tapioca/compilers/args.rb @@ -2,10 +2,17 @@ # frozen_string_literal: true require_relative "../../../global" +require "cli/parser" module Tapioca module Compilers 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 # These are cmd/dev-cmd methods that end in `_args` but are not parsers 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. # rubocop:disable Style/MutableConstant 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 sig { override.returns(T::Enumerable[Parsable]) } @@ -37,12 +44,12 @@ module Tapioca next if NON_PARSER_ARGS_METHODS.include?(args_method_name) parser = Homebrew.method(args_method_name).call - create_args_methods(klass, parser) + create_args_methods(klass, parser, include_global: true) end end else 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 @@ -72,15 +79,17 @@ module Tapioca private - sig { params(klass: RBI::Scope, parser: Homebrew::CLI::Parser).void } - def create_args_methods(klass, parser) + sig { params(klass: RBI::Scope, parser: Homebrew::CLI::Parser, include_global: T::Boolean).void } + def create_args_methods(klass, parser, include_global: false) comma_array_methods = comma_arrays(parser) 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) - 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) - klass.create_method(method_name, return_type:) + klass.create_method(method_name_str, return_type:) end end end