Strict type Homebrew::CLI::Args
This commit is contained in:
parent
f9b81af2ea
commit
3943ff1406
@ -1,4 +1,4 @@
|
|||||||
# typed: true # rubocop:disable Sorbet/StrictSigil
|
# typed: strict
|
||||||
# frozen_string_literal: true
|
# frozen_string_literal: true
|
||||||
|
|
||||||
require "ostruct"
|
require "ostruct"
|
||||||
@ -6,6 +6,12 @@ require "ostruct"
|
|||||||
module Homebrew
|
module Homebrew
|
||||||
module CLI
|
module CLI
|
||||||
class Args < OpenStruct
|
class Args < OpenStruct
|
||||||
|
# FIXME: Enable cop again when https://github.com/sorbet/sorbet/issues/3532 is fixed.
|
||||||
|
# rubocop:disable Style/MutableConstant
|
||||||
|
OptionsType = T.type_alias { T::Array[[String, T.nilable(String), T.nilable(String), String, T::Boolean]] }
|
||||||
|
# rubocop:enable Style/MutableConstant
|
||||||
|
|
||||||
|
sig { returns(T::Array[String]) }
|
||||||
attr_reader :options_only, :flags_only
|
attr_reader :options_only, :flags_only
|
||||||
|
|
||||||
# undefine tap to allow --tap argument
|
# undefine tap to allow --tap argument
|
||||||
@ -17,10 +23,12 @@ module Homebrew
|
|||||||
|
|
||||||
super
|
super
|
||||||
|
|
||||||
@processed_options = []
|
@cli_args = T.let(nil, T.nilable(T::Array[String]))
|
||||||
@options_only = []
|
@processed_options = T.let([], OptionsType)
|
||||||
@flags_only = []
|
@options_only = T.let([], T::Array[String])
|
||||||
@cask_options = false
|
@flags_only = T.let([], T::Array[String])
|
||||||
|
@cask_options = T.let(false, T::Boolean)
|
||||||
|
@table = T.let({}, T::Hash[Symbol, T.untyped])
|
||||||
|
|
||||||
# Can set these because they will be overwritten by freeze_named_args!
|
# Can set these because they will be overwritten by freeze_named_args!
|
||||||
# (whereas other values below will only be overwritten if passed).
|
# (whereas other values below will only be overwritten if passed).
|
||||||
@ -28,10 +36,12 @@ module Homebrew
|
|||||||
self[:remaining] = []
|
self[:remaining] = []
|
||||||
end
|
end
|
||||||
|
|
||||||
|
sig { params(remaining_args: T::Array[T.any(T::Array[String], String)]).void }
|
||||||
def freeze_remaining_args!(remaining_args)
|
def freeze_remaining_args!(remaining_args)
|
||||||
self[:remaining] = remaining_args.freeze
|
self[:remaining] = remaining_args.freeze
|
||||||
end
|
end
|
||||||
|
|
||||||
|
sig { params(named_args: T::Array[String], cask_options: T::Boolean, without_api: T::Boolean).void }
|
||||||
def freeze_named_args!(named_args, cask_options:, without_api:)
|
def freeze_named_args!(named_args, cask_options:, without_api:)
|
||||||
options = {}
|
options = {}
|
||||||
options[:force_bottle] = true if self[:force_bottle?]
|
options[:force_bottle] = true if self[:force_bottle?]
|
||||||
@ -46,6 +56,7 @@ module Homebrew
|
|||||||
)
|
)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
sig { params(processed_options: OptionsType).void }
|
||||||
def freeze_processed_options!(processed_options)
|
def freeze_processed_options!(processed_options)
|
||||||
# Reset cache values reliant on processed_options
|
# Reset cache values reliant on processed_options
|
||||||
@cli_args = nil
|
@cli_args = nil
|
||||||
@ -53,8 +64,8 @@ module Homebrew
|
|||||||
@processed_options += processed_options
|
@processed_options += processed_options
|
||||||
@processed_options.freeze
|
@processed_options.freeze
|
||||||
|
|
||||||
@options_only = cli_args.select { |a| a.start_with?("-") }.freeze
|
@options_only = cli_args.select { _1.start_with?("-") }.freeze
|
||||||
@flags_only = cli_args.select { |a| a.start_with?("--") }.freeze
|
@flags_only = cli_args.select { _1.start_with?("--") }.freeze
|
||||||
end
|
end
|
||||||
|
|
||||||
sig { returns(NamedArgs) }
|
sig { returns(NamedArgs) }
|
||||||
@ -63,10 +74,10 @@ module Homebrew
|
|||||||
self[:named]
|
self[:named]
|
||||||
end
|
end
|
||||||
|
|
||||||
def no_named?
|
sig { returns(T::Boolean) }
|
||||||
named.blank?
|
def no_named? = named.blank?
|
||||||
end
|
|
||||||
|
|
||||||
|
sig { returns(T::Array[String]) }
|
||||||
def build_from_source_formulae
|
def build_from_source_formulae
|
||||||
if build_from_source? || self[:HEAD?] || self[:build_bottle?]
|
if build_from_source? || self[:HEAD?] || self[:build_bottle?]
|
||||||
named.to_formulae.map(&:full_name)
|
named.to_formulae.map(&:full_name)
|
||||||
@ -75,6 +86,7 @@ module Homebrew
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
sig { returns(T::Array[String]) }
|
||||||
def include_test_formulae
|
def include_test_formulae
|
||||||
if include_test?
|
if include_test?
|
||||||
named.to_formulae.map(&:full_name)
|
named.to_formulae.map(&:full_name)
|
||||||
@ -83,6 +95,7 @@ module Homebrew
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
sig { params(name: String).returns(T.nilable(String)) }
|
||||||
def value(name)
|
def value(name)
|
||||||
arg_prefix = "--#{name}="
|
arg_prefix = "--#{name}="
|
||||||
flag_with_value = flags_only.find { |arg| arg.start_with?(arg_prefix) }
|
flag_with_value = flags_only.find { |arg| arg.start_with?(arg_prefix) }
|
||||||
@ -96,6 +109,7 @@ module Homebrew
|
|||||||
Context::ContextStruct.new(debug: debug?, quiet: quiet?, verbose: verbose?)
|
Context::ContextStruct.new(debug: debug?, quiet: quiet?, verbose: verbose?)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
sig { returns(T.nilable(Symbol)) }
|
||||||
def only_formula_or_cask
|
def only_formula_or_cask
|
||||||
if formula? && !cask?
|
if formula? && !cask?
|
||||||
:formula
|
:formula
|
||||||
@ -141,11 +155,13 @@ module Homebrew
|
|||||||
|
|
||||||
private
|
private
|
||||||
|
|
||||||
|
sig { params(option: String).returns(String) }
|
||||||
def option_to_name(option)
|
def option_to_name(option)
|
||||||
option.sub(/\A--?/, "")
|
option.sub(/\A--?/, "")
|
||||||
.tr("-", "_")
|
.tr("-", "_")
|
||||||
end
|
end
|
||||||
|
|
||||||
|
sig { returns(T::Array[String]) }
|
||||||
def cli_args
|
def cli_args
|
||||||
return @cli_args if @cli_args
|
return @cli_args if @cli_args
|
||||||
|
|
||||||
@ -165,10 +181,12 @@ module Homebrew
|
|||||||
@cli_args.freeze
|
@cli_args.freeze
|
||||||
end
|
end
|
||||||
|
|
||||||
def respond_to_missing?(method_name, *)
|
sig { params(method_name: Symbol, _include_private: T::Boolean).returns(T::Boolean) }
|
||||||
|
def respond_to_missing?(method_name, _include_private = false)
|
||||||
@table.key?(method_name)
|
@table.key?(method_name)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
sig { params(method_name: Symbol, args: T.untyped).returns(T.untyped) }
|
||||||
def method_missing(method_name, *args)
|
def method_missing(method_name, *args)
|
||||||
return_value = super
|
return_value = super
|
||||||
|
|
||||||
|
|||||||
@ -16,7 +16,6 @@ module Homebrew
|
|||||||
# 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
|
||||||
ArgType = T.type_alias { T.any(NilClass, Symbol, T::Array[String], T::Array[Symbol]) }
|
ArgType = T.type_alias { T.any(NilClass, Symbol, T::Array[String], T::Array[Symbol]) }
|
||||||
OptionsType = T.type_alias { T::Array[[String, T.nilable(String), T.nilable(String), String, T::Boolean]] }
|
|
||||||
# rubocop:enable Style/MutableConstant
|
# rubocop:enable Style/MutableConstant
|
||||||
HIDDEN_DESC_PLACEHOLDER = "@@HIDDEN@@"
|
HIDDEN_DESC_PLACEHOLDER = "@@HIDDEN@@"
|
||||||
SYMBOL_TO_USAGE_MAPPING = T.let({
|
SYMBOL_TO_USAGE_MAPPING = T.let({
|
||||||
@ -25,7 +24,7 @@ module Homebrew
|
|||||||
}.freeze, T::Hash[Symbol, String])
|
}.freeze, T::Hash[Symbol, String])
|
||||||
private_constant :ArgType, :HIDDEN_DESC_PLACEHOLDER, :SYMBOL_TO_USAGE_MAPPING
|
private_constant :ArgType, :HIDDEN_DESC_PLACEHOLDER, :SYMBOL_TO_USAGE_MAPPING
|
||||||
|
|
||||||
sig { returns(OptionsType) }
|
sig { returns(Args::OptionsType) }
|
||||||
attr_reader :processed_options
|
attr_reader :processed_options
|
||||||
|
|
||||||
sig { returns(T::Boolean) }
|
sig { returns(T::Boolean) }
|
||||||
@ -177,7 +176,7 @@ module Homebrew
|
|||||||
@constraints = T.let([], T::Array[[String, String]])
|
@constraints = T.let([], T::Array[[String, String]])
|
||||||
@conflicts = T.let([], T::Array[T::Array[String]])
|
@conflicts = T.let([], T::Array[T::Array[String]])
|
||||||
@switch_sources = T.let({}, T::Hash[String, Symbol])
|
@switch_sources = T.let({}, T::Hash[String, Symbol])
|
||||||
@processed_options = T.let([], OptionsType)
|
@processed_options = T.let([], Args::OptionsType)
|
||||||
@non_global_processed_options = T.let([], T::Array[[String, ArgType]])
|
@non_global_processed_options = T.let([], T::Array[[String, ArgType]])
|
||||||
@named_args_type = T.let(nil, T.nilable(ArgType))
|
@named_args_type = T.let(nil, T.nilable(ArgType))
|
||||||
@max_named_args = T.let(nil, T.nilable(Integer))
|
@max_named_args = T.let(nil, T.nilable(Integer))
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user