audit: Use OptionParser to generate help text

This commit is contained in:
Gautham Goli 2018-06-28 09:28:19 +05:30
parent 8f03ea7ca9
commit 32e5a5686b
No known key found for this signature in database
GPG Key ID: 6A9ABBC284468364
3 changed files with 44 additions and 20 deletions

View File

@ -73,7 +73,7 @@ begin
# - a help flag is passed AND there is no command specified # - a help flag is passed AND there is no command specified
# - no arguments are passed # - no arguments are passed
# - if cmd is Cask, let Cask handle the help command instead # - if cmd is Cask, let Cask handle the help command instead
if (empty_argv || help_flag) && cmd != "cask" if (empty_argv || help_flag) && cmd != "cask" && !internal_dev_cmd
require "help" require "help"
Homebrew::Help.help cmd, empty_argv: empty_argv Homebrew::Help.help cmd, empty_argv: empty_argv
# `Homebrew.help` never returns, except for external/unknown commands. # `Homebrew.help` never returns, except for external/unknown commands.

View File

@ -17,13 +17,21 @@ module Homebrew
@constraints = [] @constraints = []
@conflicts = [] @conflicts = []
instance_eval(&block) instance_eval(&block)
post_initialize
end
def post_initialize
@parser.on_tail("-h", "--help", "Show this message") do
puts @parser
exit
end
end end
def switch(*names, description: nil, env: nil, required_for: nil, depends_on: nil) def switch(*names, description: nil, env: nil, required_for: nil, depends_on: nil)
description = option_to_description(*names) if description.nil?
global_switch = names.first.is_a?(Symbol) global_switch = names.first.is_a?(Symbol)
names, env = common_switch(*names) if global_switch names, env, description = common_switch(*names) if global_switch
@parser.on(*names, description) do description = option_to_description(*names) if description.nil?
@parser.on(*names, *description.split("\n")) do
enable_switch(*names) enable_switch(*names)
end end
@ -34,9 +42,13 @@ module Homebrew
enable_switch(*names) if !env.nil? && !ENV["HOMEBREW_#{env.to_s.upcase}"].nil? enable_switch(*names) if !env.nil? && !ENV["HOMEBREW_#{env.to_s.upcase}"].nil?
end end
def banner(text)
@parser.banner = text
end
def comma_array(name, description: nil) def comma_array(name, description: nil)
description = option_to_description(name) if description.nil? description = option_to_description(name) if description.nil?
@parser.on(name, OptionParser::REQUIRED_ARGUMENT, Array, description) do |list| @parser.on(name, OptionParser::REQUIRED_ARGUMENT, Array, *description.split("\n")) do |list|
Homebrew.args[option_to_name(name)] = list Homebrew.args[option_to_name(name)] = list
end end
end end
@ -49,7 +61,7 @@ module Homebrew
required = OptionParser::OPTIONAL_ARGUMENT required = OptionParser::OPTIONAL_ARGUMENT
end end
description = option_to_description(name) if description.nil? description = option_to_description(name) if description.nil?
@parser.on(name, description, required) do |option_value| @parser.on(name, *description.split("\n"), required) do |option_value|
Homebrew.args[option_to_name(name)] = option_value Homebrew.args[option_to_name(name)] = option_value
end end
@ -78,6 +90,10 @@ module Homebrew
names.map { |name| name.to_s.sub(/\A--?/, "").tr("-", " ") }.max names.map { |name| name.to_s.sub(/\A--?/, "").tr("-", " ") }.max
end end
def summary
@parser.to_s
end
def parse(cmdline_args) def parse(cmdline_args)
remaining_args = @parser.parse(cmdline_args) remaining_args = @parser.parse(cmdline_args)
check_constraint_violations check_constraint_violations
@ -95,10 +111,10 @@ module Homebrew
# These are common/global switches accessible throughout Homebrew # These are common/global switches accessible throughout Homebrew
def common_switch(name) def common_switch(name)
case name case name
when :quiet then [["-q", "--quiet"], :quiet] when :quiet then [["-q", "--quiet"], :quiet, "Suppress warnings."]
when :verbose then [["-v", "--verbose"], :verbose] when :verbose then [["-v", "--verbose"], :verbose, "Verbose mode."]
when :debug then [["-d", "--debug"], :debug] when :debug then [["-d", "--debug"], :debug, "Display debug info."]
when :force then [["-f", "--force"], :force] when :force then [["-f", "--force"], :force, "Override any warnings/validations."]
else name else name
end end
end end

View File

@ -54,19 +54,27 @@ module Homebrew
def audit def audit
Homebrew::CLI::Parser.parse do Homebrew::CLI::Parser.parse do
switch "--strict" banner <<~EOS
switch "--online" Usage: brew audit [options] [<formulae>]
switch "--new-formula"
switch "--fix" Check <formulae> for Homebrew coding style violations. This should be
switch "--display-cop-names" run before submitting a new formula.
switch "--display-filename"
If no <formulae> are provided, all of them are checked.
EOS
switch "--strict", description: "Run additional style checks, including Rubocop style checks."
switch "--online", description: "Run additional slower style checks that require a\nnetwork connection."
switch "--new-formula", description: "Run various additional style checks to determine if a new formula \nis eligible for Homebrew. This should be used when creating \nnew formula and implies --strict and --online."
switch "--fix", description: "Fix style violations automatically using\nRuboCop's auto-correct feature."
switch "--display-cop-names", description: "Include the RuboCop cop name for each violation\nin the output."
switch "--display-filename", description: "Prefix everyline of output with name of the file or\nformula being audited, to make output easy to grep."
switch "-D", "--audit-debug", description: "Activates debugging and profiling" switch "-D", "--audit-debug", description: "Activates debugging and profiling"
comma_array "--only", description: "Passing --only=method will run only the methods named audit_method,\n`method` should be a comma-separated list."
comma_array "--except", description: "Passing --except=method will run only the methods named audit_method,\n`method` should be a comma-separated list."
comma_array "--only-cops", description: "Passing --only-cops=cops will check for violations of only the listed\nRuboCop cops. `cops` should be a comma-separated list of cop names."
comma_array "--except-cops", description: "Passing --except-cops=cops will skip checking the listed\nRuboCop cops violations. `cops` should be a comma-separated list of cop names."
switch :verbose switch :verbose
switch :debug switch :debug
comma_array "--only"
comma_array "--except"
comma_array "--only-cops"
comma_array "--except-cops"
end end
Homebrew.auditing = true Homebrew.auditing = true