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 (empty_argv || help_flag) && cmd != "cask"
require "help"
Homebrew::Help.help cmd, empty_argv: empty_argv, internal_cmd: internal_cmd
# `Homebrew.help` never returns, except for external/unknown commands.
Homebrew::Help.help cmd, empty_argv: empty_argv
# `Homebrew.help` never returns, except for unknown commands.
end
if internal_cmd

View File

@ -16,6 +16,22 @@ module Homebrew
new(args, &block).parse(args)
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
{
quiet: [["-q", "--quiet"], :quiet, "Suppress any warnings."],

View File

@ -4,8 +4,6 @@ require "formula"
require "erb"
require "ostruct"
require "cli/parser"
# Require all commands
Dir.glob("#{HOMEBREW_LIBRARY_PATH}/{dev-,}cmd/*.rb").sort.each { |cmd| require cmd }
module Homebrew
module_function
@ -151,19 +149,13 @@ module Homebrew
# preserve existing manpage order
cmd_paths.sort_by(&method(:sort_key_for_path))
.each do |cmd_path|
cmd_args_method_name = cmd_arg_parser(cmd_path)
cmd_man_page_lines = begin
cmd_parser = Homebrew.send(cmd_args_method_name)
cmd_man_page_lines = if cmd_parser = CLI::Parser.from_cmd_path(cmd_path)
next if cmd_parser.hide_from_man_page
cmd_parser_manpage_lines(cmd_parser).join
rescue NoMethodError => e
raise if e.name != cmd_args_method_name
nil
else
cmd_comment_manpage_lines(cmd_path)
end
cmd_man_page_lines ||= cmd_comment_manpage_lines(cmd_path)
man_page_lines << cmd_man_page_lines
end
@ -171,10 +163,6 @@ module Homebrew
man_page_lines.compact.join("\n")
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)
lines = [format_usage_banner(cmd_parser.usage_banner_text)]
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.
return if path.nil?
# Resume execution in `brew.rb` for external commands using "cli/parser".
return if !flags[:internal_cmd] && path.read.match?("require .cli/parser.")
# Display help for internal command (or generic help if undocumented).
# Display help for commands (or generic help if undocumented).
puts command_help(path)
exit 0
end
def command_help(path)
# Let OptionParser generate help text for commands which have a parser defined
cmd = path.basename(path.extname)
cmd_args_method_name = "#{cmd.to_s.tr("-", "_")}_args".to_sym
begin
return Homebrew.send(cmd_args_method_name)
.generate_help_text
rescue NoMethodError => e
raise if e.name != cmd_args_method_name
nil
# Let OptionParser generate help text for commands which have a parser
if cmd_parser = CLI::Parser.from_cmd_path(path)
return cmd_parser.generate_help_text
end
# Otherwise read #: lines from the file.