From 3af16832d9baa6ebe3d85701734720d0fe75bea4 Mon Sep 17 00:00:00 2001 From: Rylan Polster Date: Tue, 5 Jan 2021 22:44:39 -0500 Subject: [PATCH] Generate bash completions automatically --- Library/Homebrew/cli/parser.rb | 3 +- Library/Homebrew/cmd/options.rb | 26 +- Library/Homebrew/cmd/upgrade.rb | 2 +- Library/Homebrew/commands.rb | 46 +- Library/Homebrew/completions.rb | 102 + Library/Homebrew/completions/bash.erb | 167 ++ Library/Homebrew/dev-cmd/man.rb | 2 + completions/bash/brew | 2446 +++++++++++++++++++----- completions/internal_commands_list.txt | 3 - 9 files changed, 2311 insertions(+), 486 deletions(-) create mode 100644 Library/Homebrew/completions/bash.erb diff --git a/Library/Homebrew/cli/parser.rb b/Library/Homebrew/cli/parser.rb index 5ebd993613..f54633f9ea 100644 --- a/Library/Homebrew/cli/parser.rb +++ b/Library/Homebrew/cli/parser.rb @@ -16,7 +16,7 @@ module Homebrew class Parser extend T::Sig - attr_reader :processed_options, :hide_from_man_page + attr_reader :processed_options, :hide_from_man_page, :named_args_type def self.from_cmd_path(cmd_path) cmd_args_method_name = Commands.args_method_name(cmd_path) @@ -539,6 +539,7 @@ module Homebrew def process_option(*args) option, = @parser.make_switch(args) + @processed_options.reject! { |existing| option.long.first.present? && existing.second == option.long.first } @processed_options << [option.short.first, option.long.first, option.arg, option.desc.first] end diff --git a/Library/Homebrew/cmd/options.rb b/Library/Homebrew/cmd/options.rb index f87a2f81ec..29a7038e34 100644 --- a/Library/Homebrew/cmd/options.rb +++ b/Library/Homebrew/cmd/options.rb @@ -3,8 +3,6 @@ require "formula" require "options" -require "cli/parser" -require "commands" module Homebrew extend T::Sig @@ -42,15 +40,7 @@ module Homebrew elsif args.installed? puts_options Formula.installed.sort, args: args elsif !args.command.nil? - path = Commands.path(args.command) - odie "Unknown command: #{args.command}" unless path - cmd_options = if cmd_parser = CLI::Parser.from_cmd_path(path) - cmd_parser.processed_options.map do |short, long, _, desc| - [long || short, desc] - end - else - cmd_comment_options(path) - end + cmd_options = Commands.command_options(args.command) if args.compact? puts cmd_options.sort.map(&:first) * " " else @@ -64,20 +54,6 @@ module Homebrew end end - def cmd_comment_options(cmd_path) - options = [] - comment_lines = cmd_path.read.lines.grep(/^#:/) - return options if comment_lines.empty? - - # skip the comment's initial usage summary lines - comment_lines.slice(2..-1).each do |line| - if / (?