| 
									
										
										
										
											2019-04-19 15:38:03 +09:00
										 |  |  | # frozen_string_literal: true | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-04-17 18:25:08 +09:00
										 |  |  | require "cli/parser" | 
					
						
							| 
									
										
										
										
											2018-06-05 23:19:18 -04:00
										 |  |  | require "commands" | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | module Homebrew | 
					
						
							| 
									
										
										
										
											2020-08-17 18:31:37 +02:00
										 |  |  |   # Helper module for printing help output. | 
					
						
							|  |  |  |   # | 
					
						
							|  |  |  |   # @api private | 
					
						
							| 
									
										
										
										
											2018-06-05 23:19:18 -04:00
										 |  |  |   module Help | 
					
						
							| 
									
										
										
										
											2020-08-17 18:31:37 +02:00
										 |  |  |     # NOTE: Keep the length of vanilla `--help` less than 25 lines! | 
					
						
							|  |  |  |     #       This is because the default Terminal height is 25 lines. Scrolling sucks | 
					
						
							|  |  |  |     #       and concision is important. If more help is needed we should start | 
					
						
							|  |  |  |     #       specialising help like the gem command does. | 
					
						
							|  |  |  |     # NOTE: Keep lines less than 80 characters! Wrapping is just not cricket. | 
					
						
							|  |  |  |     HOMEBREW_HELP = <<~EOS | 
					
						
							|  |  |  |       Example usage: | 
					
						
							|  |  |  |         brew search [TEXT|/REGEX/] | 
					
						
							|  |  |  |         brew info [FORMULA...] | 
					
						
							|  |  |  |         brew install FORMULA... | 
					
						
							|  |  |  |         brew update | 
					
						
							|  |  |  |         brew upgrade [FORMULA...] | 
					
						
							|  |  |  |         brew uninstall FORMULA... | 
					
						
							|  |  |  |         brew list [FORMULA...] | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       Troubleshooting: | 
					
						
							|  |  |  |         brew config | 
					
						
							|  |  |  |         brew doctor | 
					
						
							|  |  |  |         brew install --verbose --debug FORMULA | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       Contributing: | 
					
						
							|  |  |  |         brew create [URL [--no-fetch]] | 
					
						
							|  |  |  |         brew edit [FORMULA...] | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       Further help: | 
					
						
							|  |  |  |         brew commands | 
					
						
							|  |  |  |         brew help [COMMAND] | 
					
						
							|  |  |  |         man brew | 
					
						
							|  |  |  |         https://docs.brew.sh | 
					
						
							|  |  |  |     EOS | 
					
						
							|  |  |  |     private_constant :HOMEBREW_HELP | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-06-05 23:19:18 -04:00
										 |  |  |     module_function | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-02 15:36:05 +02:00
										 |  |  |     def help(cmd = nil, empty_argv: false, usage_error: nil, remaining_args: []) | 
					
						
							| 
									
										
										
										
											2020-07-30 18:40:10 +02:00
										 |  |  |       if cmd.nil? | 
					
						
							|  |  |  |         # Handle `brew` (no arguments). | 
					
						
							|  |  |  |         if empty_argv | 
					
						
							|  |  |  |           $stderr.puts HOMEBREW_HELP | 
					
						
							|  |  |  |           exit 1
 | 
					
						
							|  |  |  |         end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         # Handle `brew (-h|--help|--usage|-?|help)` (no other arguments). | 
					
						
							|  |  |  |         puts HOMEBREW_HELP | 
					
						
							|  |  |  |         exit 0
 | 
					
						
							|  |  |  |       end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-06-05 23:19:18 -04:00
										 |  |  |       # Resolve command aliases and find file containing the implementation. | 
					
						
							| 
									
										
										
										
											2020-07-30 18:40:10 +02:00
										 |  |  |       path = Commands.path(cmd) | 
					
						
							| 
									
										
										
										
											2018-06-05 23:19:18 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  |       # Display command-specific (or generic) help in response to `UsageError`. | 
					
						
							| 
									
										
										
										
											2020-07-30 18:40:10 +02:00
										 |  |  |       if usage_error | 
					
						
							| 
									
										
										
										
											2020-08-02 15:36:05 +02:00
										 |  |  |         $stderr.puts path ? command_help(cmd, path, remaining_args: remaining_args) : HOMEBREW_HELP | 
					
						
							| 
									
										
										
										
											2018-06-05 23:19:18 -04:00
										 |  |  |         $stderr.puts | 
					
						
							| 
									
										
										
										
											2020-07-30 18:40:10 +02:00
										 |  |  |         onoe usage_error | 
					
						
							| 
									
										
										
										
											2018-06-05 23:19:18 -04:00
										 |  |  |         exit 1
 | 
					
						
							|  |  |  |       end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       # Resume execution in `brew.rb` for unknown commands. | 
					
						
							|  |  |  |       return if path.nil? | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-02 17:05:45 +01:00
										 |  |  |       # Display help for internal command (or generic help if undocumented). | 
					
						
							| 
									
										
										
										
											2020-08-02 15:36:05 +02:00
										 |  |  |       puts command_help(cmd, path, remaining_args: remaining_args) | 
					
						
							| 
									
										
										
										
											2018-06-05 23:19:18 -04:00
										 |  |  |       exit 0
 | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-02 15:36:05 +02:00
										 |  |  |     def command_help(cmd, path, remaining_args:) | 
					
						
							| 
									
										
										
										
											2020-02-02 17:05:45 +01:00
										 |  |  |       # Only some types of commands can have a parser. | 
					
						
							|  |  |  |       output = if Commands.valid_internal_cmd?(cmd) || | 
					
						
							|  |  |  |                   Commands.valid_internal_dev_cmd?(cmd) || | 
					
						
							|  |  |  |                   Commands.external_ruby_v2_cmd_path(cmd) | 
					
						
							| 
									
										
										
										
											2020-08-02 15:36:05 +02:00
										 |  |  |         parser_help(path, remaining_args: remaining_args) | 
					
						
							| 
									
										
										
										
											2018-10-03 21:12:44 +05:30
										 |  |  |       end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-02 17:05:45 +01:00
										 |  |  |       output ||= comment_help(path) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       output ||= if output.blank? | 
					
						
							| 
									
										
										
										
											2020-04-05 15:44:50 +01:00
										 |  |  |         opoo "No help text in: #{path}" if Homebrew::EnvConfig.developer? | 
					
						
							| 
									
										
										
										
											2020-02-02 17:05:45 +01:00
										 |  |  |         HOMEBREW_HELP | 
					
						
							| 
									
										
										
										
											2018-06-05 23:19:18 -04:00
										 |  |  |       end | 
					
						
							| 
									
										
										
										
											2020-02-02 11:32:50 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-02 17:05:45 +01:00
										 |  |  |       output | 
					
						
							|  |  |  |     end | 
					
						
							| 
									
										
										
										
											2020-08-17 18:31:37 +02:00
										 |  |  |     private_class_method :command_help | 
					
						
							| 
									
										
										
										
											2020-02-02 17:05:45 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-02 15:36:05 +02:00
										 |  |  |     def parser_help(path, remaining_args:) | 
					
						
							| 
									
										
										
										
											2020-02-02 17:05:45 +01:00
										 |  |  |       # Let OptionParser generate help text for commands which have a parser. | 
					
						
							|  |  |  |       cmd_parser = CLI::Parser.from_cmd_path(path) | 
					
						
							|  |  |  |       return unless cmd_parser | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-31 17:37:36 +02:00
										 |  |  |       # Try parsing arguments here in order to show formula options in help output. | 
					
						
							| 
									
										
										
										
											2020-08-02 15:36:05 +02:00
										 |  |  |       cmd_parser.parse(remaining_args, ignore_invalid_options: true) | 
					
						
							| 
									
										
										
										
											2020-02-02 17:05:45 +01:00
										 |  |  |       cmd_parser.generate_help_text | 
					
						
							|  |  |  |     end | 
					
						
							| 
									
										
										
										
											2020-08-17 18:31:37 +02:00
										 |  |  |     private_class_method :parser_help | 
					
						
							| 
									
										
										
										
											2020-02-02 17:05:45 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-24 00:45:14 +02:00
										 |  |  |     def command_help_lines(path) | 
					
						
							|  |  |  |       path.read | 
					
						
							|  |  |  |           .lines | 
					
						
							|  |  |  |           .grep(/^#:/) | 
					
						
							|  |  |  |           .map { |line| line.slice(2..-1).delete_prefix("  ") } | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  |     private_class_method :command_help_lines | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-02 17:05:45 +01:00
										 |  |  |     def comment_help(path) | 
					
						
							|  |  |  |       # Otherwise read #: lines from the file. | 
					
						
							|  |  |  |       help_lines = command_help_lines(path) | 
					
						
							|  |  |  |       return if help_lines.blank? | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-25 11:59:42 -04:00
										 |  |  |       Formatter.wrap(help_lines.join, COMMAND_DESC_WIDTH) | 
					
						
							| 
									
										
										
										
											2020-02-02 11:32:50 +01:00
										 |  |  |                .sub("@hide_from_man_page ", "") | 
					
						
							|  |  |  |                .sub(/^\* /, "#{Tty.bold}Usage: brew#{Tty.reset} ") | 
					
						
							|  |  |  |                .gsub(/`(.*?)`/m, "#{Tty.bold}\\1#{Tty.reset}") | 
					
						
							|  |  |  |                .gsub(%r{<([^\s]+?://[^\s]+?)>}) { |url| Formatter.url(url) } | 
					
						
							|  |  |  |                .gsub(/<(.*?)>/m, "#{Tty.underline}\\1#{Tty.reset}") | 
					
						
							|  |  |  |                .gsub(/\*(.*?)\*/m, "#{Tty.underline}\\1#{Tty.reset}") | 
					
						
							| 
									
										
										
										
											2018-06-05 23:19:18 -04:00
										 |  |  |     end | 
					
						
							| 
									
										
										
										
											2020-08-17 18:31:37 +02:00
										 |  |  |     private_class_method :comment_help | 
					
						
							| 
									
										
										
										
											2018-06-05 23:19:18 -04:00
										 |  |  |   end | 
					
						
							|  |  |  | end |