-
-
Notifications
You must be signed in to change notification settings - Fork 9.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Enable strict typing in CLI::Parser
#17030
Conversation
@@ -26,6 +26,9 @@ def command_name = Utils.underscore(T.must(name).split("::").fetch(-1)).tr("_", | |||
sig { params(name: String).returns(T.nilable(T.class_of(AbstractCommand))) } | |||
def command(name) = subclasses.find { _1.command_name == name } | |||
|
|||
sig { returns(T::Boolean) } | |||
def dev_cmd? = T.must(name).start_with?("Homebrew::DevCmd") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This logic seems more appropriate to capture here
@@ -9,15 +9,28 @@ | |||
require "set" | |||
require "utils/tty" | |||
|
|||
COMMAND_DESC_WIDTH = 80 | |||
OPTION_DESC_WIDTH = 45 | |||
HIDDEN_DESC_PLACEHOLDER = "@@HIDDEN@@" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I moved these out of top level while I was here
@@ -119,49 +133,52 @@ def self.global_options | |||
] | |||
end | |||
|
|||
sig { params(option: String).returns(String) } | |||
def self.option_to_name(option) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I hoisted this with other class methods, to follow class layout conventions
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There might be a cop for that.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Indeed, there is (though disabled by default)! https://docs.rubocop.org/rubocop/cops_layout.html#layoutclassstructure
@parser.summary_indent = " " * 2 | ||
|
||
@parser = T.let(OptionParser.new, OptionParser) | ||
@parser.summary_indent = " " |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It made more sense to just do the math on this one, IMO
else | ||
# FIXME: remove once commands are all subclasses of `AbstractCommand`: | ||
# Filter out Sorbet runtime type checking method calls. | ||
cmd_location = T.must(caller_locations).select do |location| | ||
cmd_location = caller_locations.select do |location| |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not really sure why the T.must is no longer necessary, but \o/
@min_named_args = @max_named_args = number | ||
elsif min.present? || max.present? | ||
elsif min || max |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
removed the unecessary present?
s while i was here
@@ -459,11 +479,7 @@ def hide_from_man_page! | |||
|
|||
private | |||
|
|||
SYMBOL_TO_USAGE_MAPPING = { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this isn't actually private, i hoisted it and added a private_constant
declaration
Library/Homebrew/cli/parser.rb
Outdated
@@ -590,12 +614,13 @@ def check_conflicts | |||
end | |||
|
|||
select_cli_arg = violations.count - env_var_options.count == 1 | |||
raise OptionConflictError, violations.map(&method(:name_to_option)) unless select_cli_arg | |||
raise OptionConflictError, violations.map { name_to_option(_1) } unless select_cli_arg |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
sorbet requires these to be removed under strict
typing, to validate method arity
@@ -282,17 +294,23 @@ def name_to_option(name) | |||
end | |||
end | |||
|
|||
sig { params(names: String).returns(T.nilable(String)) } | |||
def option_to_description(*names) | |||
names.map { |name| name.to_s.sub(/\A--?/, "").tr("-", " ") }.max |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i think this logic is wrong (pretty sure it's choosing alphabetically, which seems off), but i left it alone
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I agree. It's only used in the #option_description
method as a fallback if an explicit description doesn't exist. It'd probably make more sense to grab the max by string length instead. I think all of our internal commands have descriptions so it doesn't really matter much for us. I'm fine with fixing it here or as a follow-up though.
@@ -7,6 +7,9 @@ | |||
# | |||
# @api private | |||
module Formatter | |||
COMMAND_DESC_WIDTH = 80 | |||
OPTION_DESC_WIDTH = 45 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These are only used in calls to Formatter
, so seemed to make the most sense here
6a96f88
to
46549dd
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great work here @dduugg! A few nits but happy for them to be addressed in follow-up PRs or inline and then self-merged.
sig { params(text: T.nilable(String)).returns(T.nilable(String)) } | ||
def description(text = nil) | ||
return @description if text.blank? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Doesn't need changed now but: this type of method API kinda sucks with Sorbet to me. Better to fall back to getters/setters.
Similarly, I wonder about returning empty strings more often rather than nilable strings.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@dduugg One final "add a Ruby comment" nit and I'm ✅ to ship!
@@ -119,49 +133,52 @@ def self.global_options | |||
] | |||
end | |||
|
|||
sig { params(option: String).returns(String) } | |||
def self.option_to_name(option) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There might be a cop for that.
@@ -282,17 +294,23 @@ def name_to_option(name) | |||
end | |||
end | |||
|
|||
sig { params(names: String).returns(T.nilable(String)) } | |||
def option_to_description(*names) | |||
names.map { |name| name.to_s.sub(/\A--?/, "").tr("-", " ") }.max |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I agree. It's only used in the #option_description
method as a fallback if an explicit description doesn't exist. It'd probably make more sense to grab the max by string length instead. I think all of our internal commands have descriptions so it doesn't really matter much for us. I'm fine with fixing it here or as a follow-up though.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@dduugg Good to self-merge when you are!
brew style
with your changes locally?brew typecheck
with your changes locally?brew tests
with your changes locally?I just wanted to capture #parser to work on Args namespacing, but decided to go all the way. (There's a pretty decent chance of type error creeping in, it's hard to find the right balance of rigor & narrowness without allowing an opportunity for some to slip in.)