Move shared man/help logic to cli/parser.

This commit is contained in:
Mike McQuaid 2020-02-01 19:05:03 +01:00
parent cd423a2755
commit b03efb4356
No known key found for this signature in database
GPG Key ID: 48A898132FD8EE70
4 changed files with 25 additions and 31 deletions

View File

@ -94,8 +94,8 @@ begin
# - 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"
require "help" require "help"
Homebrew::Help.help cmd, empty_argv: empty_argv, internal_cmd: internal_cmd Homebrew::Help.help cmd, empty_argv: empty_argv
# `Homebrew.help` never returns, except for external/unknown commands. # `Homebrew.help` never returns, except for unknown commands.
end end
if internal_cmd if internal_cmd

View File

@ -16,6 +16,22 @@ module Homebrew
new(args, &block).parse(args) new(args, &block).parse(args)
end end
def self.from_cmd_path(cmd_path)
cmd_method_prefix = cmd_path.basename(cmd_path.extname)
.to_s
.sub(/^brew-/, "")
.tr("-", "_")
cmd_args_method_name = "#{cmd_method_prefix}_args".to_sym
begin
Homebrew.send(cmd_args_method_name) if require?(cmd_path)
rescue NoMethodError => e
raise if e.name != cmd_args_method_name
nil
end
end
def self.global_options def self.global_options
{ {
quiet: [["-q", "--quiet"], :quiet, "Suppress any warnings."], quiet: [["-q", "--quiet"], :quiet, "Suppress any warnings."],

View File

@ -4,8 +4,6 @@ require "formula"
require "erb" require "erb"
require "ostruct" require "ostruct"
require "cli/parser" require "cli/parser"
# Require all commands
Dir.glob("#{HOMEBREW_LIBRARY_PATH}/{dev-,}cmd/*.rb").sort.each { |cmd| require cmd }
module Homebrew module Homebrew
module_function module_function
@ -151,19 +149,13 @@ module Homebrew
# preserve existing manpage order # preserve existing manpage order
cmd_paths.sort_by(&method(:sort_key_for_path)) cmd_paths.sort_by(&method(:sort_key_for_path))
.each do |cmd_path| .each do |cmd_path|
cmd_args_method_name = cmd_arg_parser(cmd_path) cmd_man_page_lines = if cmd_parser = CLI::Parser.from_cmd_path(cmd_path)
cmd_man_page_lines = begin
cmd_parser = Homebrew.send(cmd_args_method_name)
next if cmd_parser.hide_from_man_page next if cmd_parser.hide_from_man_page
cmd_parser_manpage_lines(cmd_parser).join cmd_parser_manpage_lines(cmd_parser).join
rescue NoMethodError => e else
raise if e.name != cmd_args_method_name cmd_comment_manpage_lines(cmd_path)
nil
end end
cmd_man_page_lines ||= cmd_comment_manpage_lines(cmd_path)
man_page_lines << cmd_man_page_lines man_page_lines << cmd_man_page_lines
end end
@ -171,10 +163,6 @@ module Homebrew
man_page_lines.compact.join("\n") man_page_lines.compact.join("\n")
end end
def cmd_arg_parser(cmd_path)
"#{cmd_path.basename.to_s.gsub(".rb", "").tr("-", "_")}_args".to_sym
end
def cmd_parser_manpage_lines(cmd_parser) def cmd_parser_manpage_lines(cmd_parser)
lines = [format_usage_banner(cmd_parser.usage_banner_text)] lines = [format_usage_banner(cmd_parser.usage_banner_text)]
lines += cmd_parser.processed_options.map do |short, long, _, desc| lines += cmd_parser.processed_options.map do |short, long, _, desc|

View File

@ -72,25 +72,15 @@ module Homebrew
# Resume execution in `brew.rb` for unknown commands. # Resume execution in `brew.rb` for unknown commands.
return if path.nil? return if path.nil?
# Resume execution in `brew.rb` for external commands using "cli/parser". # Display help for commands (or generic help if undocumented).
return if !flags[:internal_cmd] && path.read.match?("require .cli/parser.")
# Display help for internal command (or generic help if undocumented).
puts command_help(path) puts command_help(path)
exit 0 exit 0
end end
def command_help(path) def command_help(path)
# Let OptionParser generate help text for commands which have a parser defined # Let OptionParser generate help text for commands which have a parser
cmd = path.basename(path.extname) if cmd_parser = CLI::Parser.from_cmd_path(path)
cmd_args_method_name = "#{cmd.to_s.tr("-", "_")}_args".to_sym return cmd_parser.generate_help_text
begin
return Homebrew.send(cmd_args_method_name)
.generate_help_text
rescue NoMethodError => e
raise if e.name != cmd_args_method_name
nil
end end
# Otherwise read #: lines from the file. # Otherwise read #: lines from the file.